0

Could you please help me with this code?

I'm trying to print the index of odd occuring element in the array. In this case it's supposed to return the index of 5 but it's showing ERROR: Run Timeout https://code.sololearn.com/c1qQMwilbWsL/?ref=app

23rd Feb 2023, 10:32 AM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
2 odpowiedzi
+ 4
Hemasri Kottapalli At the end of the while loop you are repeating: mid=(s+(e-s))/2; which is the same value you started with. Try incrementing the value of mid (mid++). But there still seem to be some possible logic errors. It helps to use extra cout statements thruout the code to follow the value of s, and possibly e as well. https://code.sololearn.com/cz82Y7JMuaar/?ref=app
23rd Feb 2023, 11:21 AM
Scott D
Scott D - avatar
+ 2
I tried to Resolve error u missing parentheses and return statement int findOdd(vector<int> arr) { int s=0; int e= arr.size()-1; int mid = (s+(e-s))/2; while(s<=e) { if(s==e) { return arr[s]; } //mid is evn if(mid%2==0) { if(arr[mid]==arr[mid+1]) { s=mid+2; } else{ e=mid; } } else{ if(arr[mid]==arr[mid-1]) { s=mid+1; } else{ e=mid-1; } return mid=(s+(e-s))/2; } } return 0; }
23rd Feb 2023, 4:27 PM
A S Raghuvanshi
A S Raghuvanshi - avatar