Menu Close

Find whether the largest element in the array is at least twice as much as every other number in the array.

In a given integer array nums, there is always exactly one largest element. Find 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, otherwise return -1.

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

nums=list(map(int,input('Enter the array of nums: ').split(' ')))
l=sorted(nums,reverse=True)
if(len(l)>=2):
    if(l[0]>=(l[1]*2)):
        print(nums.index(l[0]))
    else:
        print(-1)
else:
    print(0)

Input_1:
Enter the array of nums: 0 0 0 1
Output:
3


Input_2:
Enter the array of nums: 0 1
Output:
1


Input_3:
Enter the array of nums: 1
Output:
0


Input_4:
Enter the array of nums: 1 2 3 4
Output:
-1


Input_5:
Enter the array of nums: 3 6 1 0
Output:
1


Morae!Q!