✅C++ | ✅Hashmap & Sorting | ✅Easy Approach
Approach\nSort nums in decreasing order so that instead of finding square of elements, we can find square root of elements. Then, we store frequency of each ele
# Approach\nSort nums in decreasing order so that instead of finding square of elements, we can find square root of elements. Then, we store frequency of each element in the hashmap. Then, we find perfect square root of each element (if exists) repeatedly until that element becomes a prime number.\n\n# Time Complexity\nO(N*log(N))\n\n# Space Complexity\nO(N) \n\n# Code\n```\nclass Solution \n{\npublic:\n    int longestSquareStreak(vector<int>& nums) \n    {  \n        sort(nums.begin(), nums.end(), greater<int>());\n        \n        unordered_map<int, int> mp;\n        for(auto &it:nums)\n        {\n            mp[it]++;\n        }\n        \n        int count;\n        int maxi=1;\n        \n        for(int i=0; i<nums.size()-1; i++)\n        {\n            count=1;\n            int x=nums[i];\n            while(mp[sqrt(x)]>0)\n            {\n                int p=sqrt(x);\n                //Since sqrt(x) can be a decimal number so we need to check  perfect square condition\n                if(p*p==x) count++;\n                else break;\n                mp[sqrt(x)]--;\n                x = sqrt(x);\n            }\n            maxi = max(maxi, count);\n        }\n        return maxi==1?-1:maxi;\n    }\n};\n```
['Hash Table', 'Sorting', 'C++']