✔ C++ HashMap and Brute force easy solutions
Hey all..\nI have solved this in cpp using 2 methods:- Hashmap and brute force.\nIf you have any doubts be free to ask..\nand please upvote if you understood or
Hey all..\nI have solved this in cpp using 2 methods:- Hashmap and brute force.\nIf you have any doubts be free to ask..\nand please upvote if you understood or found it useful :)\n\nThank you..\n\n```\n  Method 1:- Using Hashmap\n  TC = O( N^2) But more efficient than brute force\n  SC = O(N)\n  \n  \nclass Solution {\npublic:\n    int countPairs(vector<int>& nums, int k) {\n        // Map of int and vector<int> to store the frequency and the indices of frequencies.\n        unordered_map<int,vector<int>> m;\n       \n        int cnt=0,n=nums.size();\n        \n\t\t// Traversing the vector to insert or find in the map\n        for(int i=0;i<n;i++){\n\t\t    // If the element is already in the map\n            if(m.find(nums[i])!=m.end()){\n\t\t\t\t// For all indices of the element discovered\n                for(auto a:m[nums[i]]){\n\t\t\t\t\t// If products remainder is zero\n                    if((a*i)%k==0){\n                        cnt++;\n                    }\n                }\n            }\n            // Inserting into the map to multiply for next found same elements.\n            m[nums[i]].push_back(i);\n            \n        }\n        return cnt;\n    }\n};\n\nMethod 2:- Brute force\nTC = O(N^2)\nSC=  O(1)\n\n\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums, int k) {\n  \n        int cnt=0,n=nums.size();\n\t\tfor(int i=0;i<n;i++){\n\t\t     for(int j=i+1;j<n;j++){\n\t\t\t\t if(nums[i] == nums[j] and (i*j)%k==0){\n\t\t\t\t\tcnt++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn cnt;\n    }\n};\n```
['C']