Write an efficient function
int rightmost(double x, const vector<double>& v);
that returns the rightmost position where could be inserted in the sorted vector (by adding one position at the end of and moving the necessary elements one position to the right) so that would remain sorted.
For instance, assume that is 23. If is , then we must insert at the position 3 (between 20 and 30), and the resulting would be . If is , then we could insert at the positions 1, 2 or 3, so your function must return 3. If is , should be inserted at the position just to the right of the end of the vector, that is, 4. As a final example, if is , should be inserted at 2.
The vector is sorted in nondecreasing order.
Your solution can only include the vector
library.
You can write and use additional functions if you need them.
You only need to submit the required procedure; your main program will be ignored.