93% run time
IntuitionThe problem requires finding the longest sequence of numbers in the input such that each number is the square of the previous one. My initial thought i
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires finding the longest sequence of numbers in the input such that each number is the square of the previous one. My initial thought is to use a brute force approach where for every number in the input, I keep checking if its square exists in the list, and continue forming a sequence. However, to make it efficient, I can use a set for O(1) lookups and avoid redundant calculations.



# Approach
<!-- Describe your approach to solving the problem. -->
Preprocessing: Convert the list nums into a set to allow for fast lookups.
Iterate Through the List:
For each number, check if its square exists in the set.
If it does, initialize a streak counter to 2 (current number and its square) and repeatedly square the number until no further square exists in the set.
Avoid Redundant Work: Numbers already part of a streak do not need to be processed again.
Track Maximum Streak: Update the maximum streak length encountered during the iterations.
Edge Cases:
If no valid streak is found, return -1 (default value).
Handle single-element or empty input lists appropriately.


# Complexity
- Time complexity:O(nlogm)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def longestSquareStreak(self, nums: List[int]) -> int:
        res = -1
        nums_set = set(nums)  # Convert to a set for O(1) lookups
        
        for num in nums:
            if num * num in nums_set:
                st = 2
                prev = num * num
                while prev:
                    if prev * prev in nums_set:
                        prev = prev * prev
                        st += 1
                    else:
                        break  # Exit the loop if no further square exists
                res = max(st, res)
        
        return res

```
['Python3']