Smallest Index With Digit Sum Equal to Index

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`.

30:00

Smallest Index With Digit Sum Equal to Index
easy
Free written solution
Topics
Companies

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
Constraints:
  • 1≤nums.length≤1001 \leq nums.length \leq 100

  • 0≤nums[i]≤10000 \leq nums[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,3,2]}

Walk the indices from left to right. For each one, add up the digits of nums[i] and compare that sum with i itself.

0

1

Σ0

1

3

Σ0

2

2

Σ0

Variables
VariableValue
n3
DepthFunction Call
Stack empty
0/9