Java Solution for Longest Square Streak In An Array Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the longest \u201Csquare streak\u201D in the array, which 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the longest \u201Csquare streak\u201D in the array, which is defined as a sequence where each successive element is the square of the previous element. This approach:\n\n1.\tUses a HashSet to store all unique elements in the array, allowing for constant-time lookups to check if the next number in the streak exists.\n2.\tIterates through each number, treating it as a potential starting point of a square streak and attempts to build a streak by repeatedly squaring the current element.\n3.\tTracks the longest streak found, provided that the streak length is at least 2, which qualifies it as a valid square streak.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tStore Unique Elements: Insert all numbers in a HashSet for efficient lookups.\n2.\tIterate Through Each Number:\n\t\u2022\tFor each number, initialize currentStreak to track the length of the streak starting from that number.\n\t\u2022\tUse a while loop to continue the streak as long as the square of the current element is found in the HashSet.\n\t\u2022\tUpdate current by squaring it and checking if it\u2019s within bounds (<= 10^5); if it exceeds this limit, break the loop to avoid overflow.\n3.\tUpdate Longest Streak: After evaluating each potential starting point, update longestStreak if the current streak is longer than the previous maximum.\n4.\tReturn Result: If longestStreak is less than 2, return -1 as no valid square streak was found; otherwise, return longestStreak.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n1.\tBuilding the HashSet: Takes O(n) time, where n is the number of elements in nums.\n2.\tIterating Through Each Element: For each element x in nums, we try to form a streak by repeatedly squaring it until it exceeds 10^5 or the square is no longer in the set.\n\t\u2022\tThe number of times we can square a number before it exceeds 10^5 is logarithmic, specifically O(log(log x)).\n\t\u2022\tThis results in an amortized complexity close to O(n), since only a limited number of streaks can be built within the constraints.\n3.\tTotal Complexity: The overall time complexity is O(n).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n1.\tHashSet Storage: We use a HashSet to store all unique elements in nums, requiring O(n) space.\n2.\tAuxiliary Variables: Minimal extra space is used for variables like currentStreak and current.\n\nOverall Space Complexity: O(n).\n\n# Code\n```java []\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        int longestStreak = 0;\n\n        // Create a Set to store all unique numbers from the input array\n        Set<Integer> uniqueNumbers = new HashSet<>();\n        for (int num : nums) {\n            uniqueNumbers.add(num);\n        }\n\n        // Iterate through each number in the input array\n        for (int startNumber : nums) {\n            int currentStreak = 0;\n            long current = startNumber;\n\n            // Continue the streak as long as we can find the next square in the set\n            while (uniqueNumbers.contains((int) current)) {\n                currentStreak++;\n                \n                // Calculate the square and check if it is within bounds\n                current = current * current;\n                if (current > 100000) break;  // Stop if the square exceeds the constraint\n            }\n\n            // Update the longest streak if necessary\n            longestStreak = Math.max(longestStreak, currentStreak);\n        }\n\n        // Return -1 if no valid streak found, otherwise return the longest streak\n        return longestStreak < 2 ? -1 : longestStreak;\n    }\n}\n```
['Java']