Skip to main content

2501.js

/** ------------------------------------------------------------------------------
*
* 2501. Longest Square Streak in an Array
* Topics: Array
* https://leetcode.com/problems/longest-square-streak-in-an-array/description/?envType=daily-question&envId=2024-10-28
*
------------------------------------------------------------------------------ */
/**
* @param {number[]} nums
* @return {number}
*/
var longestSquareStreak = function (nums) {
const set = new Set(nums)
let max = -1

for (let num of nums) {
let count = 0
while (set.has(num)) {
num = num * num
count++
}

if (count > 1) {
max = Math.max(max, count)
}
}

return max
}