R9 found at last


where am I going wrong?

Hello @Aditiverma,

There are four issues with your program:

  1. In the input, the elements should be space-separated.
    You are passing it as a single integer: 14525

  2. Pass 0 as ans during the time of first function call.
    As you have not initialized ans, so it will pass junk value as ans.

  3. Pass ans+1 in the recursive function call, as ans is keeping track of the index.

I have modified the above in your code:

  1. Even if you would correct the above-specified errors, the code would not produce the correct answer.
    Reason:
    You have to print the integer value of the last index that M is found at within the given array.
    Solution:
    Start checking in the reverse order i.e from rightmost index to leftmost index.
    4.1. Pass i as n-1 in the function call.
    4.1. decrement i in each recursive function call.

Hope, this would help.
Give a like, if you are satisfied.

1 Like

sir in this code one of the test cases is coming wrong when a number repeated so i tried to use for loop but still the ans is same what should be done?

Can, you give an example to explain your doubt.
Actually, i didn’t understand this.