{"id":8660,"date":"2021-11-03T19:35:35","date_gmt":"2021-11-04T02:35:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8660"},"modified":"2021-11-03T23:30:07","modified_gmt":"2021-11-04T06:30:07","slug":"leetcode-2057-smallest-index-with-equal-value","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2057-smallest-index-with-equal-value\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2057. Smallest Index With Equal Value"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, return&nbsp;<em>the&nbsp;<strong>smallest<\/strong>&nbsp;index&nbsp;<\/em><code>i<\/code><em>&nbsp;of&nbsp;<\/em><code>nums<\/code><em>&nbsp;such that&nbsp;<\/em><code>i mod 10 == nums[i]<\/code><em>, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if such index does not exist<\/em>.<\/p>\n\n\n\n<p><code>x mod y<\/code>&nbsp;denotes the&nbsp;<strong>remainder<\/strong>&nbsp;when&nbsp;<code>x<\/code>&nbsp;is divided by&nbsp;<code>y<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [0,1,2]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> \ni=0: 0 mod 10 = 0 == nums[0].\ni=1: 1 mod 10 = 1 == nums[1].\ni=2: 2 mod 10 = 2 == nums[2].\nAll indices have i mod 10 == nums[i], so we return the smallest index 0.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [4,3,2,1]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \ni=0: 0 mod 10 = 0 != nums[0].\ni=1: 1 mod 10 = 1 != nums[1].\ni=2: 2 mod 10 = 2 == nums[2].\ni=3: 3 mod 10 = 3 != nums[3].\n2 is the only index which has i mod 10 == nums[i].\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5,6,7,8,9,0]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> No index satisfies i mod 10 == nums[i].\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,1,3,5,2]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> 1 is the only index with i mod 10 == nums[i].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int smallestEqual(vector<int>& nums) {    \n    for (int i = 0; i < nums.size(); ++i)\n      if (i % 10 == nums[i]) return i;\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, return&nbsp;the&nbsp;smallest&nbsp;index&nbsp;i&nbsp;of&nbsp;nums&nbsp;such that&nbsp;i mod 10 == nums[i], or&nbsp;-1&nbsp;if such index does not exist. x mod y&nbsp;denotes the&nbsp;remainder&nbsp;when&nbsp;x&nbsp;is divided by&nbsp;y. Example 1: Input: nums&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222],"class_list":["post-8660","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8660"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8660\/revisions"}],"predecessor-version":[{"id":8663,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8660\/revisions\/8663"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}