Largest Number At Least Twice of Others (LeetCode 747): Solution and Explanation

Easy
Array
Sorting

Scan once keeping the largest value, its index and the runner-up. The largest dominates everyone exactly when it is at least twice the runner-up.

Intuition

Checking the maximum against every other element works, but most of those checks are wasted. If the maximum is at least twice the biggest of the others, it is automatically at least twice all the smaller ones.

So the only number that matters besides the maximum is the second largest. That turns the problem into finding the top two values.

Finding the top two is a single pass: a new leader pushes the old leader into second place, and a value between them only replaces the runner-up.

Approach: Track first and second

  1. Set first = second = -1 and idx = -1. All values are at least 0, so -1 is below everything.
  2. For each x at index i: if x > first, move first into second, then set first = x and idx = i.
  3. Otherwise, if x > second, set second = x.
  4. Return idx if first >= 2 * second, else -1.

Step-by-step example

nums = [3,6,1,0]
  1. i = 0, x = 3: new leader. first = 3, idx = 0, second = -1.
  2. i = 1, x = 6: beats 3. second = 3, first = 6, idx = 1.
  3. i = 2, x = 1: not above 6 and not above 3. Nothing changes.
  4. i = 3, x = 0: nothing changes.
  5. 6 >= 2 * 3, so return idx = 1.

Code: Python, Java, C++

1class Solution:
2    def dominantIndex(self, nums: List[int]) -> int:
3        first = second = -1
4        idx = -1
5        for i, x in enumerate(nums):
6            if x > first:
7                second = first
8                first, idx = x, i
9            elif x > second:
10                second = x
11        return idx if first >= 2 * second else -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Track first and second O(n)
Find the max, then check everyone O(n)
Sort a copy O(n log n)
Time

O(n)

Each element is compared at most twice during the single pass.

Space

O(1)

Only three integers are stored.

Other approaches

Find the max, then check everyone (time O(n), space O(1))

First pass finds the maximum and its index, second pass checks max >= 2 * x for every other x. Same complexity, two passes.

Sort a copy (time O(n log n), space O(n))

Sort a copy of the array and compare the last two values, then look up the original index of the maximum. Simple but slower.

Common mistakes

  • Forgetting to move the old leader into second place when a new maximum appears. Without that, [3,6] would report second = -1.
  • Comparing the maximum with itself. Skip its own index, or track the runner-up separately as above.
  • Returning the value instead of its index.

Frequently asked questions

Why is comparing with the second largest enough?

Every other element is at most the second largest. If max >= 2 * second, then max >= 2 * x for any x <= second as well.

What if the runner-up is 0?

Then 2 * second is 0 and any maximum passes, so the answer is the index of the maximum. For example [0,0,3,0] returns 2.

Does the uniqueness of the largest value matter?

It guarantees one index to return. With a duplicate maximum the check would fail anyway, since max < 2 * max for any positive max.