219k views
2 votes
747. Largest Number At Least Twice of Others (Easy)

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.

Input: nums = [3,6,1,0]
Output: 1

1 Answer

6 votes

Final answer:

To determine if the largest element in the array is at least twice as much as every other number, we need to find the largest element and compare it to every other element in the array.

Step-by-step explanation:

To determine if the largest element in the array is at least twice as much as every other number, we need to compare the largest element to every other element in the array.

We can first find the largest element in the array using a loop, and then compare it to every other element using another loop.

If the largest element is at least twice as much as every other number, we return the index of the largest element. Otherwise, we return -1.

User Holt Skinner
by
8.0k points