Problem Description
Given a 0-indexed integer array nums, find and return the smallest index i such that i mod 10 equals nums[i]. If no such index exists, return -1.
Key Insights
- Use a simple loop to iterate over the array.
- For each index, compute i mod 10 and compare it to the element at that index.
- Return early on finding a matching index to ensure minimal comparisons.
- Since nums[i] is constrained to be between 0 and 9, the mod operation aligns naturally with the values.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the array. Space Complexity: O(1), as only a constant amount of extra space is used.
Solution
We iterate over the array, checking if the condition i mod 10 == nums[i] holds true at any index. We use a simple loop with a modulus operation at each iteration. If a match is found, we immediately return the index. If the loop completes without finding a match, we return -1. This approach leverages the simplicity of the modulo operation and the small constraint on the array's length.