✅ C++ || 2 DP solutions || 4 Lines || Recursive + Iterative || Faster 94%
Intuition\n- While traversing from backward, if current items i\'s square ( i * i ) does not exist store count 0 in DP[i]\n- If  square ( i * i ) exists in DP, 
**Intuition**\n- While traversing from backward, if current items `i`\'s `square ( i * i )` does not exist store count 0 in `DP[i]`\n- If  `square ( i * i )` exists in DP, store `DP[i] =  DP[i * i] + 1`\n- Keep tracking the `max_value` for `DP[i]`\n- Return `max_value + 1` if `max_value != 0`, otherwise -1\n\n**Iterative Approach** ( Accepted  \u2705 94.32 % )\n```c++\nclass Solution {\npublic:\n    int longestSquareStreak(vector<int>& nums, int res = 0) {\n        sort(nums.begin(), nums.end());\n        unordered_map<long long, int> dp;\n        for (int i = nums.size() - 1; i >= 0; i--) {\n            long long curr = nums[i], next = curr * curr;\n            dp[curr] = dp.count(next) ? dp[next] + 1: 0;\n            res = max(res, dp[curr]);\n        }\n        return res ? res + 1: -1;\n    }\n};\n```\n\n**Previous Attempt: Recursive Approach** ( Gets TLE \u274C )\n```c++\nclass Solution {\npublic:\n    int longestSquareStreak(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        auto gen_key = [](long long i, long long j) { return to_string(i) + "-" + to_string(j); };\n        unordered_map<string, long long> cache;\n        \n        function<long long(long long, long long)> go = \n            [&](auto idx, auto prev) -> long long {\n            if (idx == nums.size()) return 0;\n            \n            auto key = gen_key(idx, prev);\n            if (cache.count(key)) return cache[key];\n            \n            long long res = go(idx + 1, nums[idx]);\n            if (prev) res = max(res, go(idx + 1, prev));\n            if (prev * prev != (long long) nums[idx]) return res;\n            return cache[key] = max(res, 1 + go(idx + 1, nums[idx]));\n        };\n        \n        auto res =  go(0LL, 0LL);\n        return res ? res + 1: -1;\n    }\n};\n```\n\n**For more solutions, check out this \uD83C\uDFC6  [GITHUB REPOSITORY](https://github.com/MuhtasimTanmoy/playground) with over 1500+ solutions.**
['Dynamic Programming', 'Recursion']