Easy Java Solution using Hashmap with explanation
\n\n# Approach\nThe basic idea is that if we need a subsequence in sorted order, why not sort the whole array and then fine subsequence.\nFor every sorted eleme
\n\n# Approach\nThe basic idea is that if we need a subsequence in sorted order, why not sort the whole array and then fine subsequence.\nFor every sorted element (descending), check if the element\'s square has always been visited (using hashmap), if yes, then add 1 to the value of the square\'s value in dp array and set the current dp value. If n, set the current dp value as 0.\neg. nums = [4,3,6,16,8,2]\n    sort the array in desc - [16,8,6,4,3,2]\n    dp = [0,0,0,0,0,0]\n    The first value will be always 0. Start with index=1;\n    Add 16 to hashmap along with its index. map.put(16,0)\n    i=1; 8*8 is not present in a map, so dp[1] = 0; map.put(8,1)\n    i=2; 6*6 is not present. dp[2]=0; map.put(6,2)\n    i=3;4*4 is present, so dp[3] = 1+ dp[16] = 1\nAnd so on..\n\nAt last return the maximum value from dp array + 1;\n\n# Complexity\n- Time complexity:\n(nlog(n))\n\n- Space complexity:\n<O(n)\n\n# Code\n```java []\nclass Solution {\n    public int longestSquareStreak(int[] nums) {\n        Arrays.sort(nums);\n        int n = nums.length;\n        Map<Integer,Integer> map = new HashMap<>();\n        int[] dp = new int[n];\n        dp[0]=0;\n        map.put(nums[n-1],0);\n        int max = 0;\n        int j = 1;\n        for (int i = n-2;i>=0;i--) {\n            if (map.containsKey(nums[i]*nums[i])) {\n                dp[j] = 1+dp[map.get(nums[i]*nums[i])];  \n            }else{dp[j] = 0;}\n            if (!map.containsKey(nums[i])) {\n                map.put(nums[i],j);\n            }\n            max = Math.max(dp[j],max);\n            j++;\n        }if (max==0) {return -1;}\n        return max+1;\n    }\n}\n```
['Array', 'Hash Table', 'Binary Search', 'Dynamic Programming', 'Sorting', 'Java']