Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1533: Find the Index of the Large Integer

In this guide, we solve Leetcode #1533 Find the Index of the Large Integer in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Array, Binary Search, Interactive

Intuition

The problem structure suggests a monotonic decision, which makes binary search a natural fit.

By halving the search space each step, we reach the answer efficiently.

Approach

Search either directly on a sorted array or on the answer space using a check function.

Each check is fast, and the logarithmic search keeps the overall runtime low.

Steps:

  • Define the search bounds.
  • Check the mid point condition.
  • Narrow the bounds until convergence.

Example

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.

Python Solution

# """ # 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: left, right = 0, reader.length() - 1 while left < right: t1, t2, t3 = ( left, left + (right - left) // 3, left + ((right - left) // 3) * 2 + 1, ) cmp = reader.compareSub(t1, t2, t2 + 1, t3) if cmp == 0: left = t3 + 1 elif cmp == 1: right = t2 else: left, right = t2 + 1, t3 return left

Complexity

The time complexity is O(log n) or O(n log n). The space complexity is O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy