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

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

typedef long long int lli;
lli s[20000], t[2000];

int main(void) {
  int n, q;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &s[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++)
    scanf("%lld", &t[i]);

  std::sort(s, s + n);
  std::sort(t, t + q);

  lli *ps = s, *pt = t;
  int res = 0;
  while (ps < s + n, pt < t + q) {
    if (*ps == *pt)
      res++, ps++, pt++;
    else if (*ps < *pt)
      ps++;
    else
      pt++;
  }
  printf("%d\n", res);

  return 0;
}

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

typedef long long int lli;
lli s[20000], t[2000];

int main(void) {
  int n, q;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &s[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++)
    scanf("%lld", &t[i]);

  std::sort(s, s + n);
  std::sort(t, t + q);

  lli *ps = s, *pt = t;
  int res = 0;
  while (ps < s + n && pt < t + q) {
    if (*ps == *pt)
      res++, ps++, pt++;
    else if (*ps < *pt)
      ps++;
    else
      pt++;
  }
  printf("%d\n", res);

  return 0;
}
Problem p02267 (C++)

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

int a[1005];
bool Find(int n, int key) {
  int i = 0;
  a[n] = key;
  while (a[i] != key)
    i++;
  if (i != n)
    return true;
  else
    return false;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int q, key, cnt = 0;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (Find(n, key))
      cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}

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

int a[10005];
bool Find(int n, int key) {
  int i = 0;
  a[n] = key;
  while (a[i] != key)
    i++;
  if (i != n)
    return true;
  else
    return false;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int q, key, cnt = 0;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (Find(n, key))
      cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}
Problem p02268 (C++)

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

int A[10001], n;

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

int main() {
  int q, i, k, ans = 0;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> k;
    if (binarySearch(k))
      ans++;
  }

  cout << ans << endl;

  return 0;
}


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

int A[100000], n;

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

int main() {
  int q, i, k, ans = 0;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> k;
    if (binarySearch(k))
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02268 (C++)

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

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

int main() {
  int n, q;
  int S[100'000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  int key;
  int count = 0;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (binarySearch(S, n, key))
      count++;
  }

  cout << count << endl;
}


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

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

int main() {
  int n, q;
  int S[100'000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  int key;
  int count = 0;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (binarySearch(S, n, key))
      count++;
  }

  cout << count << endl;
}
Problem p02268 (C++)

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

using namespace std;

int b[10 ^ 9 + 1];

int main() {
  int n, q, s, t;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    b[s]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (b[t] >= 1)
      ans++;
  }
  cout << ans << endl;
  return (0);
}

Fixed submission:
#include <iostream>

using namespace std;

int b[1000000010];

int main() {
  int n, q, s, t;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    b[s]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (b[t] >= 1)
      ans++;
  }
  cout << ans << endl;
  return (0);
}
Problem p02268 (C++)

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

using namespace std;
int A[10000], B[10000], NA, NB;

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

int main() {
  int sum = 0;
  cin >> NA;
  for (int i = 0; i < NA; i++) {
    cin >> A[i];
  }
  cin >> NB;
  for (int i = 0; i < NB; i++) {
    cin >> B[i];
    if (binarysearch(B[i]))
      sum++;
  }
  cout << sum << endl;
  return 0;
}


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

using namespace std;
int A[100000], B[100000], NA, NB;

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

int main() {
  int sum = 0;
  cin >> NA;
  for (int i = 0; i < NA; i++) {
    cin >> A[i];
  }
  cin >> NB;
  for (int i = 0; i < NB; i++) {
    cin >> B[i];
    if (binarysearch(B[i]))
      sum++;
  }
  cout << sum << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

bool FindByBinarySearch(int x, int A[], int left, int right) {
  int mid = (left + right) / 2;
  if (x == A[mid])
    return true;
  if (x >= A[left] && x < A[mid])
    return FindByBinarySearch(x, A, left, mid);
  if (x > A[mid] && x <= A[right])
    return FindByBinarySearch(x, A, mid + 1, right);

  return false;
}

int main() {
  int n, q, S[100000], T[50000];
  int C = 0;

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

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

  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < n; ++j) {
      if (FindByBinarySearch(T[i], S, 0, n - 1)) {
        ++C;
        break;
      }
    }
  }

  printf("%d\n", C);

  return 0;
}

