Find All missing numbers in array


  • 0
    J

    Idea is hear pretty neat. You just have to concentrate that size of array is n and numbers are also between 1->n so just look for missing indexed.

    vector<int> findDisappearedNumbers(vector<int>& nums) {
        for(int i=0; i<nums.size(); i++){
            int val = abs(nums[i]) - 1;
            if(nums[val] > 0)
                nums[val] = - nums[val]; // Negative to tell that visited that index
        }
        vector<int> returnV; // retur vector for storing missing elements
        
        for(int i=0; i<nums.size(); i++){
            if(nums[i] > 0){
                returnV.push_back(i+1); // Postive index shows that this number never appeared in array
            }
        }
        return returnV;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.