Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.6 kB
3
Indexable
Never
#include <stdio.h>

void merge(int A[], int B[], int n, int m, int *C) {
  int i = 0, j = 0, k = 0;
  while (i < n && j < m) {
    if (A[i] <= B[j]) {
      C[k] = A[i];
      i++;
    } else {
      C[k] = B[j];
      j++;
    }
    k++;
  }

  while (i < n) {
    C[k] = A[i];
    i++;
    k++;
  }

  while (j < m) {
    C[k] = B[j];
    j++;
    k++;
  }
}

int search(int arr[], int n, int k) {
  int low = 0, high = n - 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (arr[mid] == k) {
      return mid;
    } else if (arr[mid] < k) {
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return -1;
}

int main() {
  int n, k;

  scanf("%d", &n);

  int arr[n];
  for (int i = 0; i < n; i++) {

    scanf("%d", &arr[i]);
  }

  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        int temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }

  int n1 = n / 2;
  int n2 = n - n1;
  int A[n1];
  int B[n2];
  for (int i = 0; i < n1; i++) {
    A[i] = arr[i];
  }
  for (int i = n1; i < n; i++) {
    B[i - n1] = arr[i];
  }

  int C[n];
  merge(A, B, n1, n2, C);

  scanf("%d", &k);

  int pos_a = search(A, n1, k);
  int pos_b = search(B, n2, k);

  if (pos_a != -1) {
    printf("A\n");
    printf("%d\n",pos_a);
  } else if (pos_b != -1) {
    printf("B\n");
    printf("%d\n" , pos_b );
  } else {
    printf("-1");
  }

  int pos_c = search(C, n, k);
  if (pos_c != -1) {
    printf("%d\n",pos_c);
  }
  return 0;
}