Test Case Failing

test case 0 is failing

import java.util.*;
public class Main {
public static void main(String args[]) {
// Your Code Here
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int [] arr = new int[n];
for(int i = 0; i < arr.length; i++){
arr[i] = scn.nextInt();
}

    System.out.println(isSorted(arr, 0));
}
public static Boolean isSorted(int[] arr, int i){
    Boolean ans = true;

    if(i >= arr.length-1){
        return true;
    }
	if(arr.length==1){
        return true;
    }

    if(arr[i] > arr[i+1]){
        ans= false;
    }else{
        isSorted(arr, i+1);
    }
    return ans;

}

}

HI @shruti_codess28
Here we have to Recursively traverse over the array. If it any point we reach an index where arr[i] > arr[i + 1] , we return false. If not , we simply return the result we obtain from further recursive call. Base case would be if we reach the end of the array while traversing , where we return true.

I have shared updated code here please check:-

okay thanks… but what was wrong with my code why test case 0 was failling??

okay thanks… but what was wrong with my code why test case 0 was failling??

Because of your base case i have explained that in explanation above code.

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.