Find Longest Square Streak in Sequence Using Set Lookup Optimization | Java | C++ | Video Solution
Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/U0qpuY6LGEs\n\n# Code\njava []\nclass Solution {\n    public int lo
# Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/U0qpuY6LGEs\n\n# Code\n```java []\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        Set<Long> unqNums = new HashSet<>();\n        for(var num : nums){\n            unqNums.add(num * 1l);\n        }\n        int maxSSLen = 0;\n        for(var num : nums){\n            int currSSLen = 1;\n            long currNum = num * 1l;\n            while(unqNums.contains(currNum * currNum)){\n                currSSLen++;\n                currNum = currNum * currNum;\n            }\n            maxSSLen = Math.max(maxSSLen, currSSLen);\n        }\n        return maxSSLen == 1 ? -1 :  maxSSLen;\n    }\n}\n```\n```cpp []\nclass Solution {\npublic:\n    int longestSquareStreak(vector<int>& nums) {\n        unordered_set<long long> unqNums;\n        for (auto num : nums) {\n            unqNums.insert(static_cast<long long>(num));\n        }\n        \n        int maxSSLen = 0;\n        for (auto num : nums) {\n            int currSSLen = 1;\n            long long currNum = static_cast<long long>(num);\n            while (unqNums.count(currNum * currNum)) {\n                currSSLen++;\n                currNum = currNum * currNum;\n            }\n            maxSSLen = max(maxSSLen, currSSLen);\n        }\n        \n        return maxSSLen == 1 ? -1 : maxSSLen;\n    }\n};\n\n```
['Array', 'Hash Table', 'C++', 'Java']