NeetCode #269LC-1533MediumBinary SearchNC Algo100
← Back to All Problems#269 · #1533 · Find the Index of the Large Integer(找出数组中的大整数下标)
📌 Problem Statement & Constraints
This is an interactive problem. An array of
n integers has one element strictly larger than all others. ArrayReader.compareSub(l1, r1, l2, r2) compares the sums of two equal-length subarrays, returning 1, 0 or -1. Return the index of the largest element. Constraints: 2 <= n <= 5 * 10^4; at most 20 calls to compareSub.💡 Core Algorithmic Approaches
- The array length doubles or halves each step, so the search space is halved by comparing two equal halves.
- If the length is even, split into two equal halves and compare them: the larger sum's half contains the maximum.
- If the length is odd, leave the middle element out and compare the two remaining halves. A tie means the middle element is the answer; otherwise the winning half contains it.
- Each step halves the range, so at most
log2(50000) ≈ 16calls are needed.
💻 Benchmark Python3 Implementation
class Solution:
def getIndex(self, reader: "ArrayReader") -> int:
n = reader.length()
lo, hi = 0, n - 1
while lo < hi:
length = hi - lo + 1
mid = (lo + hi) // 2
if length % 2 == 0:
# two equal halves: [lo, mid] and [mid+1, hi]
c = reader.compareSub(lo, mid, mid + 1, hi)
if c == 1:
hi = mid
else:
lo = mid + 1
else:
# leave the middle element out
c = reader.compareSub(lo, mid - 1, mid + 1, hi)
if c == 0:
return mid # the middle is the largest
if c == 1:
hi = mid - 1
else:
lo = mid + 1
return lo⚡ Complexity Deep Dive
⏱️ Time Complexity
O(log n) calls to compareSub: about 16 for the maximum n.
💾 Space Complexity
O(1): a few indices.
⚠️ Interview Pitfalls & Follow-ups
- Not excluding the middle element in the odd-length case: including it in one half biases the comparison and can hide the answer.
- Treating a tie in the odd case as "the left half wins": a tie means the two halves are equal, so the excluded middle element must be the maximum.
- Comparing unequal-length subarrays: the API requires equal lengths.
- Binary searching by value: the array is not sorted, so only sum comparisons are available.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Binary Search Problems
中文速览与面试要点
找出数组中的大整数下标
(这是一个 交互式问题 )给你一个由 n 个整数组成的数组 nums,其中 恰好有一个 元素比其余所有元素都大,其余元素全部相等。你可以通过 ArrayReader 接口访问数组: compareSub(l, r, x, y) 比较 nums[l..r] 与 nums[x..y] 的和,返回 −1/0/1; length() 返回数组长度; getIndex(i) 返回 nums[i] (只能调用一次)。请找出这个大整数的下标。约束:1 ≤ n ≤ 10^5;1 ≤ nums[i] ≤ 10^9;恰好一个元素严格大于其余元素;最多调用 20 次 compareSub。
核心要点
- 二分 + 比较两半的和(标准解) :每次把当前区间对半分,比较左右两半的元素之和。
- 若两半长度相等 :直接比较 compareSub(l, mid, mid+1, r) 。返回 1 说明大元素在左半( r = mid ),否则在右半( l = mid + 1 )。
- 若左半比右半多一个元素 (当区间长度为奇数时):比较 [l, mid−1] 与 [mid+1, r] (两者等长)。若返回 0,说明两半完全相等,大元素就是 nums[mid] ,直接返回 mid;返回 1 则 r = mid − 1 ;返回 −1 则 l = mid + 1 。
- 为什么长度为奇数时左半更长 : mid = (l + r) // 2 向下取整,因此左半长度 ≥ 右半长度,差值为 0 或 1。
常见陷阱
- 忘记处理「左半比右半多一个」的情况 :直接用 compareSub(l, mid, mid+1, r) 会因两段长度不等而得到无意义的结果(大元素所在的那一半自然和更大,无法区分)。
- 奇数长度时比较 [l, mid] 与 [mid+1, r] :长度不等,必须比较 [l, mid−1] 与 [mid+1, r] 。
- c == 0 时继续二分而不返回 :返回 0 意味着两个等长区间完全相同,因此大元素只能是中间被排除的那个 nums[mid] ,必须立即返回。
- 调用 getIndex 超过一次 :接口限制只能调用一次。
参考实现
class Solution:
def getIndex(self, reader: 'ArrayReader') -> int:
n = reader.length()
l, r = 0, n - 1
while l < r:
mid = (l + r) // 2
left_len = mid - l + 1
right_len = r - mid
if left_len == right_len:
# 两半等长,直接比较
c = reader.compareSub(l, mid, mid + 1, r)
if c == 1:
r = mid # 大元素在左半
else:
l = mid + 1 # 在右半
else:
# 左半比右半多一个:比较 [l, mid-1] 与 [mid+1, r]
c = reader.compareSub(l, mid - 1, mid + 1, r)
if c == 0:
return mid # 两半相等 -> 大元素就是 nums[mid]
elif c == 1:
r = mid - 1
else:
l = mid + 1
return l