Recursion challenge Sorted Array

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();

	int[] x = new int[N];
	for(int i=0;i<N;i++){
		x[i] = sc.nextInt();
	}

	System.out.println(isSorted(x,0));


}

public static boolean isSorted(int[] arr,int si){
	if(si==arr.length){
		return true;
	}

	if(arr[si]>arr[si+1]){
		return false;
	}
	else{
		boolean rr = isSorted(arr,si+1);
		return rr;
	}
}

}

//My this code is running in eclipse but not in ide provided by coding blocks

Hi @Siddharth_sharma1808,
You should use if(si==arr.length-1) return true because for in the case arr.length -1 is the last element and for that element si+1 is not defined which you are comparing in the next line of code … Therefore there is a index out of bound . for example if array is of size 5 then you recursion must stop at index 4 not at index 5. just make the first if statement in isSorted function to si==arr.length-1 and your code will work fine.

Hi @Siddharth_sharma1808,
I hope i resolved your doubt as i said on chat that you need to add Custom input … Happy to help

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.