Fixed submission:
#include <stdio.h>

bool FindByBinarySearch(int x, int A[], int left, int right) {
  int mid = (left + right) / 2;
  if (x == A[mid])
    return true;
  if (x >= A[left] && x < A[mid])
    return FindByBinarySearch(x, A, left, mid);
  if (x > A[mid] && x <= A[right])
    return FindByBinarySearch(x, A, mid + 1, right);

  return false;
}

int main() {
  int n, q, S[100000], T[50000];
  int C = 0;

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

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

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

  printf("%d\n", C);

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#define N 100000
int count = 0;
void BinarySearch(int array[], int right, int left, int key) {
  int mid = (right + left) / 2;
  if (left > right) {
    return;
  }

  if (array[mid] == key) {
    count++;
    return;
  } else if (array[mid] < key) {
    BinarySearch(array, right, mid + 1, key);
  } else {
    BinarySearch(array, mid, left, key);
  }
}

int main() {
  int i, n, array[N];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &array[i]);
  int m;
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    int l;
    scanf("%d", &l);
    BinarySearch(array, n, 0, l);
  }
  printf("%d\n", count);
}

Fixed submission:
#include <stdio.h>
#define N 100000
int count = 0;
void BinarySearch(int array[], int right, int left, int key) {
  while (left < right) {
    int mid = (left + right) / 2;
    if (array[mid] == key) {
      count++;
      break;
    } else if (array[mid] < key) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
}

int main() {
  int i, n, array[N];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &array[i]);
  int m;
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    int l;
    scanf("%d", &l);
    BinarySearch(array, n, 0, l);
  }
  printf("%d\n", count);
}
Problem p02268 (C++)

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

#define N 10000

int S[N], T[N];
int n, m;

int binary() {
  int l, r, mid, i;
  int count = 0;
  for (i = 0; i < m; i++) {
    l = 0;
    r = n;
    while (l < r) {
      mid = (l + r) / 2;
      if (S[mid] == T[i]) {
        count++;
        l = r;
      } else if (T[i] < S[mid]) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
  }
  return count;
}

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

  int c = binary();

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

Fixed submission:
#include <stdio.h>

#define N 100000

int S[N], T[N];
int n, m;

int binary() {
  int l, r, mid, i;
  int count = 0;
  for (i = 0; i < m; i++) {
    l = 0;
    r = n;
    while (l < r) {
      mid = (l + r) / 2;
      if (S[mid] == T[i]) {
        count++;
        l = r;
      } else if (T[i] < S[mid]) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
  }
  return count;
}

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

  int c = binary();

  printf("%d\n", c);
  return 0;
}
Problem p02268 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-

input()
s = set(input().split())
input()
t = set(input().split())
print(int(s & t))


Fixed submission:
# -*- coding: utf-8 -*-

input()
s = set(input().split())
input()
t = set(input().split())
print(len(s & t))
Problem p02268 (Python)

Buggy submission (status: Runtime Error):
def binarySearch(array, key):
    left = 0
    right = len(array)
    while left < right:
        mid = (left + right) / 2
        if key == array[mid]:
            return True
        elif key > array[mid]:
            left = mid + 1
        elif key < array[mid]:
            right = mid
        else:
            print("ERROR")
    return False


n = int(input())
s = [int(x) for x in input().split()]
q = int(input())
t = [int(x) for x in input().split()]
a = [x for x in t if binarySearch(s, x)]
print(len(a))


Fixed submission:
def binarySearch(array, key):
    left = 0
    right = len(array)
    while left < right:
        mid = int((left + right) / 2)
        if key == array[mid]:
            return True
        elif key > array[mid]:
            left = mid + 1
        elif key < array[mid]:
            right = mid
        else:
            print("ERROR")
    return False


n = int(input())
s = [int(x) for x in input().split()]
q = int(input())
t = [int(x) for x in input().split()]
a = [x for x in t if binarySearch(s, x)]
print(len(a))
Problem p02268 (C++)

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

int n, S[100000];

int search(int T) {
  int l = 0, r = n;
  int mid;

  while (l < r) {
    mid = (l + r) / 2;
    if (T == S[mid])
      return 1;
    if (T > S[mid])
      l = mid + 1;
    else if (T < S[mid])
      r = mid;
  }
  return 0;
}

int main() {
  int i, q;
  int T[5000];
  int count = 0;

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> S[i];
  }
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    std::cin >> T[i];
    if (search(T[i])) {
      count++;
    }
  }
  std::cout << count << std::endl;
  return 0;
}


