Question
Dominant Element Index
You are given an integer array nums. The largest value in the array is guaranteed to be unique.
Your task is to determine whether the largest element is at least twice as large as every other element in the array.
  • If the condition is satisfied, print the index (0-based) of the largest element.
  • Otherwise, print -1.
Input
A single line containing space-separated integers representing the array nums
Output
Print the index of the dominant element, or -1 if the condition fails
Example
Example 1:
Input:
3 6 1 0
Output:
1
Explanation:
Largest number = 6 (index 1)

Check condition:
6 ≥ 2×3 ✔
6 ≥ 2×1 ✔
6 ≥ 2×0 ✔
The condition holds for all elements → answer is index 1.

Example 2:
Input:
1 2 3 4
Output:
-1
Explanation:
Largest number = 4
But 4 is not ≥ 2×3 (6) → condition fails.

Online