2501. Longest Square Streak in an Array Medium

@problem@discussion
#Array#Hash Table#Binary Search#Dynamic Programming#Sorting



1/**
2 * [2501] Longest Square Streak in an Array
3 *
4 * You are given an integer array nums. A subsequence of nums is called a square streak if:
5 * 
6 * 	The length of the subsequence is at least 2, and
7 * 	after sorting the subsequence, each element (except the first element) is the square of the previous number.
8 * 
9 * Return the length of the longest square streak in nums, or return -1 if there is no square streak.
10 * 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.
11 *  
12 * <strong class="example">Example 1:
13 * 
14 * Input: nums = [4,3,6,16,8,2]
15 * Output: 3
16 * Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16].
17 * - 4 = 2 * 2.
18 * - 16 = 4 * 4.
19 * Therefore, [4,16,2] is a square streak.
20 * It can be shown that every subsequence of length 4 is not a square streak.
21 * 
22 * <strong class="example">Example 2:
23 * 
24 * Input: nums = [2,3,5,6,7]
25 * Output: -1
26 * Explanation: There is no square streak in nums so return -1.
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	2 <= nums.length <= 10^5
32 * 	2 <= nums[i] <= 10^5
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/longest-square-streak-in-an-array/
38// discuss: https://leetcode.com/problems/longest-square-streak-in-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn longest_square_streak(nums: Vec<i32>) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_2501() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.