Fixed submission:
#include <iostream>

int n, S[100000];

int search(int T) {
  int l = 0, r = n;
  int mid;

  while (l < r) {
    mid = (l + r) / 2;
    if (T == S[mid])
      return 1;
    if (T > S[mid])
      l = mid + 1;
    else if (T < S[mid])
      r = mid;
  }
  return 0;
}

int main() {
  int i, q;
  int T[50000];
  int count = 0;

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> S[i];
  }
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    std::cin >> T[i];
    if (search(T[i])) {
      count++;
    }
  }
  std::cout << count << std::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[n];
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  int T[q];
  for (int i = 0; i < q; i++)
    cin >> T[i];

  int x = 0;
  int mid;
  int left = 0;
  int right = n;
  for (int j = 0; j < q; j++) {
    left = 0;
    right = n;
    while (left < right) {
      mid = (left + right) / 2;
      if (S[mid] == T[j]) {
        x++;
        break;
      } else if (T[j] < S[mid]) {
        right = mid;
      } else if (T[j] > S[mid]) {
        left = mid;
      }
    }
  }
  cout << x << endl;

  return 0;
}

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

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

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

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[10000], b, c[10000], i, k = 0, j;
  for (cin >> n, i = 0; i < n; i++)
    cin >> a[i];
  for (cin >> b, i = 0; i < b; i++)
    cin >> c[i];
  for (i = 0; i < b; i++)
    if (binary_search(a, a + n, c[i]))
      k++;
  cout << k << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a[100000], b, c[50000], i, k = 0, j;
  for (cin >> n, i = 0; i < n; i++)
    cin >> a[i];
  for (cin >> b, i = 0; i < b; i++)
    cin >> c[i];
  for (i = 0; i < b; i++)
    if (binary_search(a, a + n, c[i]))
      k++;
  cout << k << endl;
}
Problem p02268 (C++)

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

int A[10000], n;

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

  return 0;
}

int main() {
  int q, k, s = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> k;

    if (Search(k))
      s++;
  }

  cout << s << "\n";
  return 0;
}

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

int A[100000], n;

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

  return 0;
}

