Search in a unimodal vector

In this problem, we say that a vector with nn integer numbers @v[0..@n1n-1@]@ is unimodal if n1n \ge 1, and there exists an index jj such that 0jn10 \leq j \leq n-1 and satisfying:

For instance, the vector @[0, 2, 5, 7, 6, 5, 4, 3, 1]@ is unimodal (with j=3j = 3).

Note that vectors with n2n \leq 2 different elements are unimodal. In general, note that any strictly increasing vector is also unimodal (and in all cases j=n1j = n-1), and analogously, any strictly decreasing vector is also unimodal (and then j=0j = 0).

Implement an efficient function

    bool search(int x, const vector<int>& v);

such that, given an integer number @x@ and a unimodal vector @v@, returns true if @x@ appears in @v@, and false otherwise. You can use and implement auxiliary functions if you need them.

Precondition

The vector @v@ is unimodal.

Observation

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

Problem information

Author: Unknown
Translator: Prof. EDA

Generation: 2026-01-25T22:41:26.180Z

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