Given an array of integers where 1 ≤ a[i] ≤ n (n = size of the array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]
Approach:
C++
#include <bits/stdc++.h>using namespace std;vector<int> findDisappearedNumbers(vector<int> &nums){sort(nums.begin(), nums.end());vector<int> res;set<int> st;for (int i = 0; i < nums.size(); i++)st.insert(nums[i]);for (int i = 1; i <= nums.size(); i++)if (st.find(i) == st.end())res.push_back(i);return res;}int main(){vector<int> nums = {4, 3, 2, 7, 8, 2, 3, 1};vector<int> res = findDisappearedNumbers(nums);cout << "[";for (int i = 0; i < res.size(); i++){cout << res[i];if (i != res.size() - 1)cout << ", ";}cout << "]";return 0;}
No comments:
Post a Comment