int main() {
  int q, k, s = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> k;

    if (Search(k))
      s++;
  }

  cout << s << "\n";
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
// 含有标记的线性搜索可以将算法效率提高常数倍
// for(int i=0;i<n;i++)
// s[n]==key;while(s[i]!=key)i++;if(i!=n)
// 区别在于主循环中比较运算的次数
#include <cstdio>
#include <iostream>
using namespace std;
int s[10004];
int cnt, n;
/*void LinearSearch(int s[],int num)
{
        for(int i=0;i<n;i++)
        {
                if(s[i]==num)
                {
                        cnt++;
                        break;
                }
        }
}*/
int BinarySearch(int key) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (s[mid] == key) {
      // cout<<"1"<<endl;
      return 1;
    } else if (key < s[mid]) // 前半部分
    {
      right = mid;
    } else // 后半部分
    {
      left = mid + 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int k = 0; k < n; k++) {
    scanf("%d", &s[k]);
    // cin>>s[k];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int nn;
    // cin>>nn;
    scanf("%d", &nn);
    if (BinarySearch(nn)) {
      cnt++;
      // cout<<nn<<endl;
    }
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
// 含有标记的线性搜索可以将算法效率提高常数倍
// for(int i=0;i<n;i++)
// s[n]==key;while(s[i]!=key)i++;if(i!=n)
// 区别在于主循环中比较运算的次数
#include <cstdio>
#include <iostream>
using namespace std;
int s[100005];
int cnt, n;
/*void LinearSearch(int s[],int num)
{
        for(int i=0;i<n;i++)
        {
                if(s[i]==num)
                {
                        cnt++;
                        break;
                }
        }
}*/
int BinarySearch(int key) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (s[mid] == key) {
      // cout<<"1"<<endl;
      return 1;
    } else if (key < s[mid]) // 前半部分
    {
      right = mid;
    } else // 后半部分
    {
      left = mid + 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int k = 0; k < n; k++) {
    scanf("%d", &s[k]);
    // cin>>s[k];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int nn;
    // cin>>nn;
    scanf("%d", &nn);
    if (BinarySearch(nn)) {
      cnt++;
      // cout<<nn<<endl;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++)

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

using namespace std;

bool binSearch(int *s, int n, int target) {
  int lo = 0;
  int hi = n - 1;
  while (lo <= hi) {
    int mid = lo + (hi - lo) / 2;
    if (s[mid] == target)
      return true;
    else if (target > s[mid])
      lo = mid + 1;
    else
      hi = mid - 1;
  }
  return false;
}

int main() {
  int val, n, q;
  int s[10000], t[500];

  cin >> n;
  for (int i = 0; i < n && cin >> val; ++i)
    s[i] = val;

  int count = 0;
  cin >> q;
  while (q--) {
    cin >> val;
    if (binSearch(s, n, val))
      count++;
  }

  cout << count << endl;
  return 0;
}

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

using namespace std;

bool binSearch(int *s, int n, int target) {
  int lo = 0;
  int hi = n - 1;
  while (lo <= hi) {
    int mid = lo + (hi - lo) / 2;
    if (s[mid] == target)
      return true;
    else if (target > s[mid])
      lo = mid + 1;
    else
      hi = mid - 1;
  }
  return false;
}

int main() {
  int val, n, q;
  int s[100000], t[50000];

  cin >> n;
  for (int i = 0; i < n && cin >> val; ++i)
    s[i] = val;

  int count = 0;
  cin >> q;
  while (q--) {
    cin >> val;
    if (binSearch(s, n, val))
      count++;
  }

  cout << count << endl;
  return 0;
}
Problem p02268 (C++)

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

#include <algorithm>
#include <cmath>

using namespace std;

int S[10010];
int s, q;

bool binarySearch(int A[], int key) {
  int left = 0;
  int right = s - 1;
  int mid;

  while (left <= right) {
    mid = (left + right) / 2;
    if (A[mid] > key) {
      right = mid - 1;
    } else if (A[mid] < key) {
      left = mid + 1;
    } else {
      return true;
    }
  }
  return false;
}

int main() {

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

  int count = 0;
  int temp;

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> temp;
    if (binarySearch(S, temp)) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <iostream>

#include <algorithm>
#include <cmath>

using namespace std;

int S[100100];
int s, q;

bool binarySearch(int A[], int key) {
  int left = 0;
  int right = s - 1;
  int mid;

  while (left <= right) {
    mid = (left + right) / 2;
    if (A[mid] > key) {
      right = mid - 1;
    } else if (A[mid] < key) {
      left = mid + 1;
    } else {
      return true;
    }
  }
  return false;
}

int main() {

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

  int count = 0;
  int temp;

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> temp;
    if (binarySearch(S, temp)) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}
Problem p02268 (C++)

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

int n, S[10000 + 1];

bool binarySearch(int key) {
  int left = 0, right = n - 1, mid;

  while (left <= right) {
    mid = (left + right) / 2;
    // printf("L, R, M, S[mid]: %d %d %d %d\n", left, right, mid, S[mid]);
    if (key == S[mid])
      return true;
    else if (key < S[mid])
      right = mid - 1;
    else
      left = mid + 1;
  }
  return false;
}

int main() {
  int q, key, count = 0;

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

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

  printf("%d\n", count);

  return 0;
}

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

int n, S[100000 + 1];

bool binarySearch(int key) {
  int left = 0, right = n - 1, mid;

  while (left <= right) {
    mid = (left + right) / 2;
    // printf("L, R, M, S[mid]: %d %d %d %d\n", left, right, mid, S[mid]);
    if (key == S[mid])
      return true;
    else if (key < S[mid])
      right = mid - 1;
    else
      left = mid + 1;
  }
  return false;
}

int main() {
  int q, key, count = 0;

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

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

  printf("%d\n", count);

  return 0;
}
Problem p02268 (C++)

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

// returns the index of the element which value equals to key.
// if no elements matched, it returns -1.
int search(const vector<int> &x, int key) {
  int left = 0, right = x.size();
  while (left < right) {
    int mid = (left + right) / 2;
    if (x[mid] == key) {
      return mid;
    } else if (key < x[mid]) {
      right = mid;
    } else if (x[mid] < key) {
      left = mid + 1;
    }
  }
  return -1;
}

int main() {
  int n;
  cin >> n;
  vector<int> na(0, n);
  for (int i = 0; i < n; i++)
    cin >> na[i];
  int q;
  cin >> q;
  int t;
  int ans = 0;
  for (int i = 0; i < q; ++i) {
    cin >> t;
    ans += search(na, t) >= 0 ? 1 : 0;
  }
  cout << ans << endl;
  return 0;
}


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

// returns the index of the element which value equals to key.
// if no elements matched, it returns -1.
int search(const vector<int> &x, int key) {
  int left = 0, right = x.size();
  while (left < right) {
    int mid = (left + right) / 2;
    if (x[mid] == key) {
      return mid;
    } else if (key < x[mid]) {
      right = mid;
    } else if (x[mid] < key) {
      left = mid + 1;
    }
  }
  return -1;
}

int main() {
  int n;
  cin >> n;
  vector<int> na(n, 0);
  for (int i = 0; i < n; i++)
    cin >> na[i];
  int q;
  cin >> q;
  int t;
  int ans = 0;
  for (int i = 0; i < q; ++i) {
    cin >> t;
    ans += search(na, t) >= 0 ? 1 : 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
//
//  main.cpp
//  binary_search
//
//  Created by ???????????? on 2017/10/06.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>

using namespace std;

bool binary_search(int A[], int n, int key) {
  int left, mid, right;
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (A[mid] == key) {
      return true;
    } else if (key < A[mid]) {
      right = mid;
    } else if (key > A[mid]) {
      left = mid;
    }
  }
  return false;
}

int main(int argc, const char *argv[]) {
  int n, i, ct, q, key;

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

  ct = 0;
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    bool found = binary_search(S, n, key);
    if (found)
      ct++;
  }
  cout << ct << endl;
  ;
  return 0;
}

Fixed submission:
//
//  main.cpp
//  binary_search
//
//  Created by ???????????? on 2017/10/06.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>

using namespace std;

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

int main(int argc, const char *argv[]) {
  int n, i, ct, q, key;

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

  ct = 0;
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    bool found = binary_search(S, n, key);
    if (found)
      ct++;
  }
  cout << ct << endl;
  ;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

bool binary_search(int *a, int n, int x) {
  int l = 0, r = n;
  while (r - l > 0) {
    int mid = (l + r) / 2;
    if (a[mid] < x)
      l = mid;
    else if (a[mid] == x)
      return true;
    else
      r = mid;
  }
  // cout<<l<<","<<r<<endl;
  return false;
}

int n;
int S[100005];
int q;
int main() {
  int ans = 0;
  cin >> n;
  rep(i, n) cin >> S[i];
  cin >> q;
  rep(i, q) {
    int t;
    cin >> t;
    if (binary_search(S, n, t))
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

bool binary_search(int *a, int n, int x) {
  int l = 0, r = n;
  while (r - l > 0) {
    int mid = (l + r) / 2;
    if (a[mid] < x)
      l = mid + 1;
    else if (a[mid] == x)
      return true;
    else
      r = mid;
  }
  // cout<<l<<","<<r<<endl;
  return false;
}

int n;
int S[100005];
int q;
int main() {
  int ans = 0;
  cin >> n;
  rep(i, n) cin >> S[i];
  cin >> q;
  rep(i, q) {
    int t;
    cin >> t;
    if (binary_search(S, n, t))
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main(void) {
  int n, S[10000], i, j, q, T[500], t, C, L, R, M;
  scanf("%d", &n);
  for (i = 0; i != n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (j = 0; j != q; j++) {
    scanf("%d", &T[j]);
  }

  C = 0;
  for (i = 0; i != q; i++) {
    L = 0;
    R = n;
    while (L < R) {
      M = (L + R) / 2;
      if (S[M] == T[i]) {
        C++;

        break;
      }
      if (S[M] > T[i]) {
        R = M;
      } else {
        L = M + 1;
      }
    }
  }
  printf("%d\n", C);
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int n, S[100000], i, j, q, T[50000], t, C, L, R, M;
  scanf("%d", &n);
  for (i = 0; i != n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (j = 0; j != q; j++) {
    scanf("%d", &T[j]);
  }

  C = 0;
  for (i = 0; i != q; i++) {
    L = 0;
    R = n;
    while (L < R) {
      M = (L + R) / 2;
      if (S[M] == T[i]) {
        C++;

        break;
      }
      if (S[M] > T[i]) {
        R = M;
      } else {
        L = M + 1;
      }
    }
  }
  printf("%d\n", C);
  return 0;
}
Problem p02268 (C++)

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

bool binSearch(int A[], int target, int size) {
  int left = 0, right = size - 1, mid;
  while (left <= right) {
    mid = (left + right) / 2;
    // cout <<  "left : " << left << ", right : " << right << ", mid : " << mid
    // << endl;
    if (A[mid] == target) {
      // cout << target << " found." << endl;
      return true;
    } else {
      if (A[mid] < target)
        left = mid + 1;
      else
        right = mid;
    }
  }
  return false;
}

int main() {
  int size, query, queryNum, count = 0;

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

  cin >> queryNum;
  for (int i = 0; i < queryNum; i++) {
    scanf("%d", &query);
    if (binSearch(A, query, size))
      count++;
  }

  cout << count << endl;

  delete[] A;
}

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

bool binSearch(int A[], int target, int size) {
  int left = 0, right = size, mid;
  while (left < right) {
    mid = (left + right) / 2;
    // cout <<  "left : " << left << ", right : " << right << ", mid : " << mid
    // << endl;
    if (A[mid] == target) {
      // cout << target << " found." << endl;
      return true;
    } else {
      if (A[mid] < target)
        left = mid + 1;
      else
        right = mid;
    }
  }
  return false;
}

int main() {
  int size, query, queryNum, count = 0;

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

  cin >> queryNum;
  for (int i = 0; i < queryNum; i++) {
    scanf("%d", &query);
    if (binSearch(A, query, size))
      count++;
  }

  cout << count << endl;

  delete[] A;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>

int binarysearch(int a[], int n, int key) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == a[mid])
      return 1;
    if (key > a[mid])
      left = mid + 1;
    else if (key < a[mid])
      right = mid;
  }
  return 0;
}
using namespace std;
int main() {
  int i, n, a[10001], q, key, sum = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binarysearch(a, n, key))
      sum++;
  }
  printf("%d\n", sum);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>

int binarysearch(int a[], int n, int key) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == a[mid])
      return 1;
    if (key > a[mid])
      left = mid + 1;
    else if (key < a[mid])
      right = mid;
  }
  return 0;
}
using namespace std;
int main() {
  int i, n, a[100001], q, key, sum = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binarysearch(a, n, key))
      sum++;
  }
  printf("%d\n", sum);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n, s[100000], q, t[50000], i, j, l, r, m, count = 0;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> t[i];
  }

  // Binary Search

  for (i = 0; i < q; i++) {
    l = 0;
    r = n;
    while (l < r) {
      m = (l + r) / 2;
      if (s[m] > t[i]) {
        r = m;
      } else if (s[m] < t[i]) {
        l = m;
      } else {
        count++;
        break;
      }
    }
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, s[100000], q, t[50000], i, j, l, r, m, count = 0;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> t[i];
  }

  // Binary Search

  for (i = 0; i < q; i++) {
    l = 0;
    r = n;
    while (l < r) {
      m = (l + r) / 2;
      if (s[m] > t[i]) {
        r = m;
      } else if (s[m] < t[i]) {
        l = m + 1;
      } else {
        count++;
        break;
      }
    }
  }

  cout << count << endl;

  return 0;
}
Problem p02268 (C++)

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

int A[1000000], n;

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

int main() {
  int i, 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);
    if (binarySearch(key))
      sum++;
  }
  printf("%d\n", sum);

  return 0;
}

