Largest Number At Least Twice of Others - Problem
You are given an integer array nums where the largest integer is unique. Determine whether the largest element in the array is at least twice as much as every other number in the array.
If it is, return the index of the largest element, or return -1 otherwise.
Input & Output
Example 1 — Valid Case
$
Input:
nums = [3,6,1,0]
›
Output:
1
💡 Note:
The largest element is 6 at index 1. It's at least twice every other element: 6 ≥ 2×3, 6 ≥ 2×1, 6 ≥ 2×0. So return index 1.
Example 2 — Invalid Case
$
Input:
nums = [1,2,3,4]
›
Output:
-1
💡 Note:
The largest element is 4 at index 3. However, 4 < 2×3 = 6, so it's not at least twice the element 3. Return -1.
Example 3 — Edge Case
$
Input:
nums = [1]
›
Output:
0
💡 Note:
Single element array - the largest (and only) element is trivially at least twice every other element (there are none). Return index 0.
Constraints
- 1 ≤ nums.length ≤ 50
- 0 ≤ nums[i] ≤ 100
- The largest element in nums is unique
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [3,6,1,0] with unique largest element
2
Process
Check if max element ≥ 2 × every other element
3
Output
Return index of max if valid, -1 otherwise
Key Takeaway
🎯 Key Insight: Only need to compare largest with second largest - if max ≥ 2×second_max, then max ≥ 2×everything else
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code