540. Find the Index of the Large Integer
Given 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.
There is no direct access to the array. Instead there is an an API ArrayReader with the following functions:
- int compareSub(int l, int r, int x, int y): Compares the sum of the sub-array arr[l..r] with the sum of the sub-array arr[x..y] and returns: 1 if sum(arr[l..r]) > sum(arr[x..y]), 0 if sum(arr[l..r]) == sum(arr[x..y]) and -1 if sum(arr[l..r]) < sum(arr[x..y]).
- int length(): Returns the length of the array.
The call to compareSub() can be made at most 20 times. Both functions work in O(1) time.
The task is to return the index of the array arr which has the largest integer.
Example 1:
Input: arr = [7, 7, 7, 10, 7, 7, 7]
Output: 3
Explanation:
The array has 7 elements, with the largest integer 10 at index 3. By dividing the array and using the compareSub function, we can determine which half contains the larger integer and recursively narrow down to find its exact index.
Example 2:
Input: arr = [5, 5, 5, 5, 5, 8]
Output: 5
Explanation: The array has 6 elements, with the largest integer 8 at index 5. Using the compareSub function, we can compare sub-arrays to identify the half containing the larger integer and continue this process to pinpoint its location.
Now Your Turn!
Pick the correct output for the given inputInput: arr = [1, 9, 1, 1, 1, 1, 1, 1]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 <= arr.length <= 5 * 105
- 1 <= arr[i] <= 100
- All elements of arr are equal except for one element which is larger than all other elements.