Recusive - Sorted array or not AND R9 - Found At Last

https://ide.codingblocks.com/s/54457
THis is my code for the above question but it cannot pass one test case #2.
Can anyone point me where I am wrong?
I am also getting test case #1 wrong answer in the R9 - Found at last.

In recursion U are are always comparing only a[0] and a[1],
You need to use a[i] and a[i+1] according to the index.

No, I am only comparing a[0] and a[1] because I am shortening the array. I take only the next n-1 elements and that is my new array.

Hey Hardik, your code will not work if same numbers are present in the array.
for eg.
input:
4
1 2 2 3

your code’s output:
false

but the expected output is :
true

So, to correct this update your code’s line:9 like this if(a[0]<=a[1] && IsSorted(a+1,n-1))

1 Like