Largest Number At Least Twice of Others

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.

30:00

Largest Number At Least Twice of Others
easy
Free written solution
Topics
Companies

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.

Example 1:
Input: {"nums":[3,6,1,0]}
Output: 1
Constraints:
  • 2≤nums.length≤502 \leq \text{nums.length} \leq 50

  • 0≤nums[i]≤1000 \leq \text{nums}[i] \leq 100

  • The largest element in nums is unique.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,6,1,0]}

Keep only two values while scanning: the largest (first) and the runner-up (second). Both start at -1, below every allowed value.

nums

3

6

1

0

0

1

2

3

second

-1

first (idx -1)

-1

-1 ≥ -2?

?

Variables
VariableValue
i-
first-1
second-1
idx-1
DepthFunction Call
Stack empty
0/6