Longest Square streak in an array- Easy Explanation and Unique Solution 👌🔜
Intuition\nThe count of the square streak should be atleast 2, hence if there is no such streak found, we\'ll return -1.\n\nTraverse through the whole array and
# Intuition\nThe count of the square streak should be atleast 2, hence if there is no such streak found, we\'ll return -1.\n\nTraverse through the whole array and add that element into the hashset for easy retrieval.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n**Initialization**:\n   1. Initialize the hashset of the type long since it also holds the square of the current element.\n   1. Intialize the maxcnt to -1 if no such streak of length 2 is found and intialize the value of n as number of elements in the array.\n\n    Set<Long> set=new HashSet<>();\n    int maxcnt=-1;\n    int n=nums.length;\n\n**Fill the Set**:\n     Iterate through the whole array and store each element of the array as type long in the hashset.\n\n    for(int num:nums)\n    {\n       set.add((long)num);\n    }\n\n**Find the square repeatedly**:\n   Iterate through the whole nums array, then at each iteration -> initialize the cnt as 1.\n   Find if the square of the current element is in hashset or not , if so change that square as current element and increase the cnt.\n   At the end of the loop , if the cnt is greater than 2, then that streak may be accepted and then replace the maximum length.\n\n    for(int num:nums)\n    {\n        long current=num;\n        int cnt=1;\n        while(set.contains(current*current))\n        {\n            current=current*current;\n            cnt++;\n        }\n        if(cnt>=2)\n            maxcnt=Math.max(cnt,maxcnt);\n            \n    }\n\n**Returning the value**:\n     Return the maxcnt which gives the longest square streak in an array.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- ***Time complexity***: **O(nlogM)**\n     n -> number of elements in the array.\n     M -> largest number in the streak of the square.\n\n**Initial Set Population**: **O(n)**.\n   We iterate over nums and insert each element into the hashset.Iterating each element will take **O(1) time** and for iterating n elements will take **O(n) time**.\n\n**Outer loop**: **O(logM).**\n   if every element leads to a lengthy streak (which is unlikely since numbers grow exponentially when squared), this could require \n**O(logM)** operations per element.\n\n**Finding Maximum Count**: **O(1)**.\n   This operation is a simple operation and it takes **O(1) time**.\n\n**Overall Time Complexity**: **O(nlogM )**.\n   Since the outerloop requires O(logM) operation per element and we have n elements.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- ***Space complexity***: **O(n)**.\n     n -> number of elements in the array.\n \n**Set Storage** :**O(n)**.\n    We use a HashSet to store up to n elements from nums, so the space complexity for the set is O(n).\n\n**Auxillary variables**: **O(1)**.\n    The program uses a **constant amount of extra space** for integers like current, cnt, and maxcnt, making this **O(1)**.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n          //Arrays.sort(nums);\n        Set<Long> set=new HashSet<>();\n          int maxcnt=-1;\n\n          int n=nums.length;\n          for(int num:nums)\n          {\n            set.add((long)num);\n          }\n          for(int num:nums)\n          {\n            long current=num;\n            int cnt=1;\n            while(set.contains(current*current))\n            {\n                current=current*current;\n                cnt++;\n            }\n            if(cnt>=2)\n              maxcnt=Math.max(cnt,maxcnt);\n            \n          }\n        return maxcnt;\n    }\n}\n```
['Array', 'Hash Table', 'Java']