Introduction
LeetCode 3550, Smallest Index With Digit Sum Equal to Index, combines two simple ideas:
- Traversing an array using its index
- Finding the sum of the digits of an integer
For every index i, the task is to check whether:
The smallest valid index should be returned.
If no index satisfies the condition, the answer is -1.
Although the problem is easy, it is a good exercise for understanding how an array index can be compared with a property calculated from the corresponding value.
Question Link
LeetCode 3550 – Smallest Index With Digit Sum Equal to Index
Understanding the Problem
Consider:
Check every index from left to right.
Index 0
Digit sum:
Compare with index:
So index 0 does not work.
Index 1
Digit sum:
Now:
Therefore, index 1 satisfies the condition.
Because the array is checked from left to right, this is automatically the smallest valid index.
Approach
The solution follows a simple left-to-right traversal.
For every index i:
- Take
nums[i]. - Calculate the sum of its digits.
- Compare that sum with
i. - Return
iimmediately when they are equal. - If the complete array is checked without finding a match, return
-1.
The early return is important because the problem asks for the smallest index.
There is no need to continue searching after finding the first valid index.
Finding the Digit Sum
The helper method so() calculates the sum of the digits.
Two operations are especially useful here.
Extracting the last digit
For example:
So the last digit is obtained.
Removing the last digit
For example:
Since integer division is being used, the decimal part is discarded.
Repeating these two operations gives every digit.
For:
the process is:
Therefore:
Checking Each Index
The main method traverses the array:
For a single-digit number, the submitted solution directly compares the value with the index:
For numbers containing multiple digits, it calculates their digit sum:
This works correctly for the given constraints.
Java Solution
Here is the submitted approach with clearer comments:
Dry Run
Consider:
The indices are:
Index 0
Digit sum:
Comparison:
Not valid.
Index 1
Digit sum:
Comparison:
Not valid.
Index 2
Digit sum:
Comparison:
A valid index is found.
Therefore:
The function immediately returns 2.
Another Example
Consider:
| Index | Value | Digit Sum | Match? |
| 0 | 1 | 1 | No |
| 1 | 10 | 1 | Yes |
| 2 | 11 | 2 | Yes |
Both indices 1 and 2 satisfy the condition.
However, index 1 appears first.
Therefore:
This demonstrates why traversing from left to right and returning immediately is enough to find the smallest valid index.
A Small Simplification
The special case for single-digit numbers is not actually necessary.
The digit-sum method can also handle a single-digit number.
For example:
returns:
So the main logic can simply calculate the digit sum for every element.
A slightly cleaner version is:
This version expresses the core idea directly:
There is no need to treat single-digit and multi-digit numbers differently.
Why Does the First Match Give the Smallest Index?
The array is traversed in increasing order:
The moment a valid index is found, every smaller index has already been checked.
Therefore, the first match must be the smallest possible answer.
This is a common pattern in array problems:
When the problem asks for the smallest index satisfying a condition, scan from left to right and return the first match.
Complexity Analysis
Let:
n= length of the arrayd= number of digits in an element
Each array element is visited once, and calculating its digit sum takes O(d) time.
Therefore:
Time Complexity: O(n × d)
Under the given constraints, nums[i] <= 1000, so each number contains at most 4 digits. This makes d effectively bounded by a small constant, giving:
Effective Time Complexity: O(n)
The digit-sum calculation uses only a few integer variables.
Space Complexity: O(1)
Interview Tip
When a problem asks for the digit sum of an integer, remember this standard pattern:
The two operations have very specific purposes:
Also, when a problem asks for the smallest index, checking elements from left to right often allows an immediate return as soon as the condition is satisfied.
Conclusion
LeetCode 3550 is a small problem, but it combines several useful programming fundamentals:
- Array traversal
- Index-based conditions
- Digit extraction
- Integer division
- Early return
- Constant-space problem solving
The main condition is simple:
By scanning the array from left to right, the first matching index is automatically the smallest one.
The solution runs in O(n × d) time, which is effectively O(n) for the given constraints, and uses O(1) extra space.




