Largest Number At Least Twice of Others - Problem
You are given an integer array nums where the largest integer is unique. Your task is to determine whether the largest element in the array is at least twice as much as every other number in the array.
Goal: If the largest element is at least twice as large as every other element, return the index of the largest element. Otherwise, return -1.
Example: In array [3, 6, 1, 0], the largest element is 6 at index 1. Since 6 >= 2×3, 6 >= 2×1, and 6 >= 2×0, we return 1.
Important: The problem guarantees that the largest element appears exactly once in the array.
Input & Output
example_1.py — Basic Dominant Case
$
Input:
[3, 6, 1, 0]
›
Output:
1
💡 Note:
The largest element is 6 at index 1. Since 6 ≥ 2×3, 6 ≥ 2×1, and 6 ≥ 2×0, the largest element is at least twice as large as every other element.
example_2.py — Non-Dominant Case
$
Input:
[1, 2, 3, 4]
›
Output:
-1
💡 Note:
The largest element is 4 at index 3. However, 4 < 2×3, so the largest element is not at least twice as large as every other element.
example_3.py — Single Element Edge Case
$
Input:
[1]
›
Output:
0
💡 Note:
With only one element, it is automatically the largest and dominant since there are no other elements to compare against.
Constraints
- 1 ≤ nums.length ≤ 50
- 0 ≤ nums[i] ≤ 100
- The largest element in nums is unique
Visualization
Tap to expand
Understanding the Visualization
1
Identify Champion
Find the strongest player (largest element) in the competition
2
Find Runner-up
Identify the second strongest player (second largest element)
3
Verify Dominance
Check if champion is at least twice as strong as the runner-up
4
Conclusion
If yes, return champion's position; if no, return -1 (no dominant champion)
Key Takeaway
🎯 Key Insight: We only need to compare the largest element with the second-largest. If the champion beats the runner-up by 2x, they automatically beat everyone else by 2x!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code