input: Write a Python function that takes a list of positive integers and returns the maximum number of elements in a subset that follows this pattern: [x, x2, x4, ..., xk/2, xk, xk/2, ..., x4, x2, x], where k is any non-negative power of 2. For instance, [2, 4, 16, 4, 2] and [3, 9, 3] fit this pattern, but [2, 4, 8, 4, 2] does not conform. The function should take into account the following constraints: the array's length is between 2 and 100,000 inclusive, and each integer in the array is between 1 and 1,000,000,000 inclusive.
  
  
  def maximum_length(nums: List[int]) -> int:
      """
      Given an array of positive integers, identify the largest subset of its elements that can be arranged to follow the pattern: [x, x2, x4, ..., xk/2, xk, xk/2, ..., x4, x2, x], where k can be any non-negative power of 2.
  
      Examples:
      >>> nums = [5,4,1,2,2]
      3
      >>> nums = [1,3,2,4]
      1
  
      Constraints:
      2 <= len(nums) <= 10^5
      1 <= nums[i] <= 10^9
      """
  
output: def maximum_length(nums: List[int]) -> int:
      from collections import defaultdict
  
      def extend_pattern(x):
          seq_forward, seq_backward = [x], {x}
          cur = x
          while cur % 2 == 0 and cur // 2 not in seq_backward:
              cur //= 2
              seq_forward.append(cur)
              seq_backward.add(cur)
          cur = x * 2
          while cur <= 1_000_000_000 and cur not in seq_backward:
              seq_forward.append(cur)
              seq_backward.add(cur)
              cur *= 2
          return seq_forward
  
      index_map = defaultdict(list)
      for ind, num in enumerate(nums):
          index_map[num].append(ind)
  
      max_len = 0
      used = [False] * len(nums)
  
      for num in sorted(index_map.keys(), reverse=True):
          if used[index_map[num][0]]:
              continue
          pattern = extend_pattern(num)
          curr_len = 0
          for val in pattern:
              if val in index_map:
                  for ind in index_map[val]:
                      if not used[ind]:
                          curr_len += 1
                          used[ind] = True
                          break
          max_len = max(max_len, curr_len)
  
      return max_len