Fixed submission:
#include <stdio.h>

int A[1000000], n;

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

int main() {
  int i, 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);
    if (binarySearch(key))
      sum++;
  }
  printf("%d\n", sum);

  return 0;
}
Problem p02268 (C++)

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

int A[1000000], n;

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

int main() {
  int i, 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);
    if (binarySearch(key))
      sum++;
  }
  printf("%d\n", sum);

  return 0;
}


Fixed submission:
#include <stdio.h>

int A[1000000], n;

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

int main() {
  int i, 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);
    if (binarySearch(key))
      sum++;
  }
  printf("%d\n", sum);

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#define int long long

using namespace std;

int S[100100];
int n, q;
int T;

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  int L = 0;
  int R = n;
  int cnt = 0;
  int mid;
  for (int i = 0; i < q; i++) {
    cin >> T;
    L = 0;
    R = n;
    for (int j = 0; j < n / 2; j++) {
      int mid = (L + R) / 2;
      // cout<<mid<<endl;
      if (S[L] == T) {
        cnt++;
        // cout<<"succes"<<endl;
        break;
      } else if (S[mid] <= T) {
        L = mid;
      } else if (S[mid] > T) {
        R = mid;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#define int long long

using namespace std;

int S[100100];
int n, q;
int T;

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  int L = 0;
  int R = n;
  int cnt = 0;
  int mid;
  for (int i = 0; i < q; i++) {
    cin >> T;
    L = 0;
    R = n;
    for (int j = 0; j < 18; j++) {
      int mid = (L + R) / 2;
      // cout<<mid<<endl;
      if (S[L] == T) {
        cnt++;
        // cout<<"succes"<<endl;
        break;
      } else if (S[mid] <= T) {
        L = mid;
      } else if (S[mid] > T) {
        R = mid;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);

  unsigned int list[100000];
  int numOfList;

  cin >> numOfList;

  rep(i, numOfList) cin >> list[i];

  int val;
  int counter = 0;
  cin >> val;

  while (cin >> val) {
    int high = numOfList - 1;
    int low = 0;
    int mid;

    while (low <= high) {
      if (list[mid] < val) {
        low = mid + 1;
      } else if (list[mid] > val) {
        high = mid - 1;
      } else if (list[mid] == val) {
        counter++;
        break;
      }
    }
  }
  cout << counter << endl;

  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(false);

  unsigned int list[100000];
  int numOfList;

  cin >> numOfList;

  rep(i, numOfList) cin >> list[i];

  int val;
  int counter = 0;
  cin >> val;

  while (cin >> val) {
    int high = numOfList - 1;
    int low = 0;
    int mid;

    while (low <= high) {
      mid = (low + high) / 2;
      if (list[mid] < val) {
        low = mid + 1;
      } else if (list[mid] > val) {
        high = mid - 1;
      } else if (list[mid] == val) {
        counter++;
        break;
      }
    }
  }
  cout << counter << endl;

  return 0;
}
Problem p02268 (C++)

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

// ?????§????´¢
bool BinarySearch(int A[], int n, int key) {
  int left = 0, right = n, mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return true;

    if (key > A[mid])
      left = mid + 1;
    if (key < A[mid])
      right = mid;
  }
  return false;
}
int main() {
  int n, q, key;
  int sum = 0;
  int A[10000 + 10];
  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);
    if (BinarySearch(A, n, key))
      sum++;
  }
  printf("%d\n", sum);
}

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

// ?????§????´¢
bool BinarySearch(int A[], int n, int key) {
  int left = 0, right = n, mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return true;

    if (key > A[mid])
      left = mid + 1;
    if (key < A[mid])
      right = mid;
  }
  return false;
}
int main() {
  int n, q, key;
  int sum = 0;
  int A[100000 + 10];
  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);
    if (BinarySearch(A, n, key))
      sum++;
  }
  printf("%d\n", sum);
}
Problem p02268 (C++)

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

