Loading...
Loading...
Your college notice board lists the roll numbers of students selected for a campus drive. The list is sorted in increasing order, and no roll number appears twice.
Your roll number is x. Print its index in the list, counting from 0. If x is not in the list, print -1.
-1.Example 1
Input
6 42 3 15 27 42 58 91
Output
3
42 is the fourth roll number, so its index is 3.
Example 2
Input
5 20 5 10 15 25 30
Output
-1
20 would sit between 15 and 25, but it is not in the list.
Try the problem first. Open one hint at a time.
The list is sorted. Compare x with the middle roll number first.
If the middle value is smaller than x, every value on its left is smaller too, so ignore the whole left part.
Keep two indices, lo and hi, for the part that can still hold x. Stop when lo passes hi.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor