✅ Clean and fully Explained Code🔥|| Step by Step breakdown
Intuition\nThe problem involves finding the longest "square streak" in a list of numbers, where each number in a streak is the square of the previous number. Ob
# Intuition\nThe problem involves finding the longest "square streak" in a list of numbers, where each number in a streak is the square of the previous number. Observing the problem, one might realize that if we can store and reference each square root\'s streak length, we can build up streaks efficiently by iterating over a sorted list of numbers.\n\n# Approach\n1. **Sort the Array**: Sorting the input list of numbers in ascending order ensures that smaller numbers and their squares appear before the larger squares in the list.\n2. **Initialize a Map**: Use a map `mp` to keep track of the streak length for each number.\n3. **Iterate Through Numbers**: For each number in the sorted list, calculate its integer square root.\n   - If the number is a perfect square (i.e., the square of the square root equals the number) and the square root exists in the map, we can extend a streak.\n   - Update the map entry for the current number to `mp[_sqrt] + 1` to indicate it\'s part of an extended streak.\n4. **Track the Longest Streak**: Update the result with the maximum streak length found during the iteration.\n\nFinally, return the result, which represents the longest square streak.\n\n# Complexity\n- Time complexity:  \n  Sorting takes $$O(n \\log n)$$, and iterating over the list to calculate the square root and update the map takes $$O(n)$$, making the overall complexity $$O(n \\log n)$$.\n\n- Space complexity:  \n  The map `mp` may store up to $$O(n)$$ entries (for each unique number in `nums`), giving a space complexity of $$O(n)$$.\n\n# Code Breakdown:\n- **Sorting**: Sorts the array in $$O(n \\log n)$$ to ensure that smaller squares are processed before larger ones.\n- **Map Setup**: A map `mp` tracks the length of streaks associated with each number.\n- **Square Check and Update**: For each number, we check if it\u2019s a perfect square and if its square root has a streak in the map, extending that streak if both conditions are met. Otherwise, we initialize a new streak.\n- **Result Calculation**: The maximum streak length is updated continuously, so at the end, `res` contains the length of the longest square streak.\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int longestSquareStreak(vector<int>& nums) {\n        map<int, int>mp;\n        sort(nums.begin(), nums.end());\n        int res = -1;\n        for(int num: nums) {\n            int _sqrt = sqrt(num);\n            if(_sqrt*_sqrt == num && mp.find(_sqrt)!=mp.end()) {\n                mp[num] = mp[_sqrt]+1;\n                res = max(res, mp[num]);\n            } else mp[num] = 1;\n        }\n        return res;\n    }\n};\n```\n```java []\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        Map<Integer, Integer> mp = new HashMap<>();\n        Arrays.sort(nums);\n        int res = -1;\n\n        for (int num : nums) {\n            int sqrt = (int) Math.sqrt(num);\n\n            if (sqrt * sqrt == num && mp.containsKey(sqrt)) {\n                mp.put(num, mp.get(sqrt) + 1);\n                res = Math.max(res, mp.get(num));\n            } else {\n                mp.put(num, 1);\n            }\n        }\n        return res;\n    }\n}\n```\n```python []\nclass Solution:\n    def longestSquareStreak(self, nums):\n        mp = {}\n        nums.sort()\n        res = -1\n\n        for num in nums:\n            sqrt = isqrt(num)\n\n            if sqrt * sqrt == num and sqrt in mp:\n                mp[num] = mp[sqrt] + 1\n                res = max(res, mp[num])\n            else:\n                mp[num] = 1\n\n        return res\n```\n```javascript []\nfunction longestSquareStreak(nums) {\n    const mp = new Map();\n    nums.sort((a, b) => a - b);\n    let res = -1;\n\n    for (const num of nums) {\n        const sqrt = Math.floor(Math.sqrt(num));\n\n        if (sqrt * sqrt === num && mp.has(sqrt)) {\n            mp.set(num, mp.get(sqrt) + 1);\n            res = Math.max(res, mp.get(num));\n        } else {\n            mp.set(num, 1);\n        }\n    }\n\n    return res;\n}\n```\n```golang []\nfunc longestSquareStreak(nums []int) int {\n    mp := make(map[int]int)\n    sort.Ints(nums)\n    res := -1\n\n    for _, num := range nums {\n        sqrt := int(math.Sqrt(float64(num)))\n\n        if sqrt*sqrt == num {\n            if val, exists := mp[sqrt]; exists {\n                mp[num] = val + 1\n                if mp[num] > res {\n                    res = mp[num]\n                }\n            } else {\n                mp[num] = 1\n            }\n        } else {\n            mp[num] = 1\n        }\n    }\n\n    return res\n}\n```\n```rust []\nimpl Solution {\n    pub fn longest_square_streak(nums: Vec<i32>) -> i32 {\n        let mut mp = HashMap::new();\n        let mut nums = nums.clone();\n        nums.sort();\n        let mut res = -1;\n\n        for &num in &nums {\n            let sqrt = (num as f64).sqrt() as i32;\n\n            if sqrt * sqrt == num {\n                if let Some(&streak) = mp.get(&sqrt) {\n                    let new_streak = streak + 1;\n                    mp.insert(num, new_streak);\n                    res = res.max(new_streak);\n                } else {\n                    mp.insert(num, 1);\n                }\n            } else {\n                mp.insert(num, 1);\n            }\n        }\n\n        res\n    }\n}\n```\n\n![image.png](https://assets.leetcode.com/users/images/65c3cf30-71a1-44a0-8743-d97b33945d0b_1729385077.052562.png)\n
['Array', 'Hash Table', 'Dynamic Programming', 'Sorting', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript']