[C++] Simple Solutions Explained, 100% Time (0ms), ~80% Space (22MB)
I am not sure if there is a single problem easier than this on the whole suite of 2000+ problems published so far, as this one is literally just about following
I am not sure if there is a single problem easier than this on the whole suite of 2000+ problems published so far, as this one is literally just about following instructions, but, hey, whatever.\n\nStill worth giving it a shot and in an interview you might still gain some points in the eyes of your interviewer if you mention that since modulo operations are rather expensive, we will not bother with them when the currently parsed argument is `> 9`.\n\nOther than that, we will have a main loop to parse the whole `nums`, `return` the first index `i` matching our condition (if any) and otherwise, exiting the loop, `return` `-1`.\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n    int smallestEqual(vector<int>& nums) {\n        for (int i = 0, len = nums.size(), n; i < len; i++) {\n            n = nums[i];\n            if (n < 10 && n == i % 10) return i;\n        }\n        return -1;\n    }\n};\n```\n\nEven better if we rule out the modulo operation altogether:\n\n```cpp\nconstexpr int mods[101] = {\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n    0, 1, 2, 3, 4, 5, 6, 7, 8, 9\n};\n\nclass Solution {\npublic:\n    int smallestEqual(vector<int>& nums) {\n        for (int i = 0, len = nums.size(), n; i < len; i++) {\n            n = nums[i];\n            if (n < 10 && n == mods[i]) return i;\n        }\n        return -1;\n    }\n};\n```\n\nIn constant space, maybe? Hell, yeah - this one gave me `0` ms \uD83D\uDC4D :\n\n```cpp\nclass Solution {\npublic:\n    int smallestEqual(vector<int>& nums) {\n        for (int i = 0, j = 0, len = nums.size(), n; i < len; i++, j++) {\n            n = nums[i];\n            if (j == 10) j -= 10;\n            if (n < 10 && n == j) return i;\n        }\n        return -1;\n    }\n};\n```
['Math', 'C', 'C++']