Binary searching a decreasing list?
I have an array sorted in strictly decreasing order and an element val; i
want to find the index of largest element in the array which is less than
val(or equal if val already exists there) and i want to do so in logn
time. And reversing the array and doing upper_bound() is not an option.
Eg. if array is {10,5,3,1} and val is 6, the function should return 1.
I am really new to iterators and tried something like adding a comparison
function in upper_bound() to make it work but it failed. How should I go
about this.
Note: I checked for similar questions prior to posting and found one but
unfortunately it pertained to Java, so.
No comments:
Post a Comment