Find the Index of the Large Integer Problem
Description
LeetCode Problem 1533.
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): where0 <= l, r, x, y < ArrayReader.length(),l <= r andx <= y. The function compares the sum of sub-arrayarr[l..r]with the sum of the sub-arrayarr[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].
- 1 if
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 * 10^51 <= arr[i] <= 100- All elements of
arrare equal except for one element which is larger than all other elements.
Follow up:
- What if there are two numbers in
arrthat 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
Approach: Ternary Search with Equal-Sized Blocks
Comparing two ranges only tells us something when they have the same length: then their sums are equal exactly when neither range holds the large integer, and otherwise the larger sum points to it. That is the whole trick.
Keep a range [lo, hi] known to contain the large integer, starting with the whole array. Split it into three parts: two blocks of the same size, [lo, mid] and [mid + 1, end], each with third + 1 elements where third = (hi - lo) / 3, and the leftover (end, hi].
compareSub(lo, mid, mid + 1, end) == 1: the large integer is in the first block, sohi = mid.== -1: it is in the second block, solo = mid + 1, hi = end.== 0: neither block has it, so it is in the leftover part:lo = end + 1.
Each call keeps only about a third of the range, so an array of 5 * 105 elements needs about 12 calls, well under the limit of 20. A plain binary search (compare the two halves, and handle the odd middle element separately) also fits, with about 19 calls. When only two elements are left, third = 0 and the call simply compares them one against one.
Complexity: O(log n) calls to compareSub and O(1) extra space.
# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
# class ArrayReader(object):
# # Compares the sum of arr[l..r] with the sum of arr[x..y]
# # return 1 if sum(arr[l..r]) > sum(arr[x..y])
# # return 0 if sum(arr[l..r]) == sum(arr[x..y])
# # return -1 if sum(arr[l..r]) < sum(arr[x..y])
# def compareSub(self, l: int, r: int, x: int, y: int) -> int:
#
# # Returns the length of the array
# def length(self) -> int:
#
class Solution:
def getIndex(self, reader: 'ArrayReader') -> int:
lo, hi = 0, reader.length() - 1 # the large integer is in arr[lo..hi]
while lo < hi:
third = (hi - lo) // 3
# Compare two equal-sized blocks [lo, mid] and [mid + 1, end]; (end, hi] is left out.
mid = lo + third
end = mid + third + 1
cmp = reader.compareSub(lo, mid, mid + 1, end)
if cmp == 0:
lo = end + 1
elif cmp == 1:
hi = mid
else:
lo, hi = mid + 1, end
return lo