description: You are given an array a consisting of n distinct elements and an integer k. Each element in the array is a non-negative integer not exceeding 2^k-1.
  
  Let's define the XOR distance for a number x as the value of 
  
  $$$f(x) = min_{i = 1}^{n} min_{j = i + 1}^{n} |(a_i ⊕ x) - (a_j ⊕ x)|,$$$
  
  where ⊕ denotes [the bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
  
  For every integer x from 0 to 2^k-1, you have to calculate f(x).
  
  Input
  
  The first line contains two integers n and k (1 ≤ k ≤ 19; 2 ≤ n ≤ 2^k).
  
  The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^k-1). All these integers are distinct.
  
  Output
  
  Print 2^k integers. The i-th of them should be equal to f(i-1).
  
  Examples
  
  Input
  
  
  3 3
  6 0 3
  
  
  Output
  
  
  3 1 1 2 2 1 1 3 
  
  
  Input
  
  
  3 4
  13 4 2
  
  
  Output
  
  
  2 2 6 6 3 1 2 2 2 2 1 3 6 6 2 2 
  
  Note
  
  Consider the first example:
  
    * for x = 0, if we apply bitwise XOR to the elements of the array with x, we get the array [6, 0, 3], and the minimum absolute difference of two elements is 3; 
    * for x = 1, if we apply bitwise XOR to the elements of the array with x, we get the array [7, 1, 2], and the minimum absolute difference of two elements is 1; 
    * for x = 2, if we apply bitwise XOR to the elements of the array with x, we get the array [4, 2, 1], and the minimum absolute difference of two elements is 1; 
    * for x = 3, if we apply bitwise XOR to the elements of the array with x, we get the array [5, 3, 0], and the minimum absolute difference of two elements is 2; 
    * for x = 4, if we apply bitwise XOR to the elements of the array with x, we get the array [2, 4, 7], and the minimum absolute difference of two elements is 2; 
    * for x = 5, if we apply bitwise XOR to the elements of the array with x, we get the array [3, 5, 6], and the minimum absolute difference of two elements is 1; 
    * for x = 6, if we apply bitwise XOR to the elements of the array with x, we get the array [0, 6, 5], and the minimum absolute difference of two elements is 1; 
    * for x = 7, if we apply bitwise XOR to the elements of the array with x, we get the array [1, 7, 4], and the minimum absolute difference of two elements is 3. 
solution: #include <bits/stdc++.h>
  using namespace std;
  const int m = 22;
  int pow_two[m];
  const int maxk = 19;
  const int maxn = 7e5;
  int n, k;
  int a[maxn];
  vector<bool> bits_arr[maxn];
  void compute(int d, int l, int r, vector<int>& min_xor, vector<int>& max_xor,
               vector<int>& min_dist) {
    assert((r - l + 1) > 0);
    int ll = l, lr = -1, rl = -1, rr = r;
    for (int i = l; i <= r; i++) {
      if ((bits_arr[i][d] == 1) && (rl == -1))
        rl = i;
      else if (bits_arr[i][d] == 0)
        lr = i;
    }
    bool left_empty = (lr == -1), right_empty = (rl == -1);
    if (d == 0) {
      if (left_empty || right_empty) {
        min_xor[0] = max_xor[0] = ((left_empty) ? 1 : 0);
        max_xor[1] = min_xor[1] = ((left_empty) ? 0 : 1);
        min_dist[0] = min_dist[1] = ((r - l) > 0) ? 0 : INT_MAX;
      } else {
        min_xor[0] = min_xor[1] = 0;
        max_xor[0] = max_xor[1] = 1;
        min_dist[0] = min_dist[1] = (((!left_empty) && ((lr - ll) > 0)) ||
                                     ((!right_empty) && ((rr - rl) > 0)))
                                        ? 0
                                        : 1;
      }
    } else {
      int sz = pow_two[d];
      vector<int> left_min_xor(sz), left_max_xor(sz), left_min_dist(sz);
      vector<int> right_min_xor(sz), right_max_xor(sz), right_min_dist(sz);
      if (!left_empty)
        compute(d - 1, ll, lr, left_min_xor, left_max_xor, left_min_dist);
      if (!right_empty)
        compute(d - 1, rl, rr, right_min_xor, right_max_xor, right_min_dist);
      for (int x = 0; x < pow_two[d + 1]; x++) {
        bool msb_one = (x >= pow_two[d]);
        int y = (msb_one) ? (x - pow_two[d]) : x;
        min_dist[x] = INT_MAX;
        if (!left_empty) min_dist[x] = min(min_dist[x], left_min_dist[y]);
        if (!right_empty) min_dist[x] = min(min_dist[x], right_min_dist[y]);
        if ((!left_empty) && (!right_empty)) {
          int min_cross_diff =
              (msb_one) ? (pow_two[d] + left_min_xor[y] - right_max_xor[y])
                        : (pow_two[d] + right_min_xor[y] - left_max_xor[y]);
          min_dist[x] = min(min_dist[x], min_cross_diff);
        }
        min_xor[x] = INT_MAX;
        max_xor[x] = INT_MIN;
        if (!left_empty) {
          min_xor[x] = min(min_xor[x], (msb_one) ? (pow_two[d] + left_min_xor[y])
                                                 : (left_min_xor[y]));
          max_xor[x] = max(max_xor[x], (msb_one) ? (pow_two[d] + left_max_xor[y])
                                                 : (left_max_xor[y]));
        }
        if (!right_empty) {
          min_xor[x] =
              min(min_xor[x], (msb_one) ? (right_min_xor[y])
                                        : (pow_two[d] + right_min_xor[y]));
          max_xor[x] =
              max(max_xor[x], (msb_one) ? (right_max_xor[y])
                                        : (pow_two[d] + right_max_xor[y]));
        }
      }
    }
  }
  void sort_and_get_bits_arr() {
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
      vector<bool> b;
      int x = a[i];
      for (int j = 0; j < k; j++) {
        b.push_back((bool)(x % 2));
        x /= 2;
      }
      bits_arr[i] = b;
    }
  }
  int main() {
    for (int i = 0; i < m; i++)
      pow_two[i] = ((i == 0) ? 1 : (pow_two[i - 1] * 2));
    cin >> n >> k;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort_and_get_bits_arr();
    int sz = pow_two[k];
    vector<int> min_xor(sz), max_xor(sz), min_dist(sz);
    compute(k - 1, 0, n - 1, min_xor, max_xor, min_dist);
    for (int x = 0; x < pow_two[k]; x++) cout << min_dist[x] << ' ';
    cout << endl;
  }
  