Largest Number At Least Twice of Others LeetCode Solution

Problem – Largest Number At Least Twice of Others

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 <= 50
  • 0 <= nums[i] <= 100
  • The largest element in nums is unique.

Largest Number At Least Twice of Others LeetCode Solution in C++

int dominantIndex(vector<int>& nums) {
    int max1 = 0, max2 = 0, idxOfMax1 = 0;
    for (int i = 0; i < nums.size(); i++) {
        if (nums[i] > max1) {
            max2 = max1;
            max1 = nums[i];
            idxOfMax1 = i;
        } else if (nums[i] > max2) {
            max2 = nums[i];
        }
    }        
    return max1 >= max2 * 2 ? idxOfMax1 : -1;
}

Largest Number At Least Twice of Others LeetCode Solution in Java

public int dominantIndex(int[] nums) {
    int max1 = 0, max2 = 0, idxOfMax1 = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] > max1) {
            max2 = max1;
            max1 = nums[i];
            idxOfMax1 = i;
        } else if (nums[i] > max2) {
            max2 = nums[i];
        }
    }        
    return max1 >= max2 * 2 ? idxOfMax1 : -1;
}

Largest Number At Least Twice of Others LeetCode Solution in Python

def dominantIndex(self, nums):        
    max1 = 0
    max2 = 0
    idxOfMax1 = 0

    for i, n in enumerate(nums):
        if n >= max1:
            max2 = max1
            max1 = n
            idxOfMax1 = i
        elif n > max2:
            max2 = n                

    return idxOfMax1 if max1 >= max2 * 2 else -1
Largest Number At Least Twice of Others LeetCode Solution Review:

In our experience, we suggest you solve this Largest Number At Least Twice of Others LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Largest Number At Least Twice of Others LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Largest Number At Least Twice of Others LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.