LeetCode 3550: Smallest Index With Digit Sum Equal to Index – Java Solution & Explanation

Find the first index where the sum of the digits of the element equals its index using a simple Java array traversal and digit-sum technique.

Krishna Shrivastava
1 views
LinkedInGithubX
0
0
LeetCode 3550: Smallest Index With Digit Sum Equal to Index – Java Solution & Explanation
Listen to articleAudio version

Introduction

LeetCode 3550, Smallest Index With Digit Sum Equal to Index, combines two simple ideas:

  1. Traversing an array using its index
  2. Finding the sum of the digits of an integer

For every index i, the task is to check whether:

sum of digits of nums[i] = i

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:

nums = [1, 10, 11]

Check every index from left to right.

Index 0

nums[0] = 1

Digit sum:

1

Compare with index:

1 != 0

So index 0 does not work.

Index 1

nums[1] = 10

Digit sum:

1 + 0 = 1

Now:

digit sum = index
1 = 1

Therefore, index 1 satisfies the condition.

Because the array is checked from left to right, this is automatically the smallest valid index.

Answer = 1

Approach

The solution follows a simple left-to-right traversal.

For every index i:

  1. Take nums[i].
  2. Calculate the sum of its digits.
  3. Compare that sum with i.
  4. Return i immediately when they are equal.
  5. 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.

public int so(int n){

int su = 0;

while(n != 0){

int dig = n % 10;

n /= 10;

su += dig;
}

return su;
}

Two operations are especially useful here.

Extracting the last digit

n % 10

For example:

123 % 10 = 3

So the last digit is obtained.

Removing the last digit

n /= 10

For example:

123 / 10 = 12

Since integer division is being used, the decimal part is discarded.

Repeating these two operations gives every digit.

For:

123

the process is:

123 % 10 → 3
123 / 10 → 12

12 % 10 → 2
12 / 10 → 1

1 % 10 → 1
1 / 10 → 0

Therefore:

Digit Sum = 3 + 2 + 1 = 6

Checking Each Index

The main method traverses the array:

for(int i = 0; i < nums.length; i++){

For a single-digit number, the submitted solution directly compares the value with the index:

if(nums[i] < 10){
if(i == nums[i]){
return i;
}
}

For numbers containing multiple digits, it calculates their digit sum:

else{
int su = so(nums[i]);

if(su == i){
return i;
}
}

This works correctly for the given constraints.

Java Solution

Here is the submitted approach with clearer comments:

class Solution {

// Returns the sum of all digits of n
public int so(int n){

int su = 0;

while(n != 0){

// Extract the last digit
int dig = n % 10;

// Remove the last digit
n /= 10;

// Add the digit to the sum
su += dig;
}

return su;
}

public int smallestIndex(int[] nums){

// Traverse from the smallest index
for(int i = 0; i < nums.length; i++){

// Single-digit numbers can be compared directly
if(nums[i] < 10){

if(i == nums[i]){
return i;
}

}else{

// Calculate digit sum for multi-digit numbers
int su = so(nums[i]);

if(su == i){
return i;
}
}
}

// No valid index was found
return -1;
}
}

Dry Run

Consider:

nums = [1, 3, 2]

The indices are:

0 1 2

Index 0

nums[0] = 1

Digit sum:

1

Comparison:

1 != 0

Not valid.

Index 1

nums[1] = 3

Digit sum:

3

Comparison:

3 != 1

Not valid.

Index 2

nums[2] = 2

Digit sum:

2

Comparison:

2 = 2

A valid index is found.

Therefore:

Answer = 2

The function immediately returns 2.

Another Example

Consider:

nums = [1, 10, 11]


IndexValueDigit SumMatch?
011No
1101Yes
2112Yes

Both indices 1 and 2 satisfy the condition.

However, index 1 appears first.

Therefore:

Answer = 1

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:

so(7)

returns:

7

So the main logic can simply calculate the digit sum for every element.

A slightly cleaner version is:

class Solution {

public int digitSum(int n){

int sum = 0;

while(n > 0){
sum += n % 10;
n /= 10;
}

return sum;
}

public int smallestIndex(int[] nums){

for(int i = 0; i < nums.length; i++){

if(digitSum(nums[i]) == i){
return i;
}
}

return -1;
}
}

This version expresses the core idea directly:

digit sum of nums[i] == i

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:

0 → 1 → 2 → 3 → ...

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:

  1. n = length of the array
  2. d = 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:

while(n > 0){
sum += n % 10;
n /= 10;
}

The two operations have very specific purposes:

n % 10 → extract the last digit
n / 10 → remove the last digit

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:

  1. Array traversal
  2. Index-based conditions
  3. Digit extraction
  4. Integer division
  5. Early return
  6. Constant-space problem solving

The main condition is simple:

digitSum(nums[i]) == i

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.

Ai Assistant Kas