prithivMLmods/Coder-Stat huggingface dataset:
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int i, n, j, k, o, count = 0;
  int a[10001];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int left = 0, right = n - 1;
  int cur = n / 2;
  scanf("%d", &k);
  for (i = 0; i < k; i++) {
    scanf("%d", &o);
    left = 0, right = n - 1;
    while (1) {
      if (left == right - 1 || left == right) {
        if (a[left] == o || a[right] == o)
          count++;
        break;
      }
      if (o == a[cur] || o == a[cur + 1]) {
        count++;
        break;
      }
      if (o > a[cur]) {
        left = cur;
        cur = (left + right) / 2;
      }
      if (o < a[cur]) {
        right = cur;
        cur = (left + right) / 2;
      }
    }
  }
  printf("%d\n", count);
}


Fixed submission:
#include <stdio.h>
int main() {
  int i, n, j, k, o, count = 0;
  int a[1000001];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int left = 0, right = n - 1;
  int cur = n / 2;
  scanf("%d", &k);
  for (i = 0; i < k; i++) {
    scanf("%d", &o);
    left = 0, right = n - 1;
    while (1) {
      if (left == right - 1 || left == right) {
        if (a[left] == o || a[right] == o)
          count++;
        break;
      }
      if (o == a[cur] || o == a[cur + 1]) {
        count++;
        break;
      }
      if (o > a[cur]) {
        left = cur;
        cur = (left + right) / 2;
      }
      if (o < a[cur]) {
        right = cur;
        cur = (left + right) / 2;
      }
    }
  }
  printf("%d\n", count);
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

int main(int argc, char const *argv[]) {
  int n, q, target;
  int findCount = 0; // ???????????£?????°????????°
  int inputSequence[10000];

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &inputSequence[i]);
  }

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &target);
    int begin = 0, end = n - 1;
    do {
      int current = (begin + end) / 2;
      if (inputSequence[current] == target) {
        findCount++;
        break;
      }
      // ?????????????????§????????????????????????
      else if (inputSequence[current] < target) {
        begin = current + 1;
      }
      // ????????????????°?????????????????????????
      else if (inputSequence[current] > target) {
        end = current - 1;
      }
    } while (begin <= end); // ????????¨????°??????\???????????£????????????
  }

  printf("%d\n", findCount);
  return 0;
}

Fixed submission:
#include <cstdio>

int main(int argc, char const *argv[]) {
  int n, q, target;
  int findCount = 0; // ???????????£?????°????????°
  int inputSequence[100000];

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &inputSequence[i]);
  }

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &target);
    int begin = 0, end = n - 1;
    do {
      int current = (begin + end) / 2;
      if (inputSequence[current] == target) {
        findCount++;
        break;
      }
      // ?????????????????§????????????????????????
      else if (inputSequence[current] < target) {
        begin = current + 1;
      }
      // ????????????????°?????????????????????????
      else if (inputSequence[current] > target) {
        end = current - 1;
      }
    } while (begin <= end); // ????????¨????°??????\???????????£????????????
  }

  printf("%d\n", findCount);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {

  int n, a[10000], m, b, result = 0, i, j;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> b;
    for (j = 0; j < n; j++)
      if (b == a[j]) {
        result++;
        break;
      }
  }

  cout << result << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {

  int n, a[100000], m, b, result = 0, i, j;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> b;
    for (j = 0; j < n; j++)
      if (b == a[j]) {
        result++;
        break;
      }
  }

  cout << result << endl;

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int search(int);

int n, S[100000];

int main() {

  int q, T[50000], counter = 0;

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];

  cin >> q;
  for (int i = 0; i < q; i++)
    cin >> T[i];

  sort(S, S + n);

  for (int i = 0; i < q; i++)
    counter += search(T[i]);

  cout << counter << endl;

  return 0;
}

