Problem 971
Largest Number At Least Twice of Others
Easy · 10 pts · Array, Sorting
Problem
The largest element of the array is unique. Decide whether it is at least twice as large as every other element. If it is, print its index (0-based); otherwise print -1. It is enough to compare the largest element with the second largest: if the largest is at least twice the second largest, it is at least twice every other element too. Find both with one pass.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1]. The largest element is unique.
Output
Print the index of the dominant element, or -1.
Constraints
2 <= N <= 50; 0 <= A[i] <= 1000
Example input
4 3 6 1 0
Example output
1
Explanation
In 3 6 1 0, the largest value 6 is at index 1 and is at least twice 3, 1 and 0. So the answer is 1.