Skip to content

2501. Longest Square Streak in an Array #759

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to identify the longest square streak in the nums array. A square streak is a subsequence where each subsequent element is the square of the previous element, and it must be at least two elements long.

Here's the solution approach:

  1. Use a Set for Quick Lookup:

    • Store the numbers in a set to quickly verify if an element's square is also in the array.
  2. Iterate Through the Array:

    • For each number in the array, try to build a square streak starting from that number.
    • Check if the square of the current number exists in the set and keep extending the streak until there’s no further square match.
  3. Track Maximum Length:

    • Track the maximum length of all possible square streaks encounte…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Oct 28, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Oct 28, 2024
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants