↓Skip to main content
  1. LeetCode/

LeetCode 3833: Count Dominant Indices

·2 mins· ·
LeetCode Easy Array Enumeration
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-02-08

  • Submissions: Wrong Answer → Accepted

Approach
#

nums[i] must exceed the average of the elements to its right (excluding itself). Scan right to left keeping their sum and count (Array Template 3). Compare averages by multiplying: nums[i] > s / c is nums[i] * c > s, avoiding floats and integer division. The last element has nothing to its right and does not count.

My Attempts
#

Attempt 1 had two problems: the average used // integer division, and position i’s average included itself. For [4, 3, 5, 4] only 5 > 4 holds, so the answer is 1; it returned 2. Switching to / and comparing with the suffix average from i + 1 passed.

Solution
#

class Solution:
    def dominantIndices(self, nums: List[int]) -> int:
        n, suffix, res = len(nums), 0, 0
        for i in range(n - 1, -1, -1):
            cnt = n - 1 - i
            if cnt and nums[i] * cnt > suffix:
                res += 1
            suffix += nums[i]
        return res

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • The average excludes the element itself.
  • Integer division rounds the average down and breaks the comparison; multiplying is safest.

Interview Explanation
#

Scanning right to left with the sum of the right side, I check whether the element times the count on its right exceeds that sum, which is the same as beating the average without floats. Time O(n).

Related

LeetCode 2094: Finding 3-Digit Even Numbers
·2 mins
LeetCode Easy Hash-Table Enumeration Array
LeetCode 2094 note: enumerate the even numbers 100 to 998 instead and check that each digit needed is available; Counter subtraction keeps only positives
LeetCode 976: Largest Perimeter Triangle
·2 mins
LeetCode Easy Math Greedy Sorting Geometry Array
LeetCode 976 note: after sorting, check consecutive triples from the largest down; the first with a + b > c gives the largest perimeter
LeetCode 2122: Recover the Original Array
·3 mins
LeetCode Hard Two-Pointers Sorting Hash-Table Enumeration Array
LeetCode 2122 note: the smallest number is always in lower, so enumerating its partner fixes k; after sorting, two pointers pair each smallest unmatched number with the number 2k above it
LeetCode 2200: Find All K-Distant Indices in an Array
·2 mins
LeetCode Easy Two-Pointers Array
LeetCode 2200 note: extend each key k positions both ways, and keep a pointer to how far output has gone so overlapping ranges are not emitted twice
LeetCode 3637: Trionic Array I
·2 mins
LeetCode Easy Array
LeetCode 3637 note: walk one index through uphill, downhill and uphill; all three runs must be non-empty and reach the end exactly
LeetCode 1304: Find N Unique Integers Sum up to Zero
·1 min
LeetCode Easy Math Array
LeetCode 1304 note: place 1..n/2 and their negatives so each pair cancels, adding 0 when n is odd