Leetcode Problem 2501. Longest Square Streak in an Array

2501. Longest Square Streak in an Array

Leetcode Solutions

HashMap with Sorting Approach

  1. Sort the input array nums.
  2. Initialize a HashMap squareStreak to store the streak lengths.
  3. Initialize a variable best to keep track of the longest streak.
  4. Iterate through the sorted array: a. For each number x, calculate its square root. b. If the square root is a perfect square and exists in squareStreak, update the streak length for x in the HashMap. c. Update best with the maximum streak length found so far.
  5. After the loop, return best if it's greater than 1, otherwise return -1.
UML Thumbnail

HashSet Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...