KatInterview

748. Largest Number At Least Twice of Others

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to determine if the largest element in an integer array nums is at least twice as large as every other element in the array. If this condition is met, we should return the index of the largest element; otherwise, we return -1. We are given a few guarantees: the array has at least two elements, and the largest element is unique.

Solution

Largest Number At Least Twice of Others in Python

class Solution:
    def dominantIndex(self, nums: List[int]) -> int:
        max_val = -1
        second_max_val = -1
        max_index = -1

        for i, num in enumerate(nums):
            if num > max_val:
                # Found a new largest number.
                # The old max_val becomes the new second_max_val.
                second_max_val = max_val
                # The current number is the new max_val.
                max_val = num
                max_index = i
            elif num > second_max_val:
                # Found a new second largest number.
                second_max_val = num

        # Check if the largest element is at least twice as much as the second largest.
        # If this holds, it holds for all other elements as well.
        if second_max_val * 2 <= max_val:
            return max_index
        else:
            return -1

Largest Number At Least Twice of Others in Java

class Solution {
    public int dominantIndex(int[] nums) {
        int maxVal = -1;
        int secondMaxVal = -1;
        int maxIndex = -1;

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > maxVal) {
                // Found a new largest number.
                // The old maxVal becomes the new secondMaxVal.
                secondMaxVal = maxVal;
                // The current number is the new maxVal.
                maxVal = nums[i];
                maxIndex = i;
            } else if (nums[i] > secondMaxVal) {
                // Found a new second largest number.
                secondMaxVal = nums[i];
            }
        }

        // Check if the largest element is at least twice as much as the second largest.
        // If this holds, it holds for all other elements as well.
        if (secondMaxVal * 2 <= maxVal) {
            return maxIndex;
        } else {
            return -1;
        }
    }
}

Largest Number At Least Twice of Others in C++

class Solution {
public:
    int dominantIndex(vector<int>& nums) {
        int maxVal = -1;
        int secondMaxVal = -1;
        int maxIndex = -1;

        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] > maxVal) {
                // Found a new largest number.
                // The old maxVal becomes the new secondMaxVal.
                secondMaxVal = maxVal;
                // The current number is the new maxVal.
                maxVal = nums[i];
                maxIndex = i;
            } else if (nums[i] > secondMaxVal) {
                // Found a new second largest number.
                secondMaxVal = nums[i];
            }
        }
        
        // Check if the largest element is at least twice as much as the second largest.
        // If this holds, it holds for all other elements as well.
        if (secondMaxVal * 2 <= maxVal) {
            return maxIndex;
        }
        
        return -1;
    }
};

Largest Number At Least Twice of Others in TypeScript

function dominantIndex(nums: number[]): number {
    let maxVal: number = -1;
    let secondMaxVal: number = -1;
    let maxIndex: number = -1;

    for (let i = 0; i < nums.length; i++) {
        const num = nums[i];
        if (num > maxVal) {
            // Found a new largest number.
            // The old maxVal becomes the new secondMaxVal.
            secondMaxVal = maxVal;
            // The current number is the new maxVal.
            maxVal = num;
            maxIndex = i;
        } else if (num > secondMaxVal) {
            // Found a new second largest number.
            secondMaxVal = num;
        }
    }

    // Check if the largest element is at least twice as much as the second largest.
    // If this holds, it holds for all other elements as well.
    if (secondMaxVal * 2 <= maxVal) {
        return maxIndex;
    } else {
        return -1;
    }
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.