"Unlocking the Longest Square Streak: A Java Solution Using HashSet"
Intuition\nThe goal is to find the longest sequence in an array where each subsequent element is the square of the previous one. Given that the elements can be 
# Intuition\nThe goal is to find the longest sequence in an array where each subsequent element is the square of the previous one. Given that the elements can be in any order, sorting the array would help in building these sequences. However, since we need to check for squares repeatedly, using a HashSet will allow quick lookups for potential square values.\n\n\n\n# Approach\nStore Elements in a HashSet: Create a HashSet to store all elements of the array. This allows for O(1) average time complexity for lookups.\nIterate Through Each Element: For each number in the array, treat it as the starting point of a possible square streak.\nCalculate Squares: Use a while loop to keep squaring the current number and check if the squared value exists in the HashSet. If it does, increment the streak length.\nTrack Maximum Streak Length: Throughout the iteration, maintain a record of the longest streak found.\nReturn Result: Finally, return the maximum streak length found. If no valid streak is found, return -1.\n\n# Complexity\n- Time complexity:\nThe outer loop runs in O(n), where n is the length of the nums array. The inner while loop potentially runs multiple times based on how many squares exist in the set. Since we are squaring the number, it reduces quickly, making it logarithmic relative to the maximum streak length.\n\n- Space complexity:\nWe use a HashSet to store all elements from the array, leading to a space complexity that is linear with respect to the size of nums.\n# Code\n```java []\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        // Step 1: Use HashSet to store all elements in nums for quick lookup\n\n        HashSet<Integer>set = new HashSet<>();\n        for(int num :nums) set.add(num);\n\n                int max_=Integer.MIN_VALUE; // Initialize max streak length\n\n        // Step 2: Check each number in nums as the start of a possible square streak\n\n        for(int num: nums){\n            int len =1; // Initial length of streak\n            long square = num;  // Initialize square as num (using long to avoid overflow)\n\n            // Step 3: Continue squaring until out of bounds or square not found in set\n\n            while(square*square<=Integer.MAX_VALUE && set.contains((int)(square*square))){\n                len++;// Increment streak length\n                square = square*square;// Update square to its square\n            }\n\n            // Update max streak length\n            max_ = Math.max(len, max_);\n        }\n        // Step 4: Return result based on max streak found\n        return max_==1 ? -1: max_;\n    }\n}\n```
['Java']