↓Skip to main content
  1. LeetCode/

LeetCode 3550: Smallest Index With Digit Sum Equal to Index

·1 min· ·
LeetCode Easy Math Array
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: 2025-05-18

  • Submissions: Accepted (first try)

Approach
#

Starting from index 0, compute the digit sum of nums[i]; the first one equal to i is the smallest index. If none, return -1.

My Solution
#

class Solution:
    def smallestIndex(self, nums: List[int]) -> int:
        for i, x in enumerate(nums):
            if sum(int(c) for c in str(x)) == i:
                return i
        return -1

Complexity
#

Time O(n · d) with d digits, space O(d).

Mistakes to Avoid
#

  • Return the smallest index: scan from the start and stop at the first match.

Interview Explanation
#

Scanning indices in increasing order, I sum each number’s digits via its string form and return the first index that matches, otherwise -1. Time O(n · d).

Related

LeetCode 1979: Find Greatest Common Divisor of Array
·1 min
LeetCode Easy Math Number-Theory Gcd Array
LeetCode 1979 note: find the minimum and maximum, then take their GCD with Euclid’s algorithm
LeetCode 1030: Matrix Cells in Distance Order
·2 mins
LeetCode Easy Array Math Geometry Sorting
LeetCode 1030 note: list every coordinate and sort by Manhattan distance to the center; return coordinates, not distances
LeetCode 989: Add to Array-Form of Integer
·2 mins
LeetCode Easy Array Math
LeetCode 989 note: treat k as the carry, add from the last element backwards, and keep prepending digits while k remains
LeetCode 908: Smallest Range I
·1 min
LeetCode Easy Array Math
LeetCode 908 note: lower the maximum by k and raise the minimum by k; the answer is max(0, max - min - 2k)
LeetCode 914: X of a Kind in a Deck of Cards
·2 mins
LeetCode Easy Array Hash-Table Math Counting
LeetCode 914 note: every card count must be divisible by one X >= 2, i.e. the GCD of all counts is at least 2
LeetCode 892: Surface Area of 3D Shapes
·2 mins
LeetCode Easy Array Math Geometry Matrix
LeetCode 892 note: a stack of v cubes has 4v + 2 faces; subtract 2 × min for each overlap with the stacks above and to the left