{x}
blog image

Smallest Index With Equal Value

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

Solution Explanation:

The problem asks to find the smallest index i in the input array nums such that i mod 10 == nums[i]. This means we're looking for an index where the remainder when the index is divided by 10 is equal to the value at that index in the array.

The most straightforward approach is to iterate through the array and check this condition for each index. If the condition is met, we immediately return the index. If the loop completes without finding such an index, we return -1.

Time and Space Complexity Analysis:

  • Time Complexity: O(n), where n is the length of the input array nums. This is because we iterate through the array at most once. In the best-case scenario (the condition is met at the first index), the time complexity is O(1).

  • Space Complexity: O(1). We use only a constant amount of extra space, regardless of the input size. We don't create any new data structures whose size depends on the input array's size.

Code Implementation in Different Languages:

The code implementations below follow the described approach. They all have the same logic: iterate through the array, check the condition, and return the index if found or -1 otherwise. The syntax differs slightly between the languages, but the core algorithm remains the same.

Python:

class Solution:
    def smallestEqual(self, nums: List[int]) -> int:
        for i, x in enumerate(nums):
            if i % 10 == x:
                return i
        return -1

Java:

class Solution {
    public int smallestEqual(int[] nums) {
        for (int i = 0; i < nums.length; ++i) {
            if (i % 10 == nums[i]) {
                return i;
            }
        }
        return -1;
    }
}

C++:

class Solution {
public:
    int smallestEqual(vector<int>& nums) {
        for (int i = 0; i < nums.size(); ++i) {
            if (i % 10 == nums[i]) {
                return i;
            }
        }
        return -1;
    }
};

Go:

func smallestEqual(nums []int) int {
	for i, x := range nums {
		if i%10 == x {
			return i
		}
	}
	return -1
}

JavaScript:

function smallestEqual(nums) {
    for (let i = 0; i < nums.length; i++) {
        if (i % 10 === nums[i]) {
            return i;
        }
    }
    return -1;
}
 

TypeScript:

function smallestEqual(nums: number[]): number {
    for (let i = 0; i < nums.length; i++) {
        if (i % 10 === nums[i]) {
            return i;
        }
    }
    return -1;
}

Rust:

impl Solution {
    pub fn smallest_equal(nums: Vec<i32>) -> i32 {
        for i in 0..nums.len() {
            if i % 10 == nums[i] as usize {
                return i as i32;
            }
        }
        -1
    }
}

These are just a few examples; the same algorithm can be implemented in many other programming languages with minor syntactic changes. The core idea remains the same – a linear scan of the array to find the smallest index satisfying the given condition.