Back to Browse

Smallest Index With Equal Value | LeetCode Solution | C++

27 views
May 4, 2026
5:23

๐Ÿ”น Problem: Smallest Index With Equal Value ๐Ÿ”— https://leetcode.com/problems/smallest-index-with-equal-value/ ๐Ÿ’ก Approach: In this problem, we are given an array nums, and we need to find the smallest index i such that: i % 10 == nums[i] The key idea here is understanding what i % 10 represents. It gives us the last digit of the index i. So basically, we are comparing the last digit of the index with the value present at that index in the array. To solve this: * We start traversing the array from index 0 to n-1. * At each index i, we calculate i % 10. * Then we compare it with nums[i]. * If both are equal, we immediately return i because we are moving from left to right, so this will be the smallest valid index. * If we finish traversing the entire array and do not find any such index, we return -1. ๐Ÿ‘‰ Why this approach is efficient: * We only traverse the array once. * We stop early as soon as we find the answer (early exit). * No extra data structures are used. โฑ Time Complexity: O(n) โ€” we visit each element once ๐Ÿ’พ Space Complexity: O(1) โ€” constant extra space ๐Ÿง  Key Concepts: * Modulo operation (i % 10) * Linear traversal of array * Early return optimization ๐Ÿ”— GitHub: https://github.com/abdulhaque2005 ๐Ÿ”— LeetCode: https://leetcode.com/u/pDjnXUuCp8/ ๐Ÿ”— LinkedIn: https://www.linkedin.com/in/abdul-haque78/ ๐Ÿ“ Notes: * This is a simple but important problem to understand modulo usage * Always check smallest condition first using left-to-right traversal * Early return improves performance in many problems ๐Ÿš€ DSA Practice Journey: * Target: 160+ LeetCode Questions * 40 YouTube Videos * Focus on C++ and STL #DSA #LeetCode #CPP #Coding #StriverA2Z #Programming #PlacementPrep

Download

0 formats

No download links available.

Smallest Index With Equal Value | LeetCode Solution | C++ | NatokHD