3550. Smallest Index With Digit Sum Equal to Index
1 min read
LeetCode Daily Problem: Smallest Index With Digit Sum Equal to Index
On this page
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.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 1000
Approach: Brute force
Nothing subtle here — just translate the condition into code.
python
for i, num in enumerate(nums):
if sum(map(int, str(num))) == i:
return i
return -1Complexity
- Time: O(n) — one pass over the array.
- Space: O(1) — only a few variables are used.
SeriesDaily Problem →
TagsSimulation