Largest Number At Least Twice of Others
EasyUpdated: Aug 2, 2025
Practice on:
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 _theindex of the largest element, or return _-1 otherwise.
Examples
Example 1
Input: nums = [3,6,1,0]
Output: 1
Explanation: 6 is the largest integer.
For every other number in the array x, 6 is at least twice as big as x.
The index of value 6 is 1, so we return 1.
Example 2
Input: nums = [1,2,3,4]
Output: -1
Explanation: 4 is less than twice the value of 3, so we return -1.
Constraints
2 <= nums.length <= 500 <= nums[i] <= 100- The largest element in
numsis unique.
Solution
Method 1 – Find Max and Second Max
Intuition
To check if the largest number is at least twice every other, find the largest and second largest numbers. If the largest is at least twice the second largest, return its index; otherwise, return -1.
Approach
- Iterate through the array to find the largest and second largest values and the index of the largest.
- Check if the largest is at least twice the second largest.
- Return the index if true, else -1.
Code
C++
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = -1, smx = -1, idx = -1;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] > mx) { smx = mx; mx = nums[i]; idx = i; }
else if (nums[i] > smx) smx = nums[i];
}
return mx >= 2 * smx ? idx : -1;
}
};
Go
func dominantIndex(nums []int) int {
mx, smx, idx := -1, -1, -1
for i, v := range nums {
if v > mx { smx, mx, idx = mx, v, i } else if v > smx { smx = v }
}
if mx >= 2*smx { return idx }
return -1
}
Java
class Solution {
public int dominantIndex(int[] nums) {
int mx = -1, smx = -1, idx = -1;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] > mx) { smx = mx; mx = nums[i]; idx = i; }
else if (nums[i] > smx) smx = nums[i];
}
return mx >= 2 * smx ? idx : -1;
}
}
Kotlin
class Solution {
fun dominantIndex(nums: IntArray): Int {
var mx = -1
var smx = -1
var idx = -1
for (i in nums.indices) {
if (nums[i] > mx) { smx = mx; mx = nums[i]; idx = i }
else if (nums[i] > smx) smx = nums[i]
}
return if (mx >= 2 * smx) idx else -1
}
}
Python
class Solution:
def dominantIndex(self, nums: list[int]) -> int:
mx = smx = -1
idx = -1
for i, v in enumerate(nums):
if v > mx:
smx, mx, idx = mx, v, i
elif v > smx:
smx = v
return idx if mx >= 2 * smx else -1
Rust
impl Solution {
pub fn dominant_index(nums: Vec<i32>) -> i32 {
let (mut mx, mut smx, mut idx) = (-1, -1, -1);
for (i, &v) in nums.iter().enumerate() {
if v > mx { smx = mx; mx = v; idx = i as i32; }
else if v > smx { smx = v; }
}
if mx >= 2 * smx { idx } else { -1 }
}
}
TypeScript
class Solution {
dominantIndex(nums: number[]): number {
let mx = -1, smx = -1, idx = -1
for (let i = 0; i < nums.length; ++i) {
if (nums[i] > mx) { smx = mx; mx = nums[i]; idx = i }
else if (nums[i] > smx) smx = nums[i]
}
return mx >= 2 * smx ? idx : -1
}
}
Complexity
- ⏰ Time complexity:
O(n), where n is the length of nums. Each element is visited once. - 🧺 Space complexity:
O(1), only a few variables are used.