3550. Smallest Index With Digit Sum Equal to Index
Difficulty: Easy
Topics: Mid Level, Array, Math, Weekly Contest 450
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: For
nums[2] = 2, the sum of digits is 2, which is equal to indexi = 2. Thus, the output is 2.
Example 2:
- Input: nums = [1,10,11]
- Output: 1
-
Explanation:
- For
nums[1] = 10, the sum of digits is1 + 0 = 1, which is equal to indexi = 1. - For
nums[2] = 11, the sum of digits is1 + 1 = 2, which is equal to indexi = 2. - Since index 1 is the smallest, the output is 1.
- For
Example 3:
- Input: nums = [1,2,3]
- Output: -1
- Explanation: Since no index satisfies the condition, the output is -1.
Example 4:
- Input: nums = [0]
- Output: 0
Example 5:
- Input: nums = [0, 1]
- Output: 0
Example 6:
- Input: nums = [9, 10]
- Output: 1
Example 7:
- Input: nums = [5, 6, 7, 8, 9, 10]
- Output: 5
Example 8:
- Input: nums = [1000]
- Output: -1
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 1000
Hint:
- Simulate as described
Solution:
We scan the array from left to right, compute the sum of digits for each nums[i], and return the first index i where that digit sum equals i. If no index satisfies the condition, we return -1.
Approach
- Iterate through
numsusingforeach ($nums as $i => $num). - For each number, compute its digit sum by repeatedly taking
% 10and dividing by10. - Compare the computed digit sum with the current index
i. - Return
iimmediately when a match is found, because scanning left-to-right guarantees the smallest index. - If the loop finishes without a match, return
-1.
Let's implement this solution in PHP: 3550. Smallest Index With Digit Sum Equal to Index
<?php
/**
* @param Integer[] $nums
* @return Integer
*/
function smallestIndex(array $nums): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo smallestIndex([1,3,2]) . "\n"; // Output: 2
echo smallestIndex([1,10,11]) . "\n"; // Output: 1
echo smallestIndex([1,2,3]) . "\n"; // Output: -1
echo smallestIndex([0]) . "\n"; // Output: 0
echo smallestIndex([0,1]) . "\n"; // Output: 0
echo smallestIndex([9,10]) . "\n"; // Output: 1
echo smallestIndex([5, 6, 7, 8, 9, 10]) . "\n"; // Output: 5
echo smallestIndex([1000]) . "\n"; // Output: -1
?>
Explanation:
- Index
istarts from0. - For a number like
10, the digit sum is1 + 0 = 1. - For
nums[i] = 0, the digit sum is0, which is handled correctly because thewhile ($x > 0)loop simply does not run. - Since we return on the first valid index, no extra comparison or storage is needed.
Complexity Analysis
-
Time Complexity:
O(n * d), wherenis the length ofnumsanddis the maximum number of digits innums[i]. Sincenums[i] <= 1000,d <= 4, so this is effectivelyO(n). -
Space Complexity:
O(1), because only a few integer variables are used.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)