Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
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] <= 100nums is unique.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[3,6,1,0]
[1,2,3,4]
keep-multiplying-found-values-by-two)largest-number-after-digit-swaps-by-parity)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #747: Largest Number At Least Twice of Others
class Solution {
public int dominantIndex(int[] nums) {
int n = nums.length;
int k = 0;
for (int i = 0; i < n; ++i) {
if (nums[k] < nums[i]) {
k = i;
}
}
for (int i = 0; i < n; ++i) {
if (k != i && nums[k] < nums[i] * 2) {
return -1;
}
}
return k;
}
}
// Accepted solution for LeetCode #747: Largest Number At Least Twice of Others
func dominantIndex(nums []int) int {
k := 0
for i, x := range nums {
if nums[k] < x {
k = i
}
}
for i, x := range nums {
if k != i && nums[k] < x*2 {
return -1
}
}
return k
}
# Accepted solution for LeetCode #747: Largest Number At Least Twice of Others
class Solution:
def dominantIndex(self, nums: List[int]) -> int:
x, y = nlargest(2, nums)
return nums.index(x) if x >= 2 * y else -1
// Accepted solution for LeetCode #747: Largest Number At Least Twice of Others
struct Solution;
impl Solution {
fn dominant_index(nums: Vec<i32>) -> i32 {
let n = nums.len();
if n == 1 {
return 0;
}
let mut copy = nums.clone();
copy.sort_unstable();
if copy[n - 1] < 2 * copy[n - 2] {
return -1;
}
for i in 0..n {
if copy[n - 1] == nums[i] {
return i as i32;
}
}
-1
}
}
#[test]
fn test() {
let nums = vec![3, 6, 1, 0];
assert_eq!(Solution::dominant_index(nums), 1);
let nums = vec![1, 2, 3, 4];
assert_eq!(Solution::dominant_index(nums), -1);
}
// Accepted solution for LeetCode #747: Largest Number At Least Twice of Others
function dominantIndex(nums: number[]): number {
let k = 0;
for (let i = 0; i < nums.length; ++i) {
if (nums[i] > nums[k]) {
k = i;
}
}
for (let i = 0; i < nums.length; ++i) {
if (i !== k && nums[k] < nums[i] * 2) {
return -1;
}
}
return k;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.