alexjercan/bugnet huggingface dataset:
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector<int> ivec1, ivec2;
int n, q, a;
bool binarySearch(vector<int> &v, int key) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (left + right) >> 1;
    if (key < v[mid])
      right = mid;
    else
      left = mid + 1;
  }
  left--;
  if (left >= 0 && v[left] == key) {
    return true;
  } else
    return false;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    ivec1.push_back(a);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &a);
    ivec2.push_back(a);
  }
  sort(ivec1.begin(), ivec1.end());
  int sum = 0;
  for (int i = 0; i < q; i++) {
    if (binarySearch(ivec1, ivec2[i]))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;
int A[10001], m;
int search(int key) {
  int left = 0;
  int right = m;
  int mid;

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

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

  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &key);
    if (search(key)) {
      sum++;
    }
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int n, m;
int s[100005];
int q[50005];
bool search(int x) {
  int t = lower_bound(s, s + n, x) - s;
  if (s[t] == x)
    return true;
  else
    return false;
}
int main() {
  int sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &s[i]);
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &q[i]);
    if (search(q[i]))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[100005];
int n, q;
// bool BinarySearch(int l,int r,int k){
//	int mi=(l+r)/2;
//	if(l>=r&&a[mi]!=k)return false;
//	if(a[mi]==k)return true;
//	if(a[mi]<k)return BinarySearch(mi+1,r,k);
//	else return BinarySearch(l,mi-1,k);
// }
bool BinarySearch(int l, int r, int k) {
  int mi;
  while (l < r) {
    mi = (l + r) / 2;
    if (a[mi] == k)
      return true;
    else if (a[mi] < k)
      l = mi;
    else
      r = mi;
  }
  return false;
}
int main() {
  //	freopen("a.txt","r",stdin);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  //	sort(a,a+n);
  cin >> q;
  int res = 0, number;
  for (int i = 0; i < q; i++) {
    cin >> number;
    if (BinarySearch(0, n - 1, number))
      res++;
    //		if(*lower_bound(a,a+n,number)==number)res++;
  }
  cout << res << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int a[100005];
int n, q;
int main() {
  //	freopen("a.txt","r",stdin);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  //	sort(a,a+n);
  cin >> q;
  int res = 0, number;
  for (int i = 0; i < q; i++) {
    cin >> number;
    if (*lower_bound(a, a + n, number) == number)
      res++;
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int a[10010];
int main() {
  int n, m, h = 0, b;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &b);
    for (int j = 0; j < n; j++)
      if (b == a[j]) {
        h++;
        break;
      }
  }
  printf("%d\\n", h);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#define ll long long
using namespace std;
int A[1000010], n;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  int q, k, sum = 0;
  cin >> q;
  for (int j = 0; j < q; ++j) {
    scanf("%d", &k);
    if (*lower_bound(A, A + n, k) == k)
      sum++;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>

void P(int l, int d, int n, int a[], int &h) {
  if (l == d - 1) {
    if (n == a[l] || n == a[d])
      h = 1;
    else
      h = 0;
  }
  int mid = (d + l) / 2;
  if (n < a[mid])
    P(1, mid, n, a, h);
  else if (n > a[mid])
    P(mid + 1, d, n, a, h);
}

int main() {
  int n, q;
  scanf("%d \\n", &n);
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    scanf("%d ", &a[i]);
  scanf("\\n");
  scanf("%d \\n", &q);
  int t[q + 1];
  for (int i = 1; i <= q; i++)
    scanf("%d ", &t[i]);
  scanf("\\n");
  int s = 0;
  int h = 0;
  for (int i = 1; i <= q; i++) {
    P(1, n, t[i], a, h);
    s = s + h;
  }
  printf("%d ", s);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

int binary_searcher(int A[], int key, int n) {
  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 i, q, key, n, 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 (binary_searcher(A, key, n))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int binary_searcher(int A[], int key, int n) {
  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 i, q, key, n, 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 (binary_searcher(A, key, n))
      sum++;
  }
  printf("%d", sum);

  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  long n, q, C = 0;
  cin >> n;
  vector<long> S(n + 1, 0);
  for (long i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  vector<long> T(q, 0);
  for (long i = 0; i < q; i++)
    cin >> T[i];
  for (long i = 0; i < q; i++) {
    long a = 0, b = n;
    while (a < b) {
      if (S[(a + b) / 2] > T[i])
        b = (a + b) / 2;
      else if (S[(a + b) / 2] == T[i]) {
        C++;
        break;
      } else
        a = (a + b) / 2 + 1;
    }
  }
  cout << C << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  long n, q, C = 0;
  cin >> n;
  vector<long> S(n + 1, 0);
  for (long i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  vector<long> T(q, 0);
  for (long i = 0; i < q; i++)
    cin >> T[i];
  for (long i = 0; i < q; i++) {
    long a = 0, b = n;
    while (a < b) {
      if (S[(a + b) / 2] > T[i])
        b = (a + b) / 2 - 1;
      else if (S[(a + b) / 2] == T[i]) {
        C++;
        break;
      } else
        a = (a + b) / 2;
    }
  }
  cout << C << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: 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 < 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;
}

Error: TLE

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 < 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;
}
Problem p02268 (C++) — buggy submission, verdict: 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;
}


Error: -11

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, verdict: Runtime Error

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

int S[100000];
int n, q;
int C;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    int a = 0, b = n;
    while (a < b) {
      int m = (a + b) / 2;
      if (t == S[m]) {
        C++;
        break;
      }
      if (t < S[m])
        b = m;
      else
        a = m + 1;
    }
  }
  cout << C << endl;
}

Error: 0

Fixed submission:

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

int S[10000];
int n, q;
int C;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    int a = 0, b = n;
    while (true) {
      int m = (a + b) / 2;
      if (t == S[m]) {
        C++;
        break;
      }
      if (t < S[m])
        b = m;
      else
        a = m + 1;
      if (a == b)
        break;
    }
  }
  cout << C << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int S[1000000] = {0};
int T[50000] = {0};

int main() {
  int n, q, k, 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[i];
  }
  sort(T, T + q);
  sort(S, S + n);
  int t = 0, s = 0;
  k = -1;
  while (s < n && t < q) {
    if (S[s] == T[t]) {
      if (T[t] != k)
        ++ans;
      ++s;
      k = T[t];
    } else if (S[s] > T[t]) {
      ++t;
    } else if (S[s] < T[t]) {
      ++s;
    }
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int S[1000000] = {0};
int t[50000] = {0};

int main() {
  int n, q, k, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    S[k] = 1;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> k;
    ++t[k];
    ans += t[k] * S[k];
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 8;
int n, q, x, num;
int s[maxn];

bool is_find(int x) {
  int left, right, mid;
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (s[mid] == x)
      return true;
    else if (s[mid] > x) {
      right = mid;
    } else if (s[mid] < x) {
      left = mid + 1;
    }
  }
  return false;
}
int main() {
  scanf("%d", &n);
  num = 0;
  for (int i = 0; i < n; ++i)
    scanf("%d", &s[i]);
  sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &x);
    if (is_find(x))
      num++;
  }
  printf("%d\\n", num);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 8;
int n, q, x, num;
int s[maxn];

bool is_find(int x) {
  int left, right, mid;
  left = 0;
  right = n;
  while (left <= right) {
    mid = (left + right) / 2;
    if (s[mid] == x)
      return true;
    else if (s[mid] > x) {
      right = mid;
    } else if (s[mid] < x) {
      left = mid + 1;
    }
  }
  return false;
}
int main() {
  scanf("%d", &n);
  num = 0;
  for (int i = 0; i < n; ++i)
    scanf("%d", &s[i]);
  sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &x);
    if (is_find(x))
      num++;
  }
  printf("%d\\n", num);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 8;
