448. Find All Numbers Disappeared in an Array - C++


  • 0
    P

    Multiply every visited index's value with -1 and then traverse at end to get which all indexes' have positive values Push them in answer array and then return that array.
    '''
    class Solution {
    public:
        vector<int> findDisappearedNumbers(vector<int>& v) {
            for(int i=0;i<v.size();i++){
                if(v[abs(v[i])-1]>0)
                     v[abs(v[i])-1]*=-1;
                 }
        vector<int> ans;
        for(int i=0;i<v.size();i++){
             if(v[i]>0)
                 ans.push_back(i+1);
         }
        return ans;
         }
    };
    '''


Log in to reply
 

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