Leetcode 448 Solution

This article provides solution to leetcode question 448 (find-all-numbers-disappeared-in-an-array)

https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array

Solution

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        for (int i = 0; i < nums.size(); i++)
        {
            int num = nums[i];
            nums[abs(num) - 1] = -abs(nums[abs(num) - 1]);
        }

        vector<int> res;
        for (int i = 0; i < nums.size(); i++)
        {
            int num = nums[i];
            if (num > 0)
                res.push_back(i + 1);
        }

        return res;
    }
};