Simple Java solution using HashSet.
Intuition\nMy initial thought is to leverage a set to efficiently check for the existence of squares of numbers. By iterating through the given array, I can com
# Intuition\nMy initial thought is to leverage a set to efficiently check for the existence of squares of numbers. By iterating through the given array, I can compute the square of each number and see if that square is also present in the array. The goal is to find the longest sequence of numbers such that each number\'s square is also part of the sequence.\n# Approach\n1. Set Creation: First, I\'ll create a Set<Integer> to store all the numbers from the input array for O(1) look-up times.\n\n2. Iteration: For each number in the array, I\'ll attempt to form a streak by checking if the square of the current number exists in the set.\n\n3. Streak Counting: I\'ll maintain a count of how many valid squares I can find in sequence. If I reach a number whose square isn\'t in the set, I\'ll break the loop.\n\n4. Update Maximum Streak: After checking each number, I\'ll update the maximum streak found so far.\n\n5. Return Result: Finally, if the longest streak is less than 2, I\'ll return -1, indicating no valid streak was found; otherwise, I\'ll return the longest streak.\n\n# Complexity\n- Time complexity: O(n), where n is the number of elements in the input array. Each number is processed once, and set operations (insert and contains) are average O(1).\n- Space complexity: O(n), for storing the elements of the input array in the set.\n\n# Code\n```java []\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        Set<Integer> set = new HashSet<>();\n        int longestStreak = 1;\n        for(int num : nums){\n            set.add(num);\n        }\n        for(int num : nums){\n            int current = 1;\n            boolean flag = true;\n            while(num<1000){\n                if(set.contains(num*num)){\n                    current++;\n                    num = num*num;\n                } else {\n                    break;\n                }\n            }\n         longestStreak = Math.max(longestStreak, current);\n        }\n        return longestStreak < 2 ? -1 : longestStreak;\n    }\n}\n```
['Java']