java.lang.Object
g2501_2600.s2501_longest_square_streak_in_an_array.Solution

public class Solution extends Object
2501 - Longest Square Streak in an Array.<p>Medium</p> <p>You are given an integer array <code>nums</code>. A subsequence of <code>nums</code> is called a <strong>square streak</strong> if:</p> <ul> <li>The length of the subsequence is at least <code>2</code>, and</li> <li><strong>after</strong> sorting the subsequence, each element (except the first element) is the <strong>square</strong> of the previous number.</li> </ul> <p>Return <em>the length of the <strong>longest square streak</strong> in</em> <code>nums</code><em>, or return</em> <code>-1</code> <em>if there is no <strong>square streak</strong>.</em></p> <p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [4,3,6,16,8,2]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16].</p> <ul> <li> <p>4 = 2 * 2.</p> </li> <li> <p>16 = 4 * 4.</p> </li> </ul> <p>Therefore, [4,16,2] is a square streak.</p> <p>It can be shown that every subsequence of length 4 is not a square streak.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,3,5,6,7]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> There is no square streak in nums so return -1.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= nums.length <= 10<sup>5</sup></code></li> <li><code>2 <= nums[i] <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestSquareStreak

      public int longestSquareStreak(int[] nums)