Left and right variables

why left and right variables are taken -1 initially

please solve the doubt soon as possible

Hey @Chhayansh we initialize left and right to -1 so that we arent assuming any initial solution if we would take left and right = 1 it would have been equivalent of saying that before iterating the array we think a[1] is the solution we take -1 since its not present of if left and right remains -1 we would know that the if condition was never satisfied. Since in this question answer definitely exists you can start left and right however you want but in questions where answer might not exist say for example if i ask about maximum subarray sum with positive value then it is possible that answer might not exist in that case if left and right remains -1 we will know that answer doesnt exist.

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.