input: from typing import List
  from collections import Counter
  
  def largest_subset_length(nums: List[int]) -> int:
      """
      Given an array of positive integers nums.
  
      Find a subset of nums that meets the following criteria:
  
      Arrange the chosen elements in a 0-indexed sequence that adheres to the pattern: 
      [x, x2, x4, ..., xk/2, xk, xk/2, ..., x4, x2, x] 
      (Here, k is any non-negative power of 2). For instance, [2, 4, 16, 4, 2] 
      and [3, 9, 3] fit the pattern, whereas [2, 4, 8, 4, 2] does not.
  
      Return the length of the largest subset that meets these criteria.
  
      Examples:
      >>> nums = [5,4,1,2,2]
      3
      >>> nums = [1,3,2,4]
      1
  
      Constraints:
      2 <= nums.length <= 10^5
      1 <= nums[i] <= 10^9
      """
output:     count = Counter(nums)
      max_length = 0
  
      for x in sorted(count):
          length = 0
          current = x
          valid = True
  
          # First half of the pattern: [x, x2, x4, ..., xk]
          while current in count:
              length += count[current]
              count[current] = 0
              current *= 2
  
          # Second half of the pattern: [xk/2, ..., x4, x2, x]
          current //= 4
          while current >= x and current in count:
              if count[current] == 0:
                  valid = False
                  break
              length += count[current]
              count[current] = 0
              current //= 2
  
          if valid:
              max_length = max(max_length, length)
  
      return max_length