53. Largest Number At Least Twice of Others

EasyArrayArray

You are given a 0-indexed array nums. The largest element is dominant if it is at least twice as large as every other element. Return the index of the dominant element, or -1 if none exists.

Assume the maximum element appears only once.

Input: An integer array nums of length n.

Output: Integer — index of the dominant element, or -1.

Examples

Example 1
Input: [3,6,1,0]
Output: 1
Explanation: max=6 at index 1. 6>=2*3, 6>=2*1, 6>=2*0. Dominant. Return 1.
Example 2
Input: [1,2,3,4]
Output: -1
Explanation: max=4 at index 3. 4<2*3=6. Not dominant. Return -1.
Example 3
Input: [1]
Output: 0
Explanation: Single element — trivially dominant. Return 0.

Constraints

Asked by

GoogleBloombergMicrosoft
Solve this problem in the editor →