2057. Smallest Index With Equal Value LeetCode Solution

In this guide, you will get 2057. Smallest Index With Equal Value LeetCode Solution with the best time and space complexity. The solution to Smallest Index With Equal Value problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Smallest Index With Equal Value solution in C++
  4. Smallest Index With Equal Value solution in Java
  5. Smallest Index With Equal Value solution in Python
  6. Additional Resources
2057. Smallest Index With Equal Value LeetCode Solution image

Problem Statement of 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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)
See also  2343. Query Kth Smallest Trimmed Number LeetCode Solution

2057. Smallest Index With Equal Value LeetCode Solution in C++

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

2057. Smallest Index With Equal Value LeetCode Solution in Java

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

2057. Smallest Index With Equal Value LeetCode Solution in Python

class Solution:
  def smallestEqual(self, nums: list[int]) -> int:
    return next((i for i, num in enumerate(nums) if i % 10 == num), -1)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.