747. Largest Number At Least Twice of Others LeetCode Solution

In this guide, you will get 747. Largest Number At Least Twice of Others LeetCode Solution with the best time and space complexity. The solution to Largest Number At Least Twice of Others problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Largest Number At Least Twice of Others solution in C++
  4. Largest Number At Least Twice of Others solution in Java
  5. Largest Number At Least Twice of Others solution in Python
  6. Additional Resources
747. Largest Number At Least Twice of Others LeetCode Solution image

Problem Statement of 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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)
See also  590. N-ary Tree Postorder Traversal LeetCode Solution

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

class Solution {
 public:
  int dominantIndex(vector<int>& nums) {
    int ans;
    int mx = 0;
    int secondMax = 0;

    for (int i = 0; i < nums.size(); ++i)
      if (nums[i] > mx) {
        secondMax = mx;
        mx = nums[i];
        ans = i;
      } else if (nums[i] > secondMax) {
        secondMax = nums[i];
      }

    return mx >= 2 * secondMax ? ans : -1;
  }
};
/* code provided by PROGIEZ */

747. Largest Number At Least Twice of Others LeetCode Solution in Java

class Solution {
  public int dominantIndex(int[] nums) {
    int ans = 0;
    int mx = 0;
    int secondMax = 0;

    for (int i = 0; i < nums.length; ++i)
      if (nums[i] > mx) {
        secondMax = mx;
        mx = nums[i];
        ans = i;
      } else if (nums[i] > secondMax) {
        secondMax = nums[i];
      }

    return mx >= 2 * secondMax ? ans : -1;
  }
}
// code provided by PROGIEZ

747. Largest Number At Least Twice of Others LeetCode Solution in Python

class Solution:
  def dominantIndex(self, nums: list[int]) -> int:
    mx = 0
    secondMax = 0

    for i, num in enumerate(nums):
      if num > mx:
        secondMax = mx
        mx = num
        ans = i
      elif num > secondMax:
        secondMax = num

    return ans if mx >= 2 * secondMax else -1
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.