We have an integer array arr, where all the integers in arr are equal except for one integer which is larger than the rest of the integers. You will not be given direct access to the array, instead, you will have an API ArrayReader which have the following functions:
int compareSub(int l, int r, int x, int y): where 0 <= l, r, x, y < ArrayReader.length(), l <= r and x <= y. The function compares the sum of sub-array arr[l..r] with the sum of the sub-array arr[x..y] and returns:
arr[l]+arr[l+1]+...+arr[r] > arr[x]+arr[x+1]+...+arr[y].arr[l]+arr[l+1]+...+arr[r] == arr[x]+arr[x+1]+...+arr[y].arr[l]+arr[l+1]+...+arr[r] < arr[x]+arr[x+1]+...+arr[y].int length(): Returns the size of the array.You are allowed to call compareSub() 20 times at most. You can assume both functions work in O(1) time.
Return the index of the array arr which has the largest integer.
Example 1:
Input: arr = [7,7,7,7,10,7,7,7] Output: 4 Explanation: The following calls to the API reader.compareSub(0, 0, 1, 1) // returns 0 this is a query comparing the sub-array (0, 0) with the sub array (1, 1), (i.e. compares arr[0] with arr[1]). Thus we know that arr[0] and arr[1] doesn't contain the largest element. reader.compareSub(2, 2, 3, 3) // returns 0, we can exclude arr[2] and arr[3]. reader.compareSub(4, 4, 5, 5) // returns 1, thus for sure arr[4] is the largest element in the array. Notice that we made only 3 calls, so the answer is valid.
Example 2:
Input: nums = [6,6,12] Output: 2
Constraints:
2 <= arr.length <= 5 * 1051 <= arr[i] <= 100arr are equal except for one element which is larger than all other elements.
Follow up:
arr that are bigger than all other numbers?Problem Overview: You are given an array where every value is equal except one element that is strictly larger. Direct access to the array is not allowed. Instead, an ArrayReader API compares the sum of two subarrays. The task is to locate the index of the larger element using as few comparisons as possible.
Approach 1: Linear Comparison with compareSub (O(n) time, O(1) space)
The simplest strategy repeatedly compares single elements using the compareSub API. Start with index 0 as the current candidate. Compare [candidate, candidate] with [i, i] for every i from 1 to n-1. If the result shows the right side is larger, update the candidate. This effectively simulates a linear scan over the array using the provided comparison interface. Time complexity is O(n) API calls with O(1) extra space. It works but wastes comparisons and is rarely expected in interviews.
Approach 2: Binary Search with Subarray Comparison (O(log n) time, O(1) space)
The optimal solution leverages the fact that all elements except one are identical. Split the array into two equal halves and compare their sums using compareSub(l, mid, mid+1, r). If the sums are equal, the larger element must be outside those ranges (which only happens when the length is odd and the middle element is excluded). If the left half is larger, the target index lies in that half; otherwise it lies in the right half. This turns the search into a classic binary search style narrowing process.
When the subarray length is odd, exclude the middle element during the comparison so both halves have equal length. If the sums match, the excluded middle index is the answer. Otherwise continue searching in the heavier half. Each step cuts the search space roughly in half while making a constant number of calls to the compareSub API.
This approach relies on reasoning about sums rather than direct element access, a common pattern in interactive problems. Because each comparison eliminates half the candidates, the total number of queries is O(log n) with constant auxiliary memory.
Recommended for interviews: The binary search strategy is the expected solution. A linear scan shows you understand how to use the API, but interviewers look for the observation that equal values allow sum comparisons to reveal which half contains the larger element. Reducing the search space with compareSub demonstrates algorithmic reasoning and efficient query usage.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Linear Comparison using compareSub | O(n) | O(1) | Simple baseline when optimizing query count is not required |
| Binary Search with Subarray Sum Comparison | O(log n) | O(1) | Optimal approach for interactive constraints and minimal API calls |
1533. Find the Index of the Large Integer - Week 3/5 Leetcode January Challenge • Programming Live with Larry • 355 views views
Watch 2 more video solutions →Practice Find the Index of the Large Integer with our built-in code editor and test cases.
Practice on FleetCode