> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find the Index of the Large Integer

> Tested Python solution for LeetCode 1533 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 1533, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Interactive](/catalog/topics/interactive). [View on LeetCode](https://leetcode.com/problems/find-the-index-of-the-large-integer/description/).

Generate this problem as a practice environment: tested reference solution, 21 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1533   # by problem number
lcpy gen -s find_the_index_of_the_large_integer   # by problem name
```

## 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

```
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.
```

```
Input: arr = [6,6,12]
Output: 2
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_index_of_the_large_integer/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_index_of_the_large_integer/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class ArrayReader:
    # Test-harness API: backs the compareSub/length interface with the array
    def __init__(self, arr: list[int]) -> None:
        self.arr = arr

    def compare_sub(self, lo1: int, hi1: int, lo2: int, hi2: int) -> int:
        left = sum(self.arr[lo1 : hi1 + 1])
        right = sum(self.arr[lo2 : hi2 + 1])
        return (left > right) - (left < right)

    def length(self) -> int:
        return len(self.arr)


class Solution:
    # Time: O(log n) compare_sub calls
    # Space: O(1)
    def get_index(self, reader: ArrayReader) -> int:
        left, right = 0, reader.length() - 1
        while left < right:
            # Split into two equal-size leading blocks plus a remainder block;
            # equal sizes guarantee a 0 result rules out both leading blocks.
            t2 = left + (right - left) // 3
            t3 = left + ((right - left) // 3) * 2 + 1
            cmp = reader.compare_sub(left, t2, t2 + 1, t3)
            if cmp == 0:
                left = t3 + 1
            elif cmp == 1:
                right = t2
            else:
                left, right = t2 + 1, t3
        return left
```

## Complexity

| Time | Space |
| - | - |
| O(log n) compare\_sub calls | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.