Dichotomic search

Write an efficient recursive function that returns the position of @x@ in the subvector @v@[@left@..@right@]. The function must return 1-1 if @x@ does not belong to @v@[@left@..@right@] or if @left@ >> @right@.

Precondition

The vector @v@ is sorted in strictly increasing order. Moreover, we have 00 \le @left@ \le size of v and 1-1 \le @right@ << size of v.

Interface

C++
int position(double x, const vector<double>& v, int left, int right);
C
int position(double x, double v[], int left, int right);
Java
public static int position(double x, double[] v, int left, int right);
Python
position(x, v, left, right)  # returns int
MyPy
position(x: float, v: list[float], left: int, right: int) -> int

Observation

You only need to submit the required procedure; your main program will be ignored.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T11:55:57.549Z

© Jutge.org, 2006–2026.
https://jutge.org