by dry run its giving correct answer but by executing it’s giving wrong answer .
please check my code where i am doing mistake
All indices problem doubt
Sonu, can you pls tell me the approach you are following up in the code, I m not able to understand few of them…
ex ample
for n=6
arr[ ]={2,2,3,2,4,2};
key =2
here the indexes of key =2 in the array is 0,1,3,5
so in recursive call i am passing the array from indexes 1 to 5 . i.e {2,3,2,4,2}
the size of the answer array given by recursion is 3
and the answer given by recursive solution will be 0, 2,4. but it is 1 smaller than the our correct answer ,therefore i am increasing the elements of answer array given by recursion by 1. so now answer array will contain 1,3,5.
now our task is to check at index 0.
so if (arr[0]==key ){
then i have to make a space at index 0 in answer array to fill 0 also. so for creating a vacancy at 0th indexes i am shifting all the element of the answer array towards right by 1 . . now we have a vacancy at index 0 . so answer[0]=0
now the answer array contain 0,1,3,5
and return (size of answer array given by recursion )+1
}
else{
i.e we have not got the key at the 0th index so our size of answer array will be same as size of answer array returned by recursion .
so return (size of answer array given by recursion )
}
by same logic i have written the code again https://ide.codingblocks.com/s/161248
its giving the correct answer and passed all the testcases .
but still the previous code is not giving correct output
Ok, I check it … and let u know…