int n, q, x, num;
int s[maxn];

bool is_find(int x) {
  int left, right, mid;
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (s[mid] == x)
      return true;
    else if (s[mid] > x) {
      right = mid;
    } else if (s[mid] < x) {
      left = mid + 1;
    }
  }
  return false;
}
int main() {
  scanf("%d", &n);
  num = 0;
  fill(s, s + maxn, 0);
  for (int i = 0; i < n; ++i)
    scanf("%d", &s[i]);
  sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &x);

    if (is_find(x)) {
      num++;
    }
  }
  printf("%d\\n", num);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 8;
int n, q, x, num;
int s[maxn];

bool is_find(int x) {
  int left, right, mid;
  left = 0;
  right = n;
  while (left <= right) {
    mid = (left + right) / 2;
    if (s[mid] == x)
      return true;
    else if (s[mid] > x) {
      right = mid;
    } else if (s[mid] < x) {
      left = mid + 1;
    }
  }
  return false;
}
int main() {
  scanf("%d", &n);
  num = 0;
  for (int i = 0; i < n; ++i)
    scanf("%d", &s[i]);
  sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &x);
    if (is_find(x))
      num++;
  }
  printf("%d\\n", num);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int n, m, c = 0, in;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> in;
    int r = 0, l = n;

    while (l > r) {
      //  cout<<in<<" "<<l<<" "<<r<<endl;
      int mid = (l + r) / 2;
      if (a[mid] == in) {
        c++;
        break;
      }

      if (a[mid] < in) {
        r = mid + 1;
      } else {
        l = mid;
      }
    }
  }
  cout << c << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int n, m, c = 0, in;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> in;
    int r = 0, l = n;

    while (l - r > 0) {
      //  cout<<in<<" "<<l<<" "<<r<<endl;
      int mid = (l + r) / 2;
      if (a[mid] == in) {
        c++;
        break;
      }

      if (a[mid] < in) {
        r = mid;
      } else {
        l = mid;
      }
      if (l - r == 0)
        break;
    }
  }
  cout << c << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int A[1000000], n;