using namespace std;

constexpr int Nmax = 100'000;

bool binarySearch(int *array, int value, int n) {
  int left = 0;
  int right = n - 1;
  int center = (left + right) / 2;
  while (array[center] != value) {
    if (left == right) {
      return false;
    }
    if (array[center] > value) {
      right = center - 1;
    } else {
      left = center + 1;
    }
    center = (left + right) / 2;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);

  int same = 0;

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

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int value;
    cin >> value;
    if (binarySearch(S, value, n)) {
      same++;
    }
  }
  cout << same << endl;

  return 0;
}

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

using namespace std;

constexpr int Nmax = 100'000;

bool binarySearch(int *array, int value, int n) {
  int left = 0;
  int right = n - 1;
  int center = (left + right) / 2;
  while (array[center] != value) {
    if (left >= right) {
      return false;
    }
    if (array[center] > value) {
      right = center - 1;
    } else {
      left = center + 1;
    }
    center = (left + right) / 2;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);

  int same = 0;

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

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int value;
    cin >> value;
    if (binarySearch(S, value, n)) {
      same++;
    }
  }
  cout << same << endl;

  return 0;
}
Problem p02268 (C++)

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

using namespace std;

int ans = 0;

bool answer(int n, long num[], int p) {
  if (n == num[p]) {
    ans++;
    return true;
  }
  return false;
}

