Skip to main content

Smallest Index With Digit Sum Equal to Index - Solution & Explanation

EasyArrayMath6 min readAsked at: Google
Practice this problem

Problem Statement

You are given an integer array nums.

Return the smallest index i such that the sum of the digits of nums[i] is equal to i.

If no such index exists, return -1.

 

Example 1:

Input: nums = [1,3,2]

Output: 2

Explanation:

  • For nums[2] = 2, the sum of digits is 2, which is equal to index i = 2. Thus, the output is 2.

Example 2:

Input: nums = [1,10,11]

Output: 1

Explanation:

  • For nums[1] = 10, the sum of digits is 1 + 0 = 1, which is equal to index i = 1.
  • For nums[2] = 11, the sum of digits is 1 + 1 = 2, which is equal to index i = 2.
  • Since index 1 is the smallest, the output is 1.

Example 3:

Input: nums = [1,2,3]

Output: -1

Explanation:

  • Since no index satisfies the condition, the output is -1.

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000

Approach Overview

Problem Overview: You are given an integer array nums. The task is to find the smallest index i where the digit sum of nums[i] equals the index itself. If no such index exists, return -1. The solution mainly relies on iterating through the array and computing the digit sum of each value.

Approach 1: Enumeration + Digit Sum (O(n · d) time, O(1) space)

Scan the array from left to right and compute the digit sum for every element. For each index i, repeatedly extract digits from nums[i] using modulo and division to calculate its digit sum. Compare the result with the current index. The first index where digitSum(nums[i]) == i is the answer. If the loop completes without a match, return -1. This works because the problem asks specifically for the smallest valid index, so the first match during iteration is guaranteed to be correct.

This approach uses a simple linear traversal over the array. Computing the digit sum of a number with d digits takes O(d) time, which makes the total complexity O(n · d). Since the digit sum calculation uses only a few integer variables, the space complexity remains O(1). The method is straightforward, easy to implement, and performs well because integers typically have a small number of digits.

Approach 2: Incremental Digit Sum Optimization (O(n) time, O(1) space)

If many digit sum calculations are required, you can slightly optimize by using properties of numbers in base‑10. Instead of recomputing the digit sum from scratch each time, maintain a running digit sum as numbers increase and adjust when carries occur (for example when moving from 19 to 20). This reduces repeated digit extraction operations. The core idea still relies on sequential iteration and digit arithmetic from math, but minimizes repeated work.

In practice, the improvement is minor for typical constraints, so most implementations still compute the digit sum directly for clarity. Both versions use constant extra memory and a single pass through the array.

Recommended for interviews: The standard enumeration with digit sum is what interviewers expect. It clearly demonstrates understanding of array traversal and basic number manipulation. Starting with the brute-force digit sum calculation shows correctness first, and discussing incremental digit-sum optimization highlights deeper insight into math properties and performance tradeoffs.

Solution

We can start from index i = 0 and iterate through each element x in the array, calculating the digit sum s of x. If s = i, return the index i. If no such index is found after traversing all elements, return -1.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1), as only constant extra space is used.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Enumeration + Digit SumO(n · d)O(1)Best general solution. Simple scan of the array with digit extraction for each number.
Incremental Digit Sum OptimizationO(n)O(1)When many digit sums must be computed and avoiding repeated digit extraction improves performance.

Video Solution

3550. Smallest Index With Digit Sum Equal to Index (Leetcode Easy)Programming Live with Larry413 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Smallest Index With Digit Sum Equal to Index easy or hard?
Smallest Index With Digit Sum Equal to Index is categorized as an Easy problem. It mainly tests basic array traversal and digit manipulation. The challenge lies in implementing the digit sum correctly and remembering to return the smallest valid index.
Smallest Index With Digit Sum Equal to Index Python/Java solution
Implement a loop over the array indices. For each index i, compute the digit sum of nums[i] using modulo and division operations, then compare it with i. Return the first index where they match; otherwise return -1 after the loop. The same logic translates directly to Python, Java, C++, Go, and TypeScript.
How to solve Smallest Index With Digit Sum Equal to Index in O(n)?
Traverse the array once and compare each index with the digit sum of the corresponding value. By computing the digit sum efficiently and stopping at the first valid match, the algorithm performs a single pass through the array. The practical complexity is O(n · d), but since d is small, it behaves close to O(n) in real scenarios.
What is the best approach for Smallest Index With Digit Sum Equal to Index?
The most practical approach is enumeration with digit sum. Iterate through the array, compute the digit sum of nums[i], and check if it equals the index i. The first match is returned immediately. This solution runs in O(n · d) time where d is the number of digits in each number and uses O(1) extra space.
Is Smallest Index With Digit Sum Equal to Index asked at Google/Amazon/Meta?
This problem represents a typical easy-level screening question focused on array traversal and basic math operations. Variants of digit-sum or index-matching problems appear in coding assessments used by large companies because they test attention to detail and implementation accuracy.
What data structure is used in Smallest Index With Digit Sum Equal to Index?
The primary data structure is a simple array. The algorithm iterates through the array sequentially and performs digit-sum calculations using basic integer arithmetic, without requiring additional data structures like hash maps or stacks.
What is the time complexity of Smallest Index With Digit Sum Equal to Index?
The typical solution runs in O(n · d) time, where n is the array length and d is the number of digits in nums[i]. Each element requires computing its digit sum using repeated division and modulo operations. Space complexity remains O(1) since only a few integer variables are used.

Ready to solve this problem?

Practice Smallest Index With Digit Sum Equal to Index with our built-in code editor and test cases.

Practice on FleetCode