Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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:
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:
nums[1] = 10, the sum of digits is 1 + 0 = 1, which is equal to index i = 1.nums[2] = 11, the sum of digits is 1 + 1 = 2, which is equal to index i = 2.Example 3:
Input: nums = [1,2,3]
Output: -1
Explanation:
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 1000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,3,2]
[1,10,11]
[1,2,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3550: Smallest Index With Digit Sum Equal to Index
class Solution {
public int smallestIndex(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
int s = 0;
while (nums[i] != 0) {
s += nums[i] % 10;
nums[i] /= 10;
}
if (s == i) {
return i;
}
}
return -1;
}
}
// Accepted solution for LeetCode #3550: Smallest Index With Digit Sum Equal to Index
func smallestIndex(nums []int) int {
for i, x := range nums {
s := 0
for ; x > 0; x /= 10 {
s += x % 10
}
if s == i {
return i
}
}
return -1
}
# Accepted solution for LeetCode #3550: Smallest Index With Digit Sum Equal to Index
class Solution:
def smallestIndex(self, nums: List[int]) -> int:
for i, x in enumerate(nums):
s = 0
while x:
s += x % 10
x //= 10
if s == i:
return i
return -1
// Accepted solution for LeetCode #3550: Smallest Index With Digit Sum Equal to Index
fn smallest_index(nums: Vec<i32>) -> i32 {
match nums.into_iter().enumerate().position(|(i, mut n)| {
let mut sum = 0;
while n > 0 {
sum += n % 10;
n /= 10;
}
sum as usize == i
}) {
Some(v) => v as i32,
None => -1,
}
}
fn main() {
let nums = vec![1, 3, 2];
let ret = smallest_index(nums);
println!("ret={ret}");
}
#[test]
fn test() {
{
let nums = vec![1, 3, 2];
let ret = smallest_index(nums);
assert_eq!(ret, 2);
}
{
let nums = vec![1, 10, 11];
let ret = smallest_index(nums);
assert_eq!(ret, 1);
}
}
// Accepted solution for LeetCode #3550: Smallest Index With Digit Sum Equal to Index
function smallestIndex(nums: number[]): number {
for (let i = 0; i < nums.length; ++i) {
let s = 0;
for (; nums[i] > 0; nums[i] = Math.floor(nums[i] / 10)) {
s += nums[i] % 10;
}
if (s === i) {
return i;
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.