Python 3   ||  11 lines,  mathematics, w/ brief comments   ||   T/S: 92% / 81%
Pretty much the same solution as others, except the set of potential squares has been pruned back to just those numbers n such that n%4 == 0 or n%4 == 1.  \nPyt
Pretty much the same solution as others, except the set of potential squares has been pruned back to just those numbers `n` such that `n%4 == 0` or `n%4 == 1`.  \n```Python3 []\nclass Solution:\n    def longestSquareStreak(self, nums: List[int]) -> int:\n\n        nums = sorted(set(nums))\n        cands = {n for n in nums if n%4 < 2}\n        ans = 0\n\n        for n in nums:\n            square = n*n\n            tally = 1\n\n            while square in cands:\n                cands.remove(square)\n                tally+= 1\n                square*= square\n\n            ans = max(ans, tally)\n   \n        return ans if ans > 1 else -1\n```\n```C++ []\nclass Solution {\npublic:\n    int longestSquareStreak(vector<int>& nums) {\n\n        sort(nums.begin(), nums.end());\n        nums.erase(unique(nums.begin(), nums.end()), nums.end());\n        \n        unordered_set<long long> cands;\n        for (int n : nums) {\n            if (n % 4 < 2) cands.insert(n); }\n\n        int ans = 0;\n        for (int n : nums) {\n            long long square = (long long)n * n;\n            int tally = 1;\n\n            while (cands.find(square) != cands.end()) {\n                cands.erase(square);\n                tally++;\n                square *= square;}\n\n            ans = max(ans, tally);}\n\n        return ans > 1 ? ans : -1;}\n};\n```\n[https://leetcode.com/problems/longest-square-streak-in-an-array/submissions/1290480480/](https://leetcode.com/problems/longest-square-streak-in-an-array/submissions/1290480480/)\n\nI could be wrong, but I think that time complexity is *O*(*N* log *N*) and space complexity is *O*(*N*), in which *N* ~ number of distinct integers in \'nums\'.\n
['C++', 'Python3']