Find the Index of the Large Integer - Problem

Imagine you're a detective trying to find the one special number in a hidden array! ๐Ÿ•ต๏ธโ€โ™‚๏ธ

You have access to an integer array arr where all numbers are identical except for one integer that's larger than the rest. However, there's a catch - you can't directly access the array elements!

Instead, you have a special ArrayReader API with two powerful functions:

  • compareSub(l, r, x, y): Compares the sum of subarray arr[l..r] with subarray arr[x..y]
    Returns: 1 if left sum > right sum, 0 if equal, -1 if left sum < right sum
  • length(): Returns the array size

Challenge: Find the index of the largest integer using at most 20 API calls! โšก

Goal: Return the index where the special (largest) number is hiding.

Input & Output

example_1.py โ€” Basic Case
$ Input: arr = [5, 5, 8, 5, 5] (hidden array)
โ€บ Output: 2
๐Ÿ’ก Note: The array has all 5s except index 2 which has 8. The algorithm compares subarrays to locate this larger element efficiently.
example_2.py โ€” Edge Position
$ Input: arr = [7, 3, 3, 3] (hidden array)
โ€บ Output: 0
๐Ÿ’ก Note: The larger element is at the beginning. Binary search will compare left half [7,3] with right half [3,3], find left half has larger sum, then narrow down to index 0.
example_3.py โ€” Single Element
$ Input: arr = [10] (hidden array)
โ€บ Output: 0
๐Ÿ’ก Note: With only one element, it must be the largest (and only) element, so return index 0.

Visualization

Tap to expand
Binary Search: The Smart Treasure HuntHeavier!Contains gold coinLighterEliminate this groupStep 1: DivideSplit coins intotwo groupsStep 2: CompareWeigh both groupson the scaleStep 3: EliminateDiscard thelighter groupStep 4: RepeatContinue untilone coin leftEfficiency Analysisโฑ๏ธ Time Complexity: O(log n)๐Ÿ“Š API Calls: At most logโ‚‚(n) โ‰ค 20๐Ÿ’พ Space Complexity: O(1)๐ŸŽฏ Success Rate: 100%โšก Each step eliminates half the search space
Understanding the Visualization
1
Divide the Coins
Split all coins into two equal groups and place them on opposite sides of the balance scale
2
Compare Groups
The scale tips toward the side with the gold coin, revealing which group contains our target
3
Eliminate & Focus
Discard the lighter group and focus only on the heavier group that contains the gold coin
4
Repeat Process
Continue dividing the remaining group until only one coin remains - that's our gold coin!
Key Takeaway
๐ŸŽฏ Key Insight: By comparing group sums instead of individual elements, we can eliminate half the search space with each comparison, guaranteeing we find the target within log(n) steps - well within our 20 API call limit!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

In worst case, we need to check each element once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 105
  • All integers in arr are equal except for one integer which is larger
  • You can call compareSub() at most 20 times
  • 0 โ‰ค l, r, x, y < arr.length where l โ‰ค r and x โ‰ค y
  • ArrayReader functions work in O(1) time
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen