First Index Equal to Its Value's Digit Sum

Quick Overview

Find the smallest zero-based array index equal to the decimal digit sum of its value. Handle zero values and multiple matching positions, and return minus one if no index qualifies.

First Index Equal to Its Value's Digit Sum

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# First Index Equal to Its Value's Digit Sum You are given an integer array `nums`, indexed from zero. Return the smallest index `i` for which the sum of the decimal digits of `nums[i]` equals `i`. If no index satisfies this condition, return `-1`. ### C++ Interface ```cpp class Solution { public: int smallestIndex(vector<int>& nums); }; ``` ### Examples **Example 1** ```text nums = [0,100,200] Output: 0 ``` The digit sums are `[0,1,2]`, so all three indices satisfy the condition. Index zero is the smallest. **Example 2** ```text nums = [8,12,90,4] Output: -1 ``` The digit sums are `[8,3,9,4]`. None equals its index in `[0,1,2,3]`. ### Constraints - `1 <= nums.length <= 100`. - `0 <= nums[i] <= 1000`. ### Clarifying Questions - What is the digit sum of zero? It is zero. - If several indices match, which one is returned? The smallest original index. - Does the function return the value at the matching index? No. It returns the index itself. - Are indices one-based? No; the first array position has index zero. ### What a Strong Answer Covers - Exact decimal digit sums, including values with zeros and the value zero itself. - The smallest matching original index when several positions qualify. - The required `-1` result when no position qualifies. - Time and space bounds for at most 100 values, each no larger than 1000. ### Follow-up Questions - If the array contains a single element, which allowed value makes the answer zero? - Can two different values at different positions both satisfy the condition? Give an example. ```hint Check indices in order Compute each value's decimal digit sum without changing the input array. Check positions from zero upward so the first match is already the smallest. ```

Overview: Find the smallest zero-based array index equal to the decimal digit sum of its value. Handle zero values and multiple matching positions, and return minus one if no index qualifies.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

First Index Equal to Its Value's Digit Sum

You are given an integer array nums, indexed from zero. Return the smallest index i for which the sum of the decimal digits of nums[i] equals i.

If no index satisfies this condition, return -1.

C++ Interface

class Solution {
public:
    int smallestIndex(vector<int>& nums);
};

Examples

Example 1

nums = [0,100,200]
Output: 0

The digit sums are [0,1,2], so all three indices satisfy the condition. Index zero is the smallest.

Example 2

nums = [8,12,90,4]
Output: -1

The digit sums are [8,3,9,4]. None equals its index in [0,1,2,3].

Constraints

  • 1 <= nums.length <= 100 .
  • 0 <= nums[i] <= 1000 .

Clarifying Questions Guidance

  • What is the digit sum of zero? It is zero.
  • If several indices match, which one is returned? The smallest original index.
  • Does the function return the value at the matching index? No. It returns the index itself.
  • Are indices one-based? No; the first array position has index zero.

What a Strong Answer Covers Guidance

  • Exact decimal digit sums, including values with zeros and the value zero itself.
  • The smallest matching original index when several positions qualify.
  • The required -1 result when no position qualifies.
  • Time and space bounds for at most 100 values, each no larger than 1000.

Follow-up Questions Guidance

  • If the array contains a single element, which allowed value makes the answer zero?
  • Can two different values at different positions both satisfy the condition? Give an example.