/* 二部探索 */
int binarySearch(int key) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1; /* keyを発見 */
    if (key > A[mid])
      left = mid + 1; /* 後半を探索 */
    else if (key < A[mid])
      right = mid; /* 前半を探索 */
  }
  return 0;
}

int main() {
  int i, q, k, sum = 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))
      sum++;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int S[100000];
int T[50000];

bool binarySearch(int key, int n) {
  int head = 0, tail = n - 1;
  int mid = (head + tail) / 2;
  if (key < S[head] || key > S[tail])
    return false;
  while (1) {
    if (S[mid] == key || S[mid + 1] == key)
      return true;
    else if (key < S[mid])
      tail = mid;
    else if (key > S[mid])
      head = mid;
    else if (head == tail - 1 && (key != S[head] && key != S[tail]))
      return false;
    mid = (head + tail) / 2;
  }
}

int main() {
  int n, q, count = 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++) {
    if (binarySearch(T[i], n))
      count++;
  }
  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

bool binarySearch(int *n, int countN, int key) {
  int left = 0, right = countN, middle;
  while (left < right) {
    middle = (left + right) / 2;
    if (n[middle] == key) {
      return true;
    } else if (key < n[middle]) {
      right = middle;
    } else {
      left = middle + 1;
    }
  }

  return false;
}

