[LeetCode]#2057. Smallest Index With Equal Value

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.
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].
Input: nums = [1,2,3,4,5,6,7,8,9,0]
Output: -1
Explanation: No index satisfies i mod 10 == nums[i].
Input: nums = [2,1,3,5,2]
Output: 1
Explanation: 1 is the only index with i mod 10 == nums[i].
  1. Calculate all number mod 10.
  2. Check mod 10 result is match nums[i] number or not.
  3. Return smallest one related index.
  4. If no mod 10, return -1.
class Solution:
def smallestEqual(self, nums):
ans=[]
for i in range(len(nums)):
if i % 10 ==nums[i]:
ans.append(i)
if ans:
return min(ans)
else:
return -1

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

You should at least do more than change a single image if you're going to blatantly rip off…

Using Python & Streamlit to predict The 2021 Masters winner

Healthy diet to avoid fat boy iOS apps

My first Side Project

How to Use JSON in Python

How to Learn to Program in C++: Introduction

Building bridges: Connecting to a third-party Service

Setup Your Dev Environment on Windows - with PHP 7 & MySQL 5.7

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#1974. Minimum Time to Type Word Using Special Typewriter

LeetCode Patterns Adventure 12 — Palindrome Linked List

Remove nth node from end of list Leetcode Python

Cracking the Coding interview Book Problem Series: Problem 10