int search(int t) {

  int left = 0;
  int right = n;

  while (left <= right) {
    int mid = (left + right) / 2;
    if (S[mid] == t) {
      return 1;
    } else if (t < S[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int search(int);

int n, S[100000];

int main() {

  int q, T[50000], counter = 0;

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];

  cin >> q;
  for (int i = 0; i < q; i++)
    cin >> T[i];

  sort(S, S + n);

  for (int i = 0; i < q; i++)
    counter += search(T[i]);

  cout << counter << endl;

  return 0;
}

int search(int t) {

  int left = 0;
  int right = n;

  while (left < right) {
    int mid = (left + right) / 2;
    if (S[mid] == t) {
      return 1;
    } else if (t < S[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int a[10000], b[10000];
int binary(int l, int r, int c) {
  while (l <= r) {
    int p = (l + r) / 2, x, y;
    if (a[p] == c)
      return 1;
    else if (c < a[p])
      r = p - 1;
    else
      l = p + 1;
  }
  return 0;
}

int ar(int n) {
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    for (int j = 0; j < i; j++)
      if (b[i] == b[j])
        i--, n--;
  }
  return n;
}

int main() {
  int p, q, n = 0;
  cin >> p;
  for (int i = 0; i < p; i++)
    cin >> a[i];
  cin >> q, q = ar(q);
  for (int i = 0; i < q; i++)
    n += binary(0, p - 1, b[i]);
  cout << n << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int a[100000], b[100000];
int binary(int l, int r, int c) {
  while (l <= r) {
    int p = (l + r) / 2, x, y;
    if (a[p] == c)
      return 1;
    else if (c < a[p])
      r = p - 1;
    else
      l = p + 1;
  }
  return 0;
}

int ar(int n) {
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    for (int j = 0; j < i; j++)
      if (b[i] == b[j])
        i--, n--;
  }
  return n;
}

int main() {
  int p, q, n = 0;
  cin >> p;
  for (int i = 0; i < p; i++)
    cin >> a[i];
  cin >> q, q = ar(q);
  for (int i = 0; i < q; i++)
    n += binary(0, p - 1, b[i]);
  cout << n << endl;

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int n, S[10010], q, key, cnt = 0;

int search() {
  int left = 0, right = n, med;
  while (left < right) {
    med = (left + right) / 2;
    if (S[med] == key)
      return 1;
    else if (S[med] < key)
      left = med + 1;
    else
      right = med;
  }
  return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    cnt += search();
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int n, S[100010], q, key, cnt = 0;

int search() {
  int left = 0, right = n, med;
  while (left < right) {
    med = (left + right) / 2;
    if (S[med] == key)
      return 1;
    else if (S[med] < key)
      left = med + 1;
    else
      right = med;
  }
  return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    cnt += search();
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int binSearch(int A[], int n, int key) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    else if (A[mid] < key)
      left = mid + 1; //+1???????????¨???????????§?????£??????
    else
      right = mid; // left<right
  }
  return 0;
}

int main() {
  int n, sum = 0, q, S[10000], T[50000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T[i];
    if (binSearch(S, n, T[i]))
      sum++;
  }
  cout << sum << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int binSearch(int A[], int n, int key) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    else if (A[mid] < key)
      left = mid + 1; //+1???????????¨???????????§?????£??????
    else
      right = mid; // left<right
  }
  return 0;
}

int main() {
  int n, sum = 0, q, S[100000], T[50000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T[i];
    if (binSearch(S, n, T[i]))
      sum++;
  }
  cout << sum << endl;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>

using namespace std;

int binSearch(int A[], int n, int key) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    if (key < A[mid])
      right = mid;
    if (key > A[mid])
      left = mid + 1;
  }
  return 0;
}

int main(int argc, char const *argv[]) {
  int i, n, q, A[10001], key, sum = 0;

  cin >> n;
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  cin >> q;
  for (int i = 0; i < q; ++i) {
    scanf("%d", &key);
    sum += binSearch(A, n, key);
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>

using namespace std;

int binSearch(int A[], int n, int key) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    if (key < A[mid])
      right = mid;
    if (key > A[mid])
      left = mid + 1;
  }
  return 0;
}

int main(int argc, char const *argv[]) {
  int i, n, q, A[100001], key, sum = 0;

  cin >> n;
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  cin >> q;
  for (int i = 0; i < q; ++i) {
    scanf("%d", &key);
    sum += binSearch(A, n, key);
  }
  cout << sum << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;

int main() {
  int n, p, S[100000], T[100000];
  bool exist;
  cin >> n;
  rep(i, n) cin >> S[i];
  cin >> p;
  rep(i, p) cin >> T[i];

  int h;
  rep(i, n) {
    int ch = i;
    REP(j, i, n) {
      if (S[ch] > S[j])
        ch = j;
    }
    h = S[ch];
    S[ch] = S[i];
    S[i] = h;
  }

  int count = 0;

  rep(i, p) {
    int lef = 0, rig = n;
    exist = false;
    while (lef < rig) {
      int index = (lef + rig) / 2;
      if (T[i] == S[index]) {
        exist = true;
        break;
      } else if (T[i] < S[index]) {
        rig = index;
      } else {
        lef = index + 1;
      }
    }
    if (exist) {
      count++;
    }
  }
  cout << count << endl;
}

Fixed submission:
#include <iostream>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;

int main() {
  int n, p, S[100000], T[100000];
  bool exist;
  cin >> n;
  rep(i, n) cin >> S[i];
  cin >> p;
  rep(i, p) cin >> T[i];

  int count = 0;

  rep(i, p) {
    int lef = 0, rig = n;
    exist = false;
    while (lef < rig) {
      int index = (lef + rig) / 2;
      if (T[i] == S[index]) {
        exist = true;
        break;
      } else if (T[i] < S[index]) {
        rig = index;
      } else {
        lef = index + 1;
      }
    }
    if (exist) {
      count++;
    }
  }
  cout << count << endl;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int BSearch(int x, int A[], int N) {
  int left = 0;
  int right = N;

  while (left <= right) {
    int mid = (left + right) / 2;

    if (A[mid] == x) {
      return mid;
    } else if (x < A[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return -1;
}

int main() {
  int n;
  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  int q;
  cin >> q;
  int T[q];
  for (int i = 0; i < q; i++) {
    cin >> T[i];
  }

  int ans = 0;

  for (int i = 0; i < q; i++) {
    if (BSearch(T[i], S, n) != -1) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int BSearch(int x, int A[], int N) {
  int left = 0;
  int right = N;

  while (left < right) {
    int mid = (left + right) / 2;

    if (A[mid] == x) {
      return mid;
    } else if (x < A[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return -1;
}

int main() {
  int n;
  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  int q;
  cin >> q;
  int T[q];
  for (int i = 0; i < q; i++) {
    cin >> T[i];
  }

  int ans = 0;

  for (int i = 0; i < q; i++) {
    if (BSearch(T[i], S, n) != -1) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool binarySearch(vector<int> vi, int t) {
  int right = vi.size() - 1;
  int left = 0;
  bool flg = true;
  while (flg) {
    if (right == left)
      flg = false;
    int center = (left + right) / 2;
    if (vi[center] < t) {
      left = center;
    } else if (vi[center] > t) {
      right = center;
    } else {
      return true;
    }
  }
  return false;
}

int main() {
  vector<int> vi;
  int n, q;
  int count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    vi.push_back(s);
  }
  sort(vi.begin(), vi.end());
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    count += binarySearch(vi, t);
  }
  cout << count << endl;
  return 0;
};


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool binarySearch(vector<int> vi, int t) {
  int right = vi.size() - 1;
  int left = 0;
  bool flg = true;
  while (flg) {
    if (right == left)
      flg = false;
    int center = (left + right) / 2;
    if (vi[center] < t) {
      left = center + 1;
    } else if (vi[center] > t) {
      right = center;
    } else {
      return true;
    }
  }
  return false;
}

int main() {
  vector<int> vi;
  int n, q;
  int count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    vi.push_back(s);
  }
  sort(vi.begin(), vi.end());
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    count += binarySearch(vi, t);
  }
  cout << count << endl;
  return 0;
};
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int s[100000], n;

int binsearch(int key) {
  int left = 0, right = n, mid = (left + right) / 2;
  while (left < right) {
    if (s[mid] == key)
      return 1;
    else if (s[mid] > key) {
      right = mid;
      mid = (left + right) / 2;
    } else if (s[mid] < key) {
      left = mid;
      mid = (left + right) / 2;
    }
  }
  return 0;
}

int main() {
  int m, count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    count += binsearch(t);
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int s[100000], n;

int binsearch(int key) {
  int left = 0, right = n, mid = (left + right) / 2;
  while (left < right) {
    if (s[mid] == key)
      return 1;
    else if (s[mid] > key) {
      right = mid;
      mid = (left + right) / 2;
    } else if (s[mid] < key) {
      left = mid + 1;
      mid = (left + right) / 2;
    }
  }
  return 0;
}

int main() {
  int m, count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    count += binsearch(t);
  }
  cout << count << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int n, q;
  cin >> n;
  int *S = new int[n];
  int number;
  for (int i; i < n; ++i) {
    cin >> number;
    S[i] = number;
  }
  cin >> q;
  int *T = new int[q];
  for (int i = 0; i < q; ++i) {
    cin >> number;
    T[i] = number;
  }
  int cnt = 0;

  for (int i = 0; i < q; ++i) {
    int left = 0, right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (S[mid] == T[i]) {
        cnt++;
        break;
      } else if (S[mid] > T[i]) {
        right = mid;
      } else if (S[mid] < T[i]) {
        left = mid;
      }
    }
  }

  cout << cnt << "\n";
  delete[] T;
  delete[] S;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n, q;
  cin >> n;
  int *S = new int[n];
  int number;
  for (int i; i < n; ++i) {
    cin >> number;
    S[i] = number;
  }
  cin >> q;
  int *T = new int[q];
  for (int i = 0; i < q; ++i) {
    cin >> number;
    T[i] = number;
  }
  int cnt = 0;

  for (int i = 0; i < q; ++i) {
    int left = 0, right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (S[mid] == T[i]) {
        cnt++;
        break;
      } else if (S[mid] > T[i]) {
        right = mid;
      } else if (S[mid] < T[i]) {
        left = mid + 1;
      }
    }
  }

  cout << cnt << "\n";
  delete[] T;
  delete[] S;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fori(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dX[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          dY[8] = {1, 1, 0, -1, -1, -1, 0, 1};
struct XY {
  int x;
  int y;
};

/*
(char)'1' - (int)48 = (int)1;


*/

int main() {
  int n, q;
  vector<int> s;
  cin >> n;
  int j;
  rep(i, n) {
    cin >> j;
    s.push_back(j);
  }
  cin >> q;
  int ans = 0;
  rep(i, q) {
    cin >> j;
    int l = 0, r = n;
    while (l != r) {
      bool ri = true;
      if (s[(l + r) / 2] > j) {
        r = (l + r) / 2 - 1;
      } else if (s[(l + r) / 2] < j) {
        l = (l + r) / 2 + 1;
      } else
        break;
    }
    if (s[(l + r) / 2] == j)
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fori(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dX[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          dY[8] = {1, 1, 0, -1, -1, -1, 0, 1};
struct XY {
  int x;
  int y;
};

/*
(char)'1' - (int)48 = (int)1;


*/

int main() {
  int n, q;
  vector<int> s;
  cin >> n;
  int j;
  rep(i, n) {
    cin >> j;
    s.push_back(j);
  }
  cin >> q;
  int ans = 0;
  rep(i, q) {
    cin >> j;
    int l = 0, r = n;
    if (binary_search(s.begin(), s.end(), j))
      ans++;
  }
  cout << ans << endl;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  ALDS1_4_B
//
//  Created by ptyuan on 2018/10/23.
//  Copyright © 2018 YY. All rights reserved.
//

#include <cstdio>
#include <iostream>
static const int kNotFound = -1;

int binarySearch(int *array, int n, int key) {
  int low = 0;
  int hight = n - 1;

  while (low <= hight) {
    int middle = (low + hight) * 0.5;
    int middleValue = array[middle];
    if (middleValue == key) {
      return middle;
    } else if (key > middleValue) {
      low = middle + 1;
    } else {
      hight = middle - 1;
    }
  }

  return kNotFound;
}

int main(int argc, const char *argv[]) {
  int n = 0;
  int s[10001] = {0};
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", s + i);
  }

  int q = 0;
  int sum = 0;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int temp = 0;
    scanf("%d", &temp);
    if (kNotFound != binarySearch(s, n, temp)) {
      sum++;
    }
  }
  printf("%d\n", sum);
  return 0;
}


Fixed submission:
//
//  main.cpp
//  ALDS1_4_B
//
//  Created by ptyuan on 2018/10/23.
//  Copyright © 2018 YY. All rights reserved.
//

#include <cstdio>
#include <iostream>
static const int kNotFound = -1;

int binarySearch(int *array, int n, int key) {
  int low = 0;
  int hight = n - 1;

  while (low <= hight) {
    int middle = (low + hight) * 0.5;
    int middleValue = array[middle];
    if (middleValue == key) {
      return middle;
    } else if (key > middleValue) {
      low = middle + 1;
    } else {
      hight = middle - 1;
    }
  }

  return kNotFound;
}

int main(int argc, const char *argv[]) {
  int n = 0;
  int s[100010] = {0};
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", s + i);
  }

  int q = 0;
  int sum = 0;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int temp = 0;
    scanf("%d", &temp);
    if (kNotFound != binarySearch(s, n, temp)) {
      sum++;
    }
  }
  printf("%d\n", sum);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <iostream>

int main() {
  int n, q;
  std::cin >> n;
  int *S = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  std::cin >> q;
  int *T = new int[q];
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
  }

  int cnt = 0;
  for (int i = 0; i < q; i++) {
    int head = 0;
    int tail = n - 1;
    int j = (head + tail) / 2;
    while (1) {
      if (S[j] == T[i]) {
        cnt++;
        break;
      } else if (head - tail == 1) {
        break;
      } else if (S[j] > T[i]) {
        tail = j;
        j = (head + tail) / 2;
      } else if (S[j] < T[i]) {
        head = j;
        j = (head + tail) / 2;
      }
    }
  }

  printf("%d\n", cnt);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>

int main() {
  int n, q;
  std::cin >> n;
  int *S = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  std::cin >> q;
  int *T = new int[q];
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
  }

  int cnt = 0;
  for (int i = 0; i < q; i++) {
    int head = 0;
    int tail = n - 1;
    int j = (head + tail) / 2;
    while (1) {
      if (S[j] == T[i]) {
        cnt++;
        break;
      } else if (tail - head == 1) {
        if (S[head] == T[i] || S[tail] == T[i]) {
          cnt++;
        }
        break;
      } else if (S[j] > T[i]) {
        tail = j;
        j = (head + tail) / 2;
      } else if (S[j] < T[i]) {
        head = j;
        j = (head + tail) / 2;
      }
    }
  }

  printf("%d\n", cnt);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int s[10000];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &s[i]);
  int q;
  scanf("%d", &q);
  int cnt = 0;
  rep(i, q) {
    int t;
    scanf("%d", &t);
    if (binary_search(s, s + n, t))
      cnt++;
  }
  printf("%d\n", cnt);
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int s[100000];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &s[i]);
  int q;
  scanf("%d", &q);
  int cnt = 0;
  rep(i, q) {
    int t;
    scanf("%d", &t);
    if (binary_search(s, s + n, t))
      cnt++;
  }
  printf("%d\n", cnt);
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;
int S[1000000], n;
int serch(int key) {
  int left = 0, right, mid;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == S[mid])
      return 1;
    if (key > S[mid])
      left = mid;
    else if (key < S[mid])
      right = mid;
  }
  return 0;
}
int main() {
  int q, key;
  int i, sum = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (serch(key))
      sum++;
  }
  printf("%d\n", sum);
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
int S[1000000], n;
int serch(int key) {
  int left = 0, right, mid;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == S[mid])
      return 1;
    if (key > S[mid])
      left = mid + 1;
    else if (key < S[mid])
      right = mid;
  }
  return 0;
}
int main() {
  int q, key;
  int i, sum = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (serch(key))
      sum++;
  }
  printf("%d\n", sum);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <vector>
using namespace std;

bool binary_search(int target, int low, int high, vector<int> &S) {
  int mid = low + (high - low) / 2;
  if (S.at(mid) == target) {
    return true;
  }
  if (low == high)
    return false;
  if (S.at(mid) > target) {
    return binary_search(target, low, mid - 1, S);
  } else {
    return binary_search(target, mid + 1, high, S);
  }
}

int main() {
  int n, q, a, c = 0, i, j;
  vector<int> S;
  scanf("%d", &n);
  S.resize(n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &S[i]);
  }

  scanf("%d", &q);
  for (j = 0; j < q; ++j) {
    scanf("%d", &a);
    if (binary_search(a, 0, n - 1, S))
      ++c;
  }
  printf("%d\n", c);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <vector>
using namespace std;

bool binary_search(int target, int low, int high, vector<int> &S) {
  int mid = low + (high - low) / 2;
  if (S.at(mid) == target) {
    return true;
  }
  if (low >= high)
    return false;
  if (S.at(mid) > target) {
    return binary_search(target, low, mid - 1, S);
  } else {
    return binary_search(target, mid + 1, high, S);
  }
}

int main() {
  int n, q, a, c = 0, i, j;
  vector<int> S;
  scanf("%d", &n);
  S.resize(n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &S[i]);
  }

  scanf("%d", &q);
  for (j = 0; j < q; ++j) {
    scanf("%d", &a);
    if (binary_search(a, 0, n - 1, S))
      ++c;
  }
  printf("%d\n", c);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n, q, s[100001], t, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    int l = 0, r = n, m;
    while (l < r) {
      m = (l + r) / 2;
      if (t < s[m])
        r = m;
      else if (t > s[m])
        l = m;
      else {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, q, s[100001], t, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    int l = 0, r = n, m;
    while (l < r) {
      m = (l + r) / 2;
      if (t < s[m])
        r = m;
      else if (t > s[m])
        l = m + 1;
      else {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  int q;
  cin >> q;
  int T[q];
  for (int i = 0; i < q; i++) {
    cin >> T[i];
  }

  int cnt = 0;
  for (int ti = 0; ti < q; ti++) {
    int left = 0, right = n, mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (S[mid] == T[ti]) {
        cnt++;
        break;
      } else if (T[ti] < S[mid])
        right = mid;
      else
        left = mid - 1;
    }
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  int q;
  cin >> q;
  int T[q];
  for (int i = 0; i < q; i++) {
    cin >> T[i];
  }

  int cnt = 0;
  for (int ti = 0; ti < q; ti++) {
    int left = 0, right = n, mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (S[mid] == T[ti]) {
        cnt++;
        break;
      } else if (T[ti] < S[mid])
        right = mid;
      else
        left = mid + 1;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdio.h>
using namespace std;

int search(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return i != n;
}

int main() {
  int n;
  scanf("%d", &n);
  int S[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  int q;
  scanf("%d", &q);
  int T[q];
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
  }
  int cnt = 0;
  for (int i = 0; i < q; i++) {
    cnt += search(S, n, T[i]);
  }

  printf("%d\n", cnt);
  return 0;
}


Fixed submission:
#include <iostream>
#include <stdio.h>
using namespace std;

int search(int A[], int n, int key) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (A[mid] == key) {
      return 1;
    } else if (key < A[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return 0;
}

int main() {
  int n;
  scanf("%d", &n);
  int S[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  int q;
  scanf("%d", &q);
  int T[q];
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
  }
  int cnt = 0;
  for (int i = 0; i < q; i++) {
    cnt += search(S, n, T[i]);
  }

  printf("%d\n", cnt);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

bool binarySearch(vector<int> vec, int target) {
  int l = 0;
  int r = vec.size();
  while (l < r) {
    int mid = (l + r) / 2;
    if (vec[mid] == target) {
      return (true);
    } else if (vec[mid] < target) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  return (false);
}

void solve() {
  int n;
  cin >> n;
  vector<int> S(n);
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  int q;
  cin >> q;
  vector<int> T(q);
  for (int i = 0; i < q; ++i) {
    cin >> T[i];
  }
  int ans = 0;
  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < n; ++j) {
      if (binarySearch(S, T[i])) {
        ++ans;
        break;
      }
    }
  }
  cout << ans << endl;
}

int main() {
  solve();
  return (0);
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

bool binarySearch(vector<int> vec, int target) {
  int l = 0;
  int r = vec.size();
  while (l < r) {
    int mid = (l + r) / 2;
    if (vec[mid] == target) {
      return (true);
    } else if (vec[mid] < target) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  return (false);
}

void solve() {
  int n;
  cin >> n;
  vector<int> S(n);
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  int q;
  cin >> q;
  vector<int> T(q);
  for (int i = 0; i < q; ++i) {
    cin >> T[i];
  }
  int ans = 0;
  for (int i = 0; i < q; ++i) {
    if (binarySearch(S, T[i])) {
      ++ans;
    }
  }
  cout << ans << endl;
}

int main() {
  solve();
  return (0);
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, n) cin >> v[i];
  int m;
  cin >> m;
  int ans = 0;
  rep(i, m) {
    int t;
    cin >> t;
    int imax = n;
    int imin = 0;
    int mid;
    while (imax > imin) {
      mid = (imax + imin) / 2;
      if (v[mid] > t)
        imax = mid;
      else if (v[mid] < t)
        imin += 1;
      else {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, n) cin >> v[i];
  int m;
  cin >> m;
  int ans = 0;
  rep(i, m) {
    int t;
    cin >> t;
    int imax = n;
    int imin = 0;
    int mid;
    while (imax > imin) {
      mid = (imax + imin) / 2;
      if (v[mid] > t)
        imax = mid;
      else if (v[mid] < t)
        imin = mid + 1;
      else {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int BsearchQ(int A[], int n, int key) {
  int l = 0;
  int r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (A[mid] == key) {
      return 1;
    } else if (A[mid] < key) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  return 0;
}

int main() {
  int n, A[10000 + 1], q, key, sum = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    sum += BsearchQ(A, n, key);
  }
  printf("%d\n", sum);
  return 0;
}

Fixed submission:
#include <stdio.h>

int BsearchQ(int A[], int n, int key) {
  int l = 0;
  int r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (A[mid] == key) {
      return 1;
    } else if (A[mid] < key) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  return 0;
}

int main() {
  int n, A[100000 + 5], q, key, sum = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    sum += BsearchQ(A, n, key);
  }
  printf("%d\n", sum);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

int binarySearch(int ary[], int n, int key) {

  int left = 0;
  int right = n;
  int mid = n / 2;

  while (left < right) {

    if (ary[mid] == key)
      return 1;

    else if (key < ary[mid])
      right = mid;

    else
      left = mid + 1;

    mid = (left + right) / 2;
  }

  return 0;
}

int main() {

  int i, j, key, answer;
  int num_of_ary, num_of_key;
  int ary[10010];

  answer = 0;

  std::cin >> num_of_ary;
  for (i = 0; i <= num_of_ary - 1; i++)
    std::cin >> ary[i];

  std::cin >> num_of_key;
  for (i = 0; i <= num_of_key - 1; i++) {

    std::cin >> key;
    answer += binarySearch(ary, num_of_ary, key);
  }

  std::cout << answer << std::endl;
  return 0;
}


Fixed submission:
#include <iostream>

int binarySearch(int ary[], int n, int key) {

  int left = 0;
  int right = n;
  int mid = n / 2;

  while (left < right) {

    if (ary[mid] == key)
      return 1;

    else if (key < ary[mid])
      right = mid;

    else
      left = mid + 1;

    mid = (left + right) / 2;
  }

  return 0;
}

int main() {

  int i, j, key, answer;
  int num_of_ary, num_of_key;
  int ary[100010];

  answer = 0;

  std::cin >> num_of_ary;
  for (i = 0; i <= num_of_ary - 1; i++)
    std::cin >> ary[i];

  std::cin >> num_of_key;
  for (i = 0; i <= num_of_key - 1; i++) {

    std::cin >> key;
    answer += binarySearch(ary, num_of_ary, key);
  }

  std::cout << answer << std::endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int Binarysearch(int *, int, int);
#define N 10000

int main() {
  int i, s, array[N], t, m, sum = 0;

  scanf("%d", &s);
  for (i = 0; i < s; i++) {
    scanf("%d", &array[i]);
  }
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d", &m);
    if (Binarysearch(array, s, m))
      sum++;
  }
  printf("%d\n", sum);
  return 0;
}

int Binarysearch(int A[], int x, int key) {
  int left = 0, right, mid = 0;
  right = x;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    else if (key < A[mid])
      right = mid;
    else if (key > A[mid])
      left = mid + 1;
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int Binarysearch(int *, int, int);
#define N 100005

int main() {
  int i, s, array[N], t, m, sum = 0;

  scanf("%d", &s);
  for (i = 0; i < s; i++) {
    scanf("%d", &array[i]);
  }
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d", &m);
    if (Binarysearch(array, s, m))
      sum++;
  }
  printf("%d\n", sum);
  return 0;
}

int Binarysearch(int A[], int x, int key) {
  int left = 0, right, mid = 0;
  right = x;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    else if (key < A[mid])
      right = mid;
    else if (key > A[mid])
      left = mid + 1;
  }
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {

  int n;

  cin >> n;

  int a[101];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int t, cnt = 0;
  cin >> t;

  int q;

  for (int i = 0; i < t; i++) {
    cin >> q;

    if (upper_bound(a, a + n, q) - lower_bound(a, a + n, q)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {

  int n;

  cin >> n;

  int a[100001];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int t, cnt = 0;
  cin >> t;

  int q;

  for (int i = 0; i < t; i++) {
    cin >> q;

    if (upper_bound(a, a + n, q) - lower_bound(a, a + n, q)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

bool search(vector<int> &S, int s, int b, int e) {
  int m = (e + b) / 2;
  int _s = S[m];
  if (s == _s)
    return true;
  if (b == e)
    return false;
  if (_s > s)
    return search(S, s, b, m - 1);
  if (_s < s)
    return search(S, s, m + 1, e);
}

int main() {
  int n;
  cin >> n;
  vector<int> S(n);
  for (auto &s : S)
    cin >> s;
  int q;
  cin >> q;
  int C = 0;
  while (q--) {
    int t;
    cin >> t;
    if (search(S, t, 0, n - 1))
      C++;
  }
  cout << C << endl;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

bool search(vector<int> &S, int s, int b, int e) {
  int m = (e + b) / 2;
  if (m < b)
    return false;
  int _s = S[m];
  if (s == _s)
    return true;
  if (b == e)
    return false;
  if (_s > s)
    return search(S, s, b, m - 1);
  if (_s < s)
    return search(S, s, m + 1, e);
}

int main() {
  int n;
  cin >> n;
  vector<int> S(n);
  for (auto &s : S)
    cin >> s;
  int q;
  cin >> q;
  int C = 0;
  while (q--) {
    int t;
    cin >> t;
    if (search(S, t, 0, n - 1))
      C++;
  }
  cout << C << endl;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

#define N 10000
int S[N], T[N], count = 0, n, q, i, j, minj, temp, left, right, mid;
int binarySearch(int);
int main() {

  scanf("%d", &n);
  i = 0;
  while (i < n) {
    scanf("%d", &S[i]);
    i++;
  }

  scanf("%d", &q);
  i = 0;
  while (i < q) {
    scanf("%d", &T[i]);
    if (binarySearch(T[i]) == 1) {
      count++;
    }
    i++;
  }
  printf("%d\n", count);

  return 0;
}

int binarySearch(int key) {
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (S[mid] == key) {
      return 1;
    } else if (key < S[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

#define N 100000
int S[N], T[N], count = 0, n, q, i, j, minj, temp, left, right, mid;
int binarySearch(int);
int main() {

  scanf("%d", &n);
  i = 0;
  while (i < n) {
    scanf("%d", &S[i]);
    i++;
  }

  scanf("%d", &q);
  i = 0;
  while (i < q) {
    scanf("%d", &T[i]);
    if (binarySearch(T[i]) == 1) {
      count++;
    }
    i++;
  }
  printf("%d\n", count);

  return 0;
}

int binarySearch(int key) {
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (S[mid] == key) {
      return 1;
    } else if (key < S[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
void A() {
  int S[10000];
  int T[500];
  int N;
  int Q;
  int C = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> T[i];
  }

  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (T[i] == S[j]) {
        C++;
        break;
      }
    }
  }

  cout << C << endl;
}

void B() {
  int S[100001];
  int T[50000];
  int N;
  int Q;
  int C = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> T[i];
  }

  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (S[i] > S[j]) {
        int tmp = S[i];
        S[i] = S[j];
        S[j] = tmp;
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    int left = 0;
    int right = N;
    while (left < right) {
      int center = (left + right) / 2;

      if (T[i] == S[center]) {
        C++;
        break;
      }

      if (T[i] > S[center]) {
        left = center + 1;
      } else {
        right = center;
      }
    }
  }

  cout << C << endl;
}

int main() {
  // A();
  B();
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
void A() {
  int S[10000];
  int T[500];
  int N;
  int Q;
  int C = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> T[i];
  }

  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (T[i] == S[j]) {
        C++;
        break;
      }
    }
  }

  cout << C << endl;
}

void B() {
  int S[100001];
  int T[50000];
  int N;
  int Q;
  int C = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> T[i];
  }

  for (int i = 0; i < Q; i++) {
    int left = 0;
    int right = N;
    while (left < right) {
      int center = (left + right) / 2;

      if (T[i] == S[center]) {
        C++;
        break;
      }

      if (T[i] > S[center]) {
        left = center + 1;
      } else {
        right = center;
      }
    }
  }

  cout << C << endl;
}

int main() {
  // A();
  B();
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int binarySearch(int A[], int n, int key) {
  int mid, left = 0, right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    else if (key < A[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return 0;
}

int main() {
  int n, s[10100], q, key, count = 0;
  int i, j, k;

  cin >> n;

  for (i = 0; i < n; i++)
    scanf("%d", &s[i]);

  cin >> q;

  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binarySearch(s, n, key))
      count++;
  }

  cout << count << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int binarySearch(int A[], int n, int key) {
  int mid, left = 0, right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    else if (key < A[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return 0;
}

int main() {
  int n, s[100100], q, key, count = 0;
  int i, j, k;

  cin >> n;

  for (i = 0; i < n; i++)
    scanf("%d", &s[i]);

  cin >> q;

  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binarySearch(s, n, key))
      count++;
  }

  cout << count << endl;

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

bool myBinarySearch(int S[], int begin, int end, int target) {
  if (begin > end) {
    return false;
  }
  int mid = (begin + end) / 2;
  if (S[mid] > target) {
    return myBinarySearch(S, begin, mid - 1, target);
  } else if (S[mid] < target) {
    return myBinarySearch(S, mid + 1, end, target);
  } else {
    return true;
  }
}

int main(void) {
  int S[10000];
  int T[500];
  int n, m;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> S[i];
  }
  sort(S, S + m);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> T[i];
  }
  sort(T, T + n);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (myBinarySearch(S, 0, m, T[i])) {
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

bool myBinarySearch(int S[], int begin, int end, int target) {
  if (begin > end) {
    return false;
  }
  int mid = (begin + end) / 2;
  if (S[mid] > target) {
    return myBinarySearch(S, begin, mid - 1, target);
  } else if (S[mid] < target) {
    return myBinarySearch(S, mid + 1, end, target);
  } else {
    return true;
  }
}

int main(void) {
  int S[100000];
  int T[50000];
  int n, m;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> S[i];
  }
  sort(S, S + m);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> T[i];
  }
  sort(T, T + n);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (myBinarySearch(S, 0, m, T[i])) {
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}