int main() {
  int countN, n[999999], countQ;

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

  cin >> countQ;
  int t = 0, match = 0;
  for (int i = 0; i < countQ; i++) {
    cin >> t;
    if (binarySearch(n, countN, t))
      match++;
  }

  cout << match << endl;

  /***
  cin >> countQ;
  for (int i = 0; i < countQ; i++) {
      cin >> t[i];
  }

  int match = 0;
  for (int i = 0; i < countQ; i++) {
      if (binarySearch(n, countN, t[i])) match++;
  }

  cout << match << endl;
  ***/

  /***
  int match = 0;
  for (int j = 0; j < countQ; j++) {
      for (int i = 0; i < countN; i++) {
          if (n[i] == t[j]) {
              match++;
              break;
          }
      }
  }
  cout << match << endl;
  ***/
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

bool binarySearch(int *n, int countN, int key) {
  int left = 0, right = countN, middle;
  while (left < right) {
    middle = (left + right) / 2;
    if (n[middle] == key) {
      return true;
    } else if (key < n[middle]) {
      right = middle;
    } else {
      left = middle + 1;
    }
  }

  return false;
}

int main() {
  int countN, n[10000], countQ;

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

  cin >> countQ;
  int t, match = 0;
  for (int i = 0; i < countQ; i++) {
    scanf("%d", t);
    if (binarySearch(n, countN, t))
      match++;
  }

  cout << match << endl;

  /***
  cin >> countQ;
  for (int i = 0; i < countQ; i++) {
      cin >> t[i];
  }

  int match = 0;
  for (int i = 0; i < countQ; i++) {
      if (binarySearch(n, countN, t[i])) match++;
  }

  cout << match << endl;
  ***/

  /***
  int match = 0;
  for (int j = 0; j < countQ; j++) {
      for (int i = 0; i < countN; i++) {
          if (n[i] == t[j]) {
              match++;
              break;
          }
      }
  }
  cout << match << endl;
  ***/
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <sstream>

int main() {
  int n1, cnt = 0;
  std::cin >> n1;
  int S[n1];
  for (int i = 0; i < n1; i++) {
    std::cin >> S[i];
  }
  int n2;
  std::cin >> n2;
  int T[n2];
  for (int i = 0; i < n2; i++) {
    std::cin >> T[i];
  }

  int left = 0;
  int right = n1 - 1;
  int c, num;
  for (int i = 0; i < n2; i++) {
    num = T[i];
    left = 0;
    right = n1 - 1;
    while (left <= right) {
      c = int((left + right) / 2.0);
      if (S[c] > num) {
        right = c - 1;
      }
      if (S[c] < num) {
        left = c + 1;
      }
      if (S[c] == num) {
        cnt++;
        break;
      }
    }
  }
  std::cout << cnt << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>

int main() {
  int n1, cnt = 0;
  std::cin >> n1;
  int S[n1];
  for (int i = 0; i < n1; i++) {
    std::cin >> S[i];
  }
  int n2;
  std::cin >> n2;
  int T[n2];
  for (int i = 0; i < n2; i++) {
    std::cin >> T[i];
  }

  int left = 0;
  int right = n1 - 1;
  int c, num;
  for (int i = 0; i < n2; i++) {
    num = T[i];
    left = 0;
    right = n1 - 1;
    while (left < right) {
      c = int((left + right) / 2.0);
      if (S[c] > num) {
        right = c;
      }
      if (S[c] < num) {
        left = c;
      }
      if (S[c] == num) {
        cnt++;
        break;
      }
    }
  }
  std::cout << cnt << '\\n';
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int BinSearch(int nA[], int nKey, int n) {
  int nLeft = 0, nRight = n;
  int nMid = (nLeft + nRight) / 2;
  while (nLeft < nRight) {
    if (nA[nMid] < nKey) {
      nLeft = nMid + 1;
    } else if (nA[nMid] > nKey) {
      nRight = nMid;
    } else {
      return 1;
    }
    nMid = (nLeft + nRight) / 2;
  }
  return 0;
}

int main() {
  int nA[100000] = {0};
  int n = 0, q = 0, nKey = 0, nSum = 0;
  int ni = 0;
  cin >> n;
  while (cin >> nA[ni], ni < (n - 1)) {
    ni++;
  }
  cin >> q;
  for (ni = 0; ni < q; ni++) {
    cin >> nKey;
    if (BinSearch(nA, nKey, n)) {
      nSum++;
    }
  }
  cout << nSum << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int BinSearch(int nA[], int nKey, int n) {
  int nLeft = 0, nRight = n;
  int nMid = (nLeft + nRight) / 2;
  while (nLeft < nRight) {

    if (nA[nMid] < nKey) {
      nLeft = nMid;
    } else if (nA[nMid] > nKey) {
      nRight = nMid;
    } else {
      return nMid != n;
    }
    nMid = (nLeft + nRight) / 2;
  }
}

int main() {
  int nA[100000] = {0};
  int n = 0, q = 0, nKey = 0, nSum = 0;
  int ni = 0;
  cin >> n;
  while (cin >> nA[ni], ni < (n - 1)) {
    ni++;
  }
  cin >> q;
  for (ni = 0; ni < q; ni++) {
    cin >> nKey;
    if (BinSearch(nA, nKey, n)) {
      nSum++;
    }
  }
  cout << nSum << endl;
  return 0;
}