Python | Two-Pointer, Sliding Window  &  Set Lookup
see the Successfully Accepted Submission\n\n# Code\npython3 []\nclass Solution:\n    def longestSquareStreak(self, nums: List[int]) -> int:\n        # Sort and 
[see the Successfully Accepted Submission](https://leetcode.com/problems/longest-square-streak-in-an-array/submissions/1435743887/?envType=daily-question&envId=2024-10-28)\n\n# Code\n```python3 []\nclass Solution:\n    def longestSquareStreak(self, nums: List[int]) -> int:\n        # Sort and convert list to a set for quick lookup\n        nums.sort()\n        num_set = set(nums)\n        \n        longest_streak = 0\n        \n        # Check for square streak starting with each number\n        for num in nums:\n            streak_length = 0\n            current = num\n            \n            # Continue as long as we find the square in the set\n            while current in num_set:\n                streak_length += 1\n                current = current * current  # Square the current number\n            \n            # Update longest streak if this one is the longest so far\n            if streak_length >= 2:\n                longest_streak = max(longest_streak, streak_length)\n        \n        # If no streak of at least 2 was found, return -1\n        return longest_streak if longest_streak >= 2 else -1\n\n```\n\n## Approach\n\n1. **Sort the Array**: \n   - Sorting `nums` will make it easier to find sequences where each element is the square of the previous one.\n\n2. **Hash Set for Fast Lookup**: \n   - Use a set to store elements from `nums` for constant-time lookup. This helps us quickly check if a number and its squares exist in `nums`.\n\n3. **Iterate and Check for Streaks**:\n   - For each number in `nums`, attempt to build a "square streak" starting from that number.\n   - Continuously check if the square of the current number exists in `nums`. If it does, add it to the streak, and update the current number to its square.\n   - Keep track of the longest streak found during this process.\n\n4. **Return the Result**:\n   - If the longest streak found is at least 2, return its length.\n   - If no valid streak of at least 2 is found, return `-1`.\n\n## Explanation of the Code\n\n1. **Sorting and Set Initialization**:\n   - `nums.sort()` and `num_set = set(nums)` allow us to search for square relationships more effectively.\n\n2. **Main Loop**:\n   - For each `num` in `nums`, we initialize a potential streak and use a `while` loop to keep squaring `current` as long as `current` exists in `num_set`.\n\n3. **Updating the Longest Streak**:\n   - If the length of the current streak (`streak_length`) is at least 2, we update `longest_streak` with the maximum length found so far.\n\n4. **Return Result**:\n   - We return the longest streak if it\u2019s at least 2; otherwise, we return `-1`.\n\n## Complexity Analysis\n\n- **Time Complexity**: \\(O(n \\log n)\\) due to sorting, where \\(n\\) is the length of `nums`. The subsequent operations are \\(O(n)\\).\n- **Space Complexity**: \\(O(n)\\) for storing `nums` in a set.\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']