Algorithm


Problem Name: 747. 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.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int dominantIndex(vector<int>& nums) {
        int a = 0, b = 0, idx = -1;
        for(int i = 0; i  <  nums.size(); i++){
            if(nums[i] > a){
                b = a;
                a = nums[i];
                idx = i;
            }
            else if(nums[i] > b) b = nums[i];
        }
        return a >= 2 * b ? idx : -1;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [3,6,1,0]

Output

x
+
cmd
1

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
    public int dominantIndex(int[] nums) {
        int maxNum = Integer.MIN_VALUE;
        int secMaxNum = Integer.MIN_VALUE;
        int maxIdx = -1;
        
        for (int i = 0; i  <  nums.length; i++) {
            if (nums[i] >= maxNum) {
                secMaxNum = maxNum;
                maxNum = nums[i];
                maxIdx = i;
            }
            else if (nums[i] > secMaxNum) {
                secMaxNum = nums[i];
            }
        }
        
        return maxNum >= 2 * secMaxNum ? maxIdx : -1;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [3,6,1,0]

Output

x
+
cmd
1

#3 Code Example with Javascript Programming

Code - Javascript Programming


const dominantIndex = function(nums) {
  let max = -Infinity
  let idx = -1
  for(let i = 0, len = nums.length; i  <  len; i++) {
    if(nums[i] > max) {
      max = nums[i]
      idx = i
    }
  }
  for(let i = 0, len = nums.length; i < len; i++) {
    if(nums[i] !== max && max  <  nums[i] * 2> return -1
  }
  return idx
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,4

Output

x
+
cmd
-1

#4 Code Example with Python Programming

Code - Python Programming

class Solution:
    def countSubstrings(self, s):       
        res = 0
        for k in range(len(s)):
            i = j = k
            while 0 <= i and j < len(s):
                if s[i] == s[j]: res += 1
                else: break
                i , j = i - 1, j + 1
            i , j =k , k + 1
            while 0 <= i and j < len(s):
                if s[i] == s[j]: res += 1
                else: break
                i , j = i - 1, j + 1
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,4

Output

x
+
cmd
-1

#5 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0747_LargestNumberAtLeastTwiceOfOthers
    {
        public int DominantIndex(int[] nums)
        {
            int max1 = int.MinValue, max2 = int.MinValue;
            int index = -1;

            for (int i = 0; i  <  nums.Length; i++)
            {
                var num = nums[i];
                if (num > max2)
                    if (num >= max1)
                    {
                        max2 = max1;
                        max1 = num;
                        index = i;
                    }
                    else
                    {
                        max2 = num;
                    }
            }

            return max1 >= 2 * max2 ? index : -1;
        }
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [3,6,1,0]

Output

x
+
cmd
1
Advertisements

Demonstration


Previous
#746 Leetcode Min Cost Climbing Stairs Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#748 Leetcode Shortest Completing Word Solution in C, C++, Java, JavaScript, Python, C# Leetcode