C++ Easy To Understand || Map and Sieve of Eratosthenes
Approach\n Describe your approach to solving the problem. \nCommented\n\n# Complexity\n- Time complexity: O( n log(log n) + m (log m) )\n Add your time complexi
# Approach\n<!-- Describe your approach to solving the problem. -->\nCommented\n\n# Complexity\n- Time complexity: O( n log(log n) + m (log m) )\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nwhere n is the input number and m is the number of primes found.\n\n- Space complexity:O(n/2 + n/log(n) + m)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nwhere n is the input number and m is the number of primes found.\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> findPrimePairs(int n) {\n        vector<vector<int>> res;\n        unordered_map<int,int> mp; // Make access fast\n        \n        primeSeive(n,mp); // Function call\n        \n        vector<int> prime; // For traversing prime\n        for(auto it : mp)\n            prime.push_back(it.first);\n        sort(prime.begin(),prime.end());  // To get res in sorted form\n    \n        \n        for(int i = 0; i<prime.size(); i++){\n            int req = n - prime[i]; // find if req exists in map and mp[req] > 0\n            \n            if(mp.find(req) != mp.end() && mp[req] > 0){\n                res.push_back({prime[i],req});\n                mp[prime[i]]--; // Make frequency of grouped prime[i] == 0 , as every key of map has freq = 1. \n                //due to unique prime numbers in prime array\n                // as for next iteration current prime[i] could become req so make mp[prime[i]]--\n            }\n        }\n        \n        return res;\n    }\n    \nprivate:\n    // sieve of eratosthenes\n    void primeSeive(int n, unordered_map<int,int>& mp){\n        vector<int> temp(n+1,0);\n        \n        for (long long i = 2; i <= n; i++)\n            if (temp[i] == 0)\n                for (long long j = i * i; j <= n; j = j + i)\n                    temp[j] = 1;\n           \n        for (int i = 2; i <= n; i++)\n            if (temp[i] == 0)\n                mp[i]++;\n    }\n};\n```
['C++']