Search in a unimodal vector X82938


Statement
 

pdf   zip   main.cc

html

In this problem, we say that a vector with n integer numbers v[0..n−1] is unimodal if n ≥ 1, and there exists an index j such that 0 ≤ jn−1 and satisfying:

  • v[0] < … < v[j−1] < v[j], and
  • v[j] > v[j+1] > v[j+2] > … > v[n−1].


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



Note that vectors with n ≤ 2 different elements are unimodal. In general, note that any strictly increasing vector is also unimodal (and in all cases j = n−1), and analogously, any strictly decreasing vector is also unimodal (and then j = 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.

Information
Author
Prof. EDA
Language
English
Translator
Prof. EDA
Original language
Catalan
Other languages
Catalan
Official solutions
C++
User solutions
C++