3020. Find the Maximum Number of Elements in Subset Medium
1/**
2 * [3020] Find the Maximum Number of Elements in Subset
3 *
4 * You are given an array of positive integers nums.
5 * You need to select a <span data-keyword="subset">subset</span> of nums which satisfies the following condition:
6 *
7 * You can place the selected elements in a 0-indexed array such that it follows the pattern: [x, x^2, x^4, ..., x^k/2, x^k, x^k/2, ..., x^4, x^2, x] (Note that k can be be any non-negative power of 2). For example, [2, 4, 16, 4, 2] and [3, 9, 3] follow the pattern while [2, 4, 8, 4, 2] does not.
8 *
9 * Return the maximum number of elements in a subset that satisfies these conditions.
10 *
11 * <strong class="example">Example 1:
12 *
13 * Input: nums = [5,4,1,2,2]
14 * Output: 3
15 * Explanation: We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 2^2 == 4. Hence the answer is 3.
16 *
17 * <strong class="example">Example 2:
18 *
19 * Input: nums = [1,3,2,4]
20 * Output: 1
21 * Explanation: We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {3}, or {4}, there may be multiple subsets which provide the same answer.
22 *
23 *
24 * Constraints:
25 *
26 * 2 <= nums.length <= 10^5
27 * 1 <= nums[i] <= 10^9
28 *
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/find-the-maximum-number-of-elements-in-subset/
33// discuss: https://leetcode.com/problems/find-the-maximum-number-of-elements-in-subset/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38 pub fn maximum_length(nums: Vec<i32>) -> i32 {
39 0
40 }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_3020() {
51 }
52}
53
Back
© 2025 bowen.ge All Rights Reserved.