Simply py,JAVA code using 3Approaches(HashMap+sort,Sort+set,set)!!!
\n# Problem understanding\n Describe your first thoughts on how to solve this problem. \n- Let\'s understand the problem first,what they are asking is a subsequ
\n# Problem understanding\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Let\'s understand the problem first,what they are asking is a *subsequence* of numbers which are *squares of on another* continously.\n```\nnums=[2,4,16,256]\n\n```\n- The longest streak is for `2` here which is `2*2(4)->4*4(16)->16*16(256)` which is `4` including `2`.\n- \n\n---\n\n# Approach-1:sets+sorting\n# Intuition\n- My idea here is to use `2-SETS` one to to store visited numbers\n`visited` and our normal `nums`(given) in a set as `num_set` or `checker`.\n- why `visited` ? I felt we are going to compute the same number more than once,if it is true for the smallest number say `2` in our above example then we get our ans (`4`) till `256`,then the computaion of `4`,`16` is useless.\n-  Because compuation of them is always going to lead to a smaller value of `maxlen`.\n- what I did is to choose an element and store it in `num` which is then *squared* each time and checked until it fails to exist in `num_set` or `checker`,while increasing `l` and adding `num*num` in `visited` to avoid future computation.\n- Sorting(`sort`) is must since I am traversing from small element,because starting from a large elment and then looking for even bigger number is pointless.\n\n\n# Complexity\n- Time complexity:$O(N*log(N))$\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```python3 []\nclass Solution:\n    def longestSquareStreak(self, nums: List[int]) -> int:\n        nums.sort()\n        nums_set=set(nums)\n        visited=set([])\n        print(nums)\n        maxl=0\n        for i in nums:\n            num=i\n            l=1\n            while num*num in nums_set and num*num not in visited:\n                visited.add(num*num)\n                num=num*num\n                l+=1\n            maxl=max(maxl,l)\n            #print(i,visited,maxl)\n            #print(\'------------------\')\n        return maxl if maxl>=2 else -1\n        \n```\n```JAVA []\n//This fails last two test cases but the same logic!!(90/92)\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        Arrays.sort(nums);\n        int maxlen=0,len=0;\n        Set<Integer> visited=new HashSet<>();\n        Set<Integer> checker=new HashSet<>();\n        for(int i:nums){\n            checker.add(i);\n        }\n        for(int n:nums){\n            int num=n;\n            len=1;\n            while(checker.contains(num*num) && !visited.contains(num*num)){\n                visited.add(num*num);\n                num=num*num;\n                len++;\n            }\n            maxlen=Math.max(maxlen,len);\n        }\n        return maxlen>=2?maxlen:-1;\n    }\n}\n```\n\n---\n\n# Approach-2:HashMap+sort\n# Intuition\n- This is a clever way to solve a problem.\n- We will `sort` as for the same reason stated above!\n- The `map` stores the previosly found *perfect squares\'s streak*\n- we will take square root of a current number `n` and store it in `sqroot` or `sqrt`.Magic happens here,if they are *perfect roots* and if they had any `prev` value in `map` which is `Dictionary or HashMap`,we increase the value(`val`) of stored `sqrt` and then map it from our current `num`.\n- else then we create *map(key,val)* for it in `map`\n```\nnums=[1,4,16,2,256,3,5]\n//after sort\nnums=[1,2,3,4,5,16,256]\n1->{1:1}\n2->{1:1,2:1} //sqrt(2)=1.414\n3->{1:1,2:1,3:1}\n4->{1:1,2:1,3:1,4:2} //sqrt(4)=2[2:1]....Hence maintains STREAK!!\n5->{1:1,2:1,3:1,4:2,5:1}\n16->{1:1,2:1,3:1,4:2,5:1,16:3} //sqrt(16)=4[4:2]....STREAK!!\n256->{1:1,2:1,3:1,4:2,5:1,16:3,256:4} //sqrt(256)=16[16:3]..STREAK!!\n```\n- This is optimised than previous code becuase `two loops are avoided!.`\n\n\n\n# Complexity\n- Time complexity:$O(N*log(N))$\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```python3 []\nclass Solution:\n    def longestSquareStreak(self, nums: List[int]) -> int:\n        nums.sort()\n        maxlen=0\n        map=dict()\n        for n in nums:\n            sqroot=int(math.sqrt(n))\n            if sqroot*sqroot==n and sqroot in map:\n                map[n]=map[sqroot]+1\n                maxlen=max(maxlen,map[n])\n            else:\n                map[n]=1\n        return maxlen if maxlen>=2 else -1\n        \n```\n```JAVA []\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        Arrays.sort(nums);\n        int maxlen=0,sqrt=0;\n        HashMap<Integer,Integer> map=new HashMap<>();\n        for(int n:nums){\n            sqrt=(int)Math.sqrt(n);\n            if(sqrt*sqrt==n && map.containsKey(sqrt)){\n                map.put(n,map.get(sqrt)+1);\n                maxlen=Math.max(maxlen,map.get(n));\n            }\n            else{\n                map.put(n,1);\n            }\n        }\n        return maxlen>=2?maxlen:-1;\n    }\n}\n```\n\n---\n\n\n# Approach-3:set(Beats100%)\n# Intuition\n- This is kinda similar to *approach-1* but here we never mind about `visited`,just  do the same logic and convert `nums` *list* into a `set(list)`\n- with help of a `set` DS(Data Structure),u can have $O(1)$\nlookup.\n\n# Complexity\n- Time complexity:$O(N)$\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```python3 []\nclass Solution:\n    def longestSquareStreak(self, nums: List[int]) -> int:\n        nums=set(nums)\n        maxlen=0\n        for n in nums:\n            num=n\n            l=1\n            while num*num in nums:\n                l+=1\n                num*=num\n            maxlen=max(maxlen,l)\n        return maxlen if maxlen>=2 else -1\n        \n```\n```JAVA []\n//This fails last two test cases but the same logic!!(90/92)\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        Arrays.sort(nums);\n        int maxlen=0,len=0,num;\n        Set<Integer> numss=new HashSet<>();\n        for(int i:nums){\n            numss.add(i);\n        }\n        for(int n:nums){\n            num=n;\n            len=1;\n            while(numss.contains(num*num)){\n                num*=num;\n                len++;\n            }\n            maxlen=Math.max(maxlen,len);\n        }\n        return maxlen>=2?maxlen:-1;\n    }\n}\n```\n[GithubCodes for same probelm with dry run!!](https://github.com/themysterysolver/LEET-AND-FUN/tree/main/2586-longest-square-streak-in-an-array)\n
['Array', 'Hash Table', 'Java', 'Python3']