[LeetCode] Count Dominant Indices

3833. Count Dominant Indices

You are given an integer array nums of length n.

An element at index i is called dominant if: nums[i] > average(nums[i + 1], nums[i + 2], ..., nums[n - 1])

Your task is to count the number of indices i that are dominant.

The average of a set of numbers is the value obtained by adding all the numbers together and dividing the sum by the total number of numbers.

Note: The rightmost element of any array is not dominant.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int dominantIndices(vector<int>& nums) {
int res = 0;
for(int i = nums.size() - 1, cnt = 1, sum = 0; i >= 0; i--, cnt++) {
sum += nums[i];
if(nums[i] * cnt > sum) res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/count-dominant-indices/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.