Find the Maximum Number of Elements in Subset

You are given an array of positive integers nums.

You need to select a subset of nums which satisfies the following condition:

  • 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], where k can be any non-negative power of 2.

Return the maximum number of elements in a subset that satisfies these conditions.

Example 1
Inputnums = [5,4,1,2,2]
Output3
We can select the subset {4,2,2}, which can be placed as [2,4,2], following the pattern because 2^2 == 4.
Example 2
Inputnums = [1,3,2,4]
Output1
We can select the subset {1}, which can be placed as [1], and other single-element subsets would also give the same answer.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate