LeetCode 1533, Medium. Topics: Array, Binary Search, Interactive. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 21 parametrized pytest cases, and a playground notebook:
Problem
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 has 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:
- 1 if
arr[l]+arr[l+1]+...+arr[r] > arr[x]+arr[x+1]+...+arr[y].
- 0 if
arr[l]+arr[l+1]+...+arr[r] == arr[x]+arr[x+1]+...+arr[y].
- -1 if
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.
Examples
Constraints
2 <= arr.length <= 5 * 10^5
1 <= arr[i] <= 100
- All elements of
arr are equal except for one element which is larger than all other elements.
Follow up:
- What if there are two numbers in
arr that are bigger than all other numbers?
- What if there is one number that is bigger than other numbers and one number that is smaller than other numbers?
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026