🌟 Beats 100.00% 👏 || Explained with example
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal of the function is to find the longest "square streak" in an array of integers
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal of the function is to find the longest "square streak" in an array of integers. A square streak begins with a number and follows the sequence of its squares until the square value no longer exists in the array. We are looking for the longest sequence among such square streaks. If the longest streak is 1 or fewer, we return -1 (as no square streak of length greater than 1 exists).\n\n# Approach\nConvert the array into a set to efficiently check the presence of elements.\nSort the unique elements to process them in increasing order.\nFor each unique number in the sorted array:\nStart counting a square streak by checking the square of the current number.\nKeep counting as long as each subsequent square exists in the set.\nTrack the maximum length of square streaks found.\nReturn the maximum length if it\u2019s greater than 1; otherwise, return -1.\n\n# Example\n![image.png](https://assets.leetcode.com/users/images/99103e47-6012-46ff-a28e-70c400c1716f_1730097329.1091988.png)\n\n# Complexity\n- Time complexity:O(NLOGN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\nvar longestSquareStreak = function(nums) {\n    let max=-1\n    let set = new Set(nums)\n    let setarr = Array.from(set.values())\n    setarr.sort((a,b)=>a-b)\n    for(let i=0;i<setarr.length;i++){\n        let curr = setarr[i]\n        let count=0\n        while(set.has(curr)){\n            set.delete(curr)\n            curr=curr**2\n            count++\n        }\n        max = Math.max(max,count)\n        \n    }\n    return max>1?max:-1\n};\n```\n```python []\nclass Solution(object):\n   \n       \n    def longestSquareStreak(self , nums):\n        max_len = -1\n        num_set = set(nums)\n        sorted_nums = sorted(num_set)\n    \n        for num in sorted_nums:\n            count = 0\n            curr = num\n            while curr in num_set:\n                num_set.remove(curr)\n                curr = curr ** 2\n                count += 1\n            \n            max_len = max(max_len, count)\n        \n        return max_len if max_len > 1 else -1\n    \n```\n```java []\nimport java.util.*;\n\npublic class Solution {\n    public int longestSquareStreak(int[] nums) {\n        int max = -1;\n        Set<Integer> set = new HashSet<>();\n        for (int num : nums) {\n            set.add(num);\n        }\n        List<Integer> setArr = new ArrayList<>(set);\n        Collections.sort(setArr);\n        \n        for (int i = 0; i < setArr.size(); i++) {\n            int curr = setArr.get(i);\n            int count = 0;\n            \n            while (set.contains(curr)) {\n                set.remove(curr);\n                curr = curr * curr;\n                count++;\n            }\n            \n            max = Math.max(max, count);\n        }\n        \n        return max > 1 ? max : -1;\n    }\n}\n\n```\n```c []\n#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n\nint compare(const void *a, const void *b) {\n    return (*(int *)a - *(int *)b);\n}\n\nbool set_contains(int *set, int setSize, int value) {\n    for (int i = 0; i < setSize; i++) {\n        if (set[i] == value) return true;\n    }\n    return false;\n}\n\nint longestSquareStreak(int *nums, int numsSize) {\n    int max = -1;\n    int set[numsSize];\n    int setSize = 0;\n\n    // Remove duplicates and add to set\n    for (int i = 0; i < numsSize; i++) {\n        bool exists = false;\n        for (int j = 0; j < setSize; j++) {\n            if (set[j] == nums[i]) {\n                exists = true;\n                break;\n            }\n        }\n        if (!exists) set[setSize++] = nums[i];\n    }\n\n    qsort(set, setSize, sizeof(int), compare);\n\n    for (int i = 0; i < setSize; i++) {\n        int curr = set[i];\n        int count = 0;\n        \n        while (set_contains(set, setSize, curr)) {\n            for (int j = 0; j < setSize; j++) {\n                if (set[j] == curr) {\n                    set[j] = -1; // Mark as removed\n                    break;\n                }\n            }\n            curr = curr * curr;\n            count++;\n        }\n        \n        if (count > max) max = count;\n    }\n    \n    return max > 1 ? max : -1;\n}\n\n```\n```c++ []\n#include <iostream>\n#include <vector>\n#include <unordered_set>\n#include <algorithm>\n\nclass Solution {\npublic:\n    int longestSquareStreak(std::vector<int>& nums) {\n        int max = -1;\n        std::unordered_set<int> numSet(nums.begin(), nums.end());\n        std::vector<int> sortedNums(numSet.begin(), numSet.end());\n        \n        std::sort(sortedNums.begin(), sortedNums.end());\n        \n        for (int num : sortedNums) {\n            int curr = num;\n            int count = 0;\n            \n            while (numSet.count(curr)) {\n                numSet.erase(curr);\n                curr *= curr;\n                count++;\n            }\n            \n            max = std::max(max, count);\n        }\n        \n        return max > 1 ? max : -1;\n    }\n};\n\n```
['Hash Table', 'Binary Search', 'Dynamic Programming', 'C', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']