int main() {
  int s, t, n;

  cin >> s;
  long num[s];

  for (int i = 0; i < s; i++)
    cin >> num[i];

  cin >> t;
  for (int i = 0; i < t; i++) {
    int p = 0, pp = s;
    bool br = false;
    cin >> n;
    while (true) {
      int a = p + ((pp - p) / 2);
      if (answer(n, num, a))
        break;
      else if (n > num[a]) {
        if (answer(n, num, a + 1))
          break;
        p = a;
        if (p >= s)
          break;
      } else if (answer(n, num, a - 1))
        break;
      else {
        pp = a - 2;
        if (pp <= p)
          break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int ans = 0;

bool answer(int n, long num[], int p) {
  if (n == num[p]) {
    ans++;
    return true;
  }
  return false;
}

int main() {
  int s, t, n;

  cin >> s;
  long num[s];

  for (int i = 0; i < s; i++)
    cin >> num[i];

  cin >> t;
  for (int i = 0; i < t; i++) {
    int p = 0, pp = s;
    bool br = false;
    cin >> n;
    while (true) {
      int a = p + ((pp - p) / 2);
      if (answer(n, num, a))
        break;
      else if (n > num[a]) {
        if (answer(n, num, a + 1))
          break;
        p = a + 1;
        if (p == pp)
          break;
      } else if (answer(n, num, a - 1))
        break;
      else {
        pp = a - 2;
        if (pp <= p)
          break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02268 (C++)

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

using namespace std;

int main() {

  int n, q;
  long long S[10000], T;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;

  int count = 0;

  for (int j = 0; j < q; j++) {
    cin >> T;
    int left = 0;
    int right = n;
    int mid = (left + right) / 2;
    while (left < right) {
      mid = (left + right) / 2;
      if (T == S[mid]) {
        count++;
        break;
      }
      if (T > S[mid]) {
        left = mid + 1;
      } else if (T < S[mid]) {
        right = mid;
      }
    }
  }
  cout << count << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, q;
  long long S[1000000], T;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;

  int count = 0;

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

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

static const size_t n_max = 10000;
static int S[n_max];

int main() {
  size_t n, q;
  scanf("%zu", &n);
  for (size_t i = 0; i < n; i++)
    scanf("%d", S + i);
  scanf("%zu", &q);
  int c = 0;
  for (size_t i = 0; i < q; i++) {
    int t;
    scanf("%d", &t);
    if (std::binary_search(S, S + n, t))
      c++;
  }
  printf("%d\n", c);
  return 0;
}

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

static const size_t n_max = 100000;
static int S[n_max];

int main() {
  size_t n, q;
  scanf("%zu", &n);
  for (size_t i = 0; i < n; i++)
    scanf("%d", S + i);
  scanf("%zu", &q);
  int c = 0;
  for (size_t i = 0; i < q; i++) {
    int t;
    scanf("%d", &t);
    if (std::binary_search(S, S + n, t))
      c++;
  }
  printf("%d\n", c);
  return 0;
}
Problem p02268 (C++)

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

int A[1000000], n;

int binarySearch(int key) {
  int left = 0, right = n, mid;

  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;
  }

  return 0;
}

int main() {
  int q, k, 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", &k);
    if (binarySearch(k))
      sum++;
  }
  printf("%d\n", sum);
}

Fixed submission:
#include <cstdio>

int A[1000000], n;

int binarySearch(int key) {
  int left = 0, right = n, mid;

  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;
}

int main() {
  int q, k, 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", &k);
    if (binarySearch(k))
      sum++;
  }
  printf("%d\n", sum);
}