Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums. A subsequence of nums is called a square streak if:
2, andReturn the length of the longest square streak in nums, or return -1 if there is no square streak.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [4,3,6,16,8,2] Output: 3 Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16]. - 4 = 2 * 2. - 16 = 4 * 4. Therefore, [4,16,2] is a square streak. It can be shown that every subsequence of length 4 is not a square streak.
Example 2:
Input: nums = [2,3,5,6,7] Output: -1 Explanation: There is no square streak in nums so return -1.
Constraints:
2 <= nums.length <= 1052 <= nums[i] <= 105Problem summary: You are given an integer array nums. A subsequence of nums is called a square streak if: The length of the subsequence is at least 2, and after sorting the subsequence, each element (except the first element) is the square of the previous number. Return the length of the longest square streak in nums, or return -1 if there is no square streak. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Binary Search · Dynamic Programming
[4,3,6,16,8,2]
[2,3,5,6,7]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2501: Longest Square Streak in an Array
class Solution {
public int longestSquareStreak(int[] nums) {
Set<Long> s = new HashSet<>();
for (long x : nums) {
s.add(x);
}
int ans = -1;
for (long x : s) {
int t = 0;
for (; s.contains(x); x *= x) {
++t;
}
if (t > 1) {
ans = Math.max(ans, t);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2501: Longest Square Streak in an Array
func longestSquareStreak(nums []int) int {
s := map[int]bool{}
for _, x := range nums {
s[x] = true
}
ans := -1
for x := range s {
t := 0
for s[x] {
x *= x
t++
}
if t > 1 {
ans = max(ans, t)
}
}
return ans
}
# Accepted solution for LeetCode #2501: Longest Square Streak in an Array
class Solution:
def longestSquareStreak(self, nums: List[int]) -> int:
s = set(nums)
ans = -1
for x in nums:
t = 0
while x in s:
x *= x
t += 1
if t > 1:
ans = max(ans, t)
return ans
// Accepted solution for LeetCode #2501: Longest Square Streak in an Array
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2501: Longest Square Streak in an Array
// class Solution {
// public int longestSquareStreak(int[] nums) {
// Set<Long> s = new HashSet<>();
// for (long x : nums) {
// s.add(x);
// }
// int ans = -1;
// for (long x : s) {
// int t = 0;
// for (; s.contains(x); x *= x) {
// ++t;
// }
// if (t > 1) {
// ans = Math.max(ans, t);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2501: Longest Square Streak in an Array
function longestSquareStreak(nums: number[]): number {
const s = new Set(nums);
let ans = -1;
for (const num of nums) {
let x = num;
let t = 0;
while (s.has(x)) {
x *= x;
t += 1;
}
if (t > 1) {
ans = Math.max(ans, t);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.