The problem lies in finding an element equal to x or the minimal element of a larger x in a sorted vector (array). Bust does not suit me. I thought to use binary search, but how?

PS It is not necessary to write about binary search, all that will go through faster than searching. Thank you in advance!

  • Binary search is the right thing, you insert into the question what you tried to do, and then I think there are many people on the forum who can finish the algorithm with a good file until a good solution is made - Alexander Muksimov
  • And what's confusing in binary? All the time arr.length / 2 .. - Alexander
  • In sorted - binary: divided by half, determined which one is necessary (or unnecessary), ... Or upload to Excel and find it with a simple formula (you do not write what array you are, in what environment you work ...) - vikttur
  • Binary - we divide in half, we look, where the required element is, and we do the same with the necessary half. You can not divide in half, but proportionally - for example, if we have 100 elements, the minimum is 0, the maximum is 1000, we are looking for 50, then we divide into parts from zero to fifth, and from fifth to hundredth ... You state what you need - code, description of the algorithm or what? - Harry
  • I do not understand what is confusing in binary search. It is just designed to solve exactly the problem described. - Akina

0