Alarmed at the performance of students in the first midterm
Solution
When | xi - xi+1 | <=1 that means numbers are arranged in increasing /decreasing order and the difference between them is less than equal to 1
1. 5 5 6 6 7 7 8 9 10 ....etc
2. 10 9 8 7 6 5 5 4 4 3 2 1 0 -1 etc
Now x1 <= b <= xn i.e b lies between one of any number or equal to them
We can apply Binary search to find a number b
void FindStudent(int marks[], int b) {
take low = 0 and high = n
while(low<=high){
int mid = ( low +high) /2;
if (marks[mid]==b)
return mid;
if(marks[mid]< b)
low = mid +1;
else
high = mid -1 ;
}
}
1. Find mid and Search in the middle ..if we got the student with b marks then return mid
2. Then if middle marks is more than b then student with mark b is on the left , Make high as mid-1.
3. Otherwise if middle marks is less than b then student with mark b is on the right , Make low as mid+1.
In this way we are dividing the array by 2 everytime. Hence the Time complexity is O(log n).
