Statistics

Problem Statement for "NonbinarySearch"

Problem Statement

Consider the very traditional problem: Given is an array of N elements. This array is stored in memory and it is guaranteed to be sorted in strictly ascending order. You are given an element X and you are asked to determine whether it appears in the given array.

A worst-case optimal algorithm for this problem is the binary search algorithm.

This algorithm works as follows: Initially, all N elements are candidates for where the value X may be. As long as you have some candidates, you repeat the following:

  1. Pick the middle candidate. (If the current number of candidates is even, you can pick either of the middle two candidates.)
  2. Examine that candidate. If it's equal to X, we found it and the search terminates.
  3. If the examined candidate is greater than X, we know that all following elements in the array are also greater than X. (Remember that the array is sorted.) Thus, we eliminate the examined candidate and we also eliminate all candidates to its right.
  4. Conversely, if the examined candidate is smaller than X, we know that all preceding elements in the array are also smaller than X. Thus, we eliminate the examined candidate and we also eliminate all candidates to its left.

A nonbinary search is a search that only differs from the binary search in step 1 of the algorithm. The new step 1 looks as follows:

  1. If you can pick a candidate that would not be picked by binary search, pick such a candidate. Only if that is impossible (i.e., when there are at most two candidates), pick a candidate binary search could also pick.

Clearly, there can be many implementations of nonbinary search that differ in the strategy used to pick the candidate in step 1. (In particular, linear search - the search that examines all candidates from the left to the right - is one possible nonbinary search.) Which one of those strategies is the best one, and how many questions will it need to solve an array of size N?


More precisely, for the given N determine and return the smallest possible number Q with the following property: "There is an implementation of nonbinary search that will compute the correct answer for any array of length N and any X, and for each such input it will only examine at most Q elements."

Definition

Class:
NonbinarySearch
Method:
search
Parameters:
int
Returns:
int
Method signature:
int search(int N)
(be sure your method is public)

Constraints

  • N will be between 1 and 10^6, inclusive.

Examples

  1. 1

    Returns: 1

    Just like with traditional binary search, we have to examine the element to check whether its value is X or not.

  2. 3

    Returns: 3

    Here the nonbinary search already becomes worse than binary search. Let's call the three elements of the array A, B, C. We know that A < B < C. Binary search would start by examining B. If B = X, the search is done, if B is too large, we check A, and if B is too small, we check C next. In either case the binary search would examine at most two elements. As binary search starts by looking at B and we have other options, we must choose one of those. Without loss of generality, we start by examining A. If A turns out to be too small, we still have two candidates: both B and C can still be equal to X. There is no way to finish the search without examining them both, so in the worst case we will look at all three elements.

  3. 15

    Returns: 6

    Binary search would be done in four questions, but the best we can do with a nonbinary search is six. With 15 elements, binary search would start by looking at the middle element - the 8th smallest and at the same time the 8th largest one. One of several possible optimal ways to start a nonbinary search is by looking at the sixth smallest element. We will get one of three possible outcomes: We find the X we are looking for. We'll see a value greater than X. This will leave us with 5 candidates: the five elements smaller than the one we examined. We'll see a value smaller than X. This will leave us with 9 candidates. Suppose we are left with 9 candidates. The situation repeats itself in the second round: binary search would examine the middle one of those nine elements, so our nonbinary search must examine one of the other eight. And so on.

  4. 1000000

    Returns: 22

    The largest valid input. The best nonbinary search can always give a correct answer for 1,000,000 elements by examining at most 22 of them. (That's only a little worse than binary search, and it is much better than linear search.)

  5. 2

    Returns: 2

  6. 4

    Returns: 4

  7. 5

    Returns: 4

  8. 6

    Returns: 5

  9. 7

    Returns: 5

  10. 8

    Returns: 5

  11. 9

    Returns: 5

  12. 10

    Returns: 6

  13. 11

    Returns: 6

  14. 12

    Returns: 6

  15. 13

    Returns: 6

  16. 14

    Returns: 6

  17. 15

    Returns: 6

  18. 16

    Returns: 6

  19. 17

    Returns: 6

  20. 18

    Returns: 7

  21. 19

    Returns: 7

  22. 20

    Returns: 7

  23. 25

    Returns: 7

  24. 33

    Returns: 7

  25. 34

    Returns: 8

  26. 50

    Returns: 8

  27. 65

    Returns: 8

  28. 66

    Returns: 9

  29. 99

    Returns: 9

  30. 129

    Returns: 9

  31. 130

    Returns: 10

  32. 136

    Returns: 10

  33. 257

    Returns: 10

  34. 258

    Returns: 11

  35. 352

    Returns: 11

  36. 513

    Returns: 11

  37. 514

    Returns: 12

  38. 769

    Returns: 12

  39. 1025

    Returns: 12

  40. 1026

    Returns: 13

  41. 1417

    Returns: 13

  42. 2049

    Returns: 13

  43. 2050

    Returns: 14

  44. 2781

    Returns: 14

  45. 4097

    Returns: 14

  46. 4098

    Returns: 15

  47. 7413

    Returns: 15

  48. 8193

    Returns: 15

  49. 8194

    Returns: 16

  50. 15759

    Returns: 16

  51. 16385

    Returns: 16

  52. 16386

    Returns: 17

  53. 26388

    Returns: 17

  54. 32769

    Returns: 17

  55. 32770

    Returns: 18

  56. 56772

    Returns: 18

  57. 65537

    Returns: 18

  58. 65538

    Returns: 19

  59. 105540

    Returns: 19

  60. 131073

    Returns: 19

  61. 131074

    Returns: 20

  62. 231076

    Returns: 20

  63. 262145

    Returns: 20

  64. 262146

    Returns: 21

  65. 382148

    Returns: 21

  66. 392150

    Returns: 21

  67. 524289

    Returns: 21

  68. 524290

    Returns: 22

  69. 724292

    Returns: 22

  70. 824294

    Returns: 22

  71. 924296

    Returns: 22

  72. 999999

    Returns: 22

  73. 100000

    Returns: 19


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: