Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 1000 <= nums[i] <= 9Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[0,1,2]
[4,3,2,1]
[1,2,3,4,5,6,7,8,9,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2057: Smallest Index With Equal Value
class Solution {
public int smallestEqual(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
if (i % 10 == nums[i]) {
return i;
}
}
return -1;
}
}
// Accepted solution for LeetCode #2057: Smallest Index With Equal Value
func smallestEqual(nums []int) int {
for i, x := range nums {
if i%10 == x {
return i
}
}
return -1
}
# Accepted solution for LeetCode #2057: Smallest Index With Equal Value
class Solution:
def smallestEqual(self, nums: List[int]) -> int:
for i, x in enumerate(nums):
if i % 10 == x:
return i
return -1
// Accepted solution for LeetCode #2057: Smallest Index With Equal Value
impl Solution {
pub fn smallest_equal(nums: Vec<i32>) -> i32 {
for (i, &x) in nums.iter().enumerate() {
if i % 10 == x as usize {
return i as i32;
}
}
-1
}
}
// Accepted solution for LeetCode #2057: Smallest Index With Equal Value
function smallestEqual(nums: number[]): number {
for (let i = 0; i < nums.length; ++i) {
if (i % 10 === nums[i]) {
return i;
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.