Largest Number At Least Twice of Others (LeetCode 747): Solution and Explanation
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
- Set first = second = -1 and idx = -1. All values are at least 0, so -1 is below everything.
- For each x at index i: if x > first, move first into second, then set first = x and idx = i.
- Otherwise, if x > second, set second = x.
- Return idx if first >= 2 * second, else -1.
Step-by-step example
nums = [3,6,1,0]
- i = 0, x = 3: new leader. first = 3, idx = 0, second = -1.
- i = 1, x = 6: beats 3. second = 3, first = 6, idx = 1.
- i = 2, x = 1: not above 6 and not above 3. Nothing changes.
- i = 3, x = 0: nothing changes.
- 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 -11class Solution {
2 public int dominantIndex(int[] nums) {
3 int first = -1, second = -1, idx = -1;
4 for (int i = 0; i < nums.length; i++) {
5 if (nums[i] > first) {
6 second = first;
7 first = nums[i];
8 idx = i;
9 } else if (nums[i] > second) {
10 second = nums[i];
11 }
12 }
13 return first >= 2 * second ? idx : -1;
14 }
15}1class Solution {
2public:
3 int dominantIndex(vector<int>& nums) {
4 int first = -1, second = -1, idx = -1;
5 for (int i = 0; i < (int)nums.size(); i++) {
6 if (nums[i] > first) {
7 second = first;
8 first = nums[i];
9 idx = i;
10 } else if (nums[i] > second) {
11 second = nums[i];
12 }
13 }
14 return first >= 2 * second ? idx : -1;
15 }
16};Time and space complexity
O(n)
Each element is compared at most twice during the single pass.
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.