14. Search

Difficulty: 

Given a sorted array of numbers, we would like to search for a specific number. Implement a function that returns the index of that number, or if it is not in the array, the index where it would be.

For example, for [1, 3, 5, 16], 3 should return 1, 10 should return 3, and 18 should return 4.

For simplicity, assume if the number exists in the array, there will only be one instance of it. Try to solve the problem in less than a linear time.

Input:

search([
  1,
  3,
  5,
  16
], -1);

Output:

0

Time Limit:

50 (ms)

Hint (hover to see):

  • Hint #1



Results

ProblemsAcceptedWrongTimed OutError
150000

Input:

Expected Output:

Output: