Skip to main content

Smallest Index With Equal Value - Solution & Explanation

EasyArray12 min readAsked at: Amazon, Google
Practice this problem

Problem Statement

Given a 0-indexed integer array nums, return the smallest index i of nums such that i mod 10 == nums[i], or -1 if such index does not exist.

x mod y denotes the remainder when x is divided by y.

 

Example 1:

Input: nums = [0,1,2]
Output: 0
Explanation: 
i=0: 0 mod 10 = 0 == nums[0].
i=1: 1 mod 10 = 1 == nums[1].
i=2: 2 mod 10 = 2 == nums[2].
All indices have i mod 10 == nums[i], so we return the smallest index 0.

Example 2:

Input: nums = [4,3,2,1]
Output: 2
Explanation: 
i=0: 0 mod 10 = 0 != nums[0].
i=1: 1 mod 10 = 1 != nums[1].
i=2: 2 mod 10 = 2 == nums[2].
i=3: 3 mod 10 = 3 != nums[3].
2 is the only index which has i mod 10 == nums[i].

Example 3:

Input: nums = [1,2,3,4,5,6,7,8,9,0]
Output: -1
Explanation: No index satisfies i mod 10 == nums[i].

 

Constraints:

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

Approach Overview

Problem Overview: Given an integer array nums, return the smallest index i such that i % 10 == nums[i]. If no index satisfies the condition, return -1. The task is mainly about scanning the array and checking a simple modulo condition.

Approach 1: Iterative Method (O(n) time, O(1) space)

The most direct solution is a single pass through the array. For each index i, compute i % 10 and compare it with nums[i]. The first index where the values match is the answer because the traversal proceeds from left to right. As soon as the condition holds, return that index immediately.

This approach relies only on sequential iteration and constant-time arithmetic operations. No additional data structures are needed. The algorithm performs exactly one comparison per element, making the time complexity O(n) and space complexity O(1). Because the problem asks for the smallest index, scanning from the beginning naturally guarantees correctness.

This is the approach most developers use in practice. The logic is minimal, easy to implement, and efficient even for large arrays. Problems involving element checks tied to index properties often reduce to a straightforward traversal over an array.

Approach 2: Divide and Conquer (O(n) time, O(log n) space)

A divide and conquer variant splits the array into two halves and recursively checks each segment for a valid index. The algorithm first searches the left half. If a valid index exists there, return it immediately because it will always be smaller than any index in the right half. If the left half contains no valid index, search the right half.

Each recursive call processes a smaller portion of the array while maintaining the original index positions for the modulo comparison. The overall number of examined elements remains n, so the time complexity stays O(n). However, recursive calls introduce a stack depth of O(log n).

This method demonstrates the mechanics of divide and conquer, but it does not provide a performance advantage over a simple loop. The modulo comparison still needs to happen for each element, and recursion only adds overhead. Still, it can be useful when practicing recursive decomposition patterns alongside simple array traversal.

Recommended for interviews: The iterative scan is the expected answer. Interviewers typically look for the observation that you only need to check each index once and can return immediately on the first match. Mentioning the O(n) time and O(1) space complexity demonstrates solid understanding. The divide and conquer version is mostly educational rather than practical.

Approach 1: Approach 1: Iterative Method

This approach involves using a simple iterative method to solve the problem. The idea is to loop through the data structure, process elements, and maintain a result based on conditions.

The function solve iteratively adds each element of the array arr and produces the sum as a result.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Approach 2: Divide and Conquer

This approach uses the divide and conquer strategy, which involves dividing the problem into sub-problems, solving each sub-problem independently, and combining the results.

This C function sum recursively divides the array into halves, sums each half, and returns the total sum.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n)
Space Complexity: O(log n), due to recursion stack space.

Try this approach in the editor →

Approach 3: Traversal

We directly traverse the array. For each index i, we check if it satisfies i bmod 10 = nums[i]. If it does, we return the current index i.

If we traverse the entire array and do not find a satisfying index, we return -1.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Cangjie

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Iterative Method

Time Complexity: O(n)
Space Complexity: O(1)

Approach 2: Divide and Conquer

Time Complexity: O(n log n)
Space Complexity: O(log n), due to recursion stack space.

Traversal—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative MethodO(n)O(1)Best for this problem. Simple linear scan and early return when the first valid index appears.
Divide and ConquerO(n)O(log n)Useful for practicing recursive decomposition, though it adds overhead compared to iteration.

Video Solution

Leetcode | 2057. Smallest Index With Equal Value | Easy | Java Solution • Developer Docs • 1,080 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Smallest Index With Equal Value easy or hard?
Smallest Index With Equal Value is classified as an Easy problem on LeetCode. The challenge mainly tests understanding of array iteration and modulo arithmetic rather than complex algorithms or data structures.
Smallest Index With Equal Value Python/Java solution
In Python or Java, implement a loop that checks each index i and compares i % 10 with nums[i]. If they match, return i. Otherwise continue scanning until the end and return -1 if no valid index exists. The implementation stays O(n) time and O(1) space in both languages.
How to solve Smallest Index With Equal Value in O(n)?
Traverse the array from left to right. For each index i, compute i % 10 and compare it with nums[i]. If they match, return i immediately because it is the smallest valid index. If the loop finishes without a match, return -1.
What is the best approach for Smallest Index With Equal Value?
The best approach is a simple linear scan of the array. Iterate through indices from 0 to n-1 and check whether i % 10 equals nums[i]. The first index satisfying this condition is returned immediately. This method runs in O(n) time and O(1) space.
Is Smallest Index With Equal Value asked at Google/Amazon/Meta?
Problems based on simple array traversal and modular arithmetic appear frequently in coding interviews at companies like Amazon and Google. While this exact question may vary, the pattern of scanning arrays and validating index-based conditions is common in entry-level interview rounds.
What data structure is used in Smallest Index With Equal Value?
The problem uses a basic array. The solution only requires sequential traversal and a modulo operation on the index. No additional data structures such as hash maps or stacks are needed.
What is the time complexity of Smallest Index With Equal Value?
The optimal solution runs in O(n) time because each element of the array is checked once. The space complexity is O(1) since only a few variables are used and no additional data structures are required.

Ready to solve this problem?

Practice Smallest Index With Equal Value with our built-in code editor and test cases.

Practice on FleetCode