LeetCode 2501: Longest Square Streak in an Array Solution

Master LeetCode problem 2501 (Longest Square Streak in an Array), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2501. Longest Square Streak in an Array

Problem Explanation

Explanation:

To solve this problem, we can iterate through each element in the array and consider it as the starting point of a potential square streak. We then try to build the square streak by looking for the square of the current element in the remaining elements. If we find the square, we continue this process until we can no longer find the next square in the sequence.

Algorithmic Idea:

  1. Initialize a variable longestStreak to keep track of the maximum square streak length found so far.
  2. Iterate through each element in the array.
  3. For each element num[i], try to build a square streak starting from num[i].
  4. Check if num[i] * num[i] exists in nums, if it does, increment the streak length by 1 and update the current element to the square found.
  5. Continue this process until we can no longer find the next square in the sequence.
  6. Update longestStreak if the current streak is longer.

Time Complexity:

The time complexity of this approach is O(n^2) where n is the length of the input array nums.

Space Complexity:

The space complexity of this approach is O(1) as we are using only a constant amount of extra space.

Solution Code

class Solution {
    public int longestSquareStreak(int[] nums) {
        int longestStreak = -1;
        for (int i = 0; i < nums.length; i++) {
            int curr = nums[i];
            int streak = 1;
            while (true) {
                int next = curr * curr;
                boolean found = false;
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] == next) {
                        streak++;
                        curr = nums[j];
                        found = true;
                        break;
                    }
                }
                if (!found) break;
            }
            longestStreak = Math.max(longestStreak, streak);
        }
        return longestStreak >= 2 ? longestStreak : -1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2501 (Longest Square Streak in an Array)?

This page provides optimized solutions for LeetCode problem 2501 (Longest Square Streak in an Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2501 (Longest Square Streak in an Array)?

The time complexity for LeetCode 2501 (Longest Square Streak in an Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2501 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2501 in Java, C++, or Python.

Back to LeetCode Solutions