[LeetCode] The Two Sneaky Numbers of Digitville

3289. The Two Sneaky Numbers of Digitville

In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.

As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
vector<int> getSneakyNumbers(vector<int>& nums) {
int n = nums.size();
vector<int> res;
for(int i = 0; i < n; i++) {
int idx = nums[i] % n;
nums[idx] += n;
if(nums[idx] / n == 2) {
res.push_back(idx);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/15/PS/LeetCode/the-two-sneaky-numbers-of-digitville/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.