Check Whether the Maximum Dominates Every Other Value

Quick Overview

Check Whether the Maximum Dominates Every Other Value. The array has a unique largest value. Return its index if it is at least twice every other entry; otherwise return -1.

Check Whether the Maximum Dominates Every Other Value

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem The array has a unique largest value. Return its index if it is at least twice every other entry; otherwise return -1. ```cpp class Solution { public: int dominantIndex(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [2,8,4] Output: 1 Explanation: 8 is at least twice both 2 and 4. ``` **Example 2:** ```text nums = [5,3] Output: -1 Explanation: 5 is smaller than 2*3. ``` ### Constraints - Array length lies in [2,50]. - Values lie in [0,100]. - Exactly one entry is the maximum. ### Contract considerations Return the maximum's original index and use an inclusive inequality. ```hint Read the boundary condition Which one value could possibly satisfy the dominance condition? ``` ### Boundary reminders Return the maximum's original index and use an inclusive inequality.

Overview: Check Whether the Maximum Dominates Every Other Value. The array has a unique largest value. Return its index if it is at least twice every other entry; otherwise return -1.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

The array has a unique largest value. Return its index if it is at least twice every other entry; otherwise return -1.

class Solution {
public:
    int dominantIndex(vector<int>& nums);
};

Examples

Example 1:

nums = [2,8,4]
Output: 1
Explanation: 8 is at least twice both 2 and 4.

Example 2:

nums = [5,3]
Output: -1
Explanation: 5 is smaller than 2*3.

Constraints

  • Array length lies in [2,50].
  • Values lie in [0,100].
  • Exactly one entry is the maximum.

Contract considerations

Return the maximum's original index and use an inclusive inequality.

Boundary reminders

Return the maximum's original index and use an inclusive inequality.