alexjercan/bugnet huggingface dataset:
Problem p02268 (C++) — buggy submission, verdict: 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) {
    if (FindByBinarySearch(T[i], S, 0, n - 1)) {
      ++C;
    }
  }

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

  return 0;
}

Error: TLE

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) {
    for (int j = 0; j < n; ++j) {
      if (FindByBinarySearch(T[i], S, 0, n - 1)) {
        ++C;
        break;
      }
    }
  }

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

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

#include <iostream>

using namespace std;

bool BSearch(int A[], unsigned int sp, unsigned int ep,
             int n) { // あり、なしのみboolで返す
  int mid = (ep + sp) / 2;
  int l = sp;
  int r = ep;
  while (l <= r) {
    mid = (l + r) / 2;
    if (A[mid] == n) {
      return 1;
    } else if (A[mid] < n) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return 0;
}

int main(void) {
  int n, q;
  int counter = 0;
  int S[100000];
  int 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];
  }

  for (int i = 0; i < q; i++) {
    if (BSearch(S, 0, n, T[i])) {
      counter++;
      // cout << T[i] << "is found!" << endl;
    } else {
      // cout << T[i] << "is not found" << endl;
    }
  }
  cout << counter << endl;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool BSearch(int A[], unsigned int sp, unsigned int ep,
             int n) { // あり、なしのみboolで返す
  int mid = (ep - sp) / 2;
  if (mid == n)
    return 1;
  if (sp > ep)
    return 0;
  else if (n < mid)
    return BSearch(A, sp, mid - 1, n);
  else
    return BSearch(A, mid + 1, ep, n);
}

int main(void) {
  int n, q;
  int counter = 0;
  int S[100000];
  int 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];
  }
  for (int i = 0; i < n; i++) {
    if (BSearch(T, 0, q, S[i]) && S[i] != S[i - 1])
      counter++;
  }
  cout << counter << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
vector<int> A;

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

int main() {
  int q, a, k = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    A.push_back(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a;
    k += bisearch(a);
  }
  cout << k << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int bisearch(vector<int> A, int a, int left, int right) {
  int mid = (left + right) / 2;
  if (A[mid] == a)
    return 1;
  else if (left == (right - 1)) {
    if (A[left] == a || A[right] == a)
      return 1;
    else
      return 0;
  } else
    return (bisearch(A, a, left, mid) || bisearch(A, a, mid, right));
}

int main() {
  int n, q, a, k = 0;
  cin >> n;
  vector<int> A;
  for (int i = 0; i < n; i++) {
    cin >> a;
    A.push_back(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a;
    k += bisearch(A, a, 0, n - 1);
  }
  cout << k << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int BinarySearch(int a[], int key, int start, int end) {
  int Start = start, End = end;
  int med;
  while (Start != End) {
    med = (Start + End) / 2;
    if (a[med] == key)
      return med;
    else if (a[med] < key)
      Start = med + 1;
    else
      End = med;
  }
  if (a[Start] == key)
    return Start;
  return -1;
}

int main() {
  int n, q;
  int S[100000], T[50000];
  int cnt = 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 j = 0; j < q; j++) {
    if (BinarySearch(S, T[j], 0, n) != -1) {
      cnt++;
    }
  }
  printf("%d\\n", cnt);

  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>

int BinarySearch(int a[], int key, int start, int end) {
  int Start = start, End = end;
  int med;
  while (Start != End) {
    med = (Start + End) / 2;
    if (a[med] == key)
      return med;
    else if (a[med] < key)
      Start = med + 1;
    else
      End = med - 1;
  }
  if (a[Start] == key)
    return Start;
  return -1;
}

int main() {
  int n, q;
  int S[100000], T[50000];
  int cnt = 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 j = 0; j < q; j++) {
    if (BinarySearch(S, T[j], 0, n - 1) != -1) {
      cnt++;
    }
  }
  printf("%d\\n", cnt);

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

#include <iostream>
using namespace std;

bool search(int S[], int n, int num) {
  int min = 0;
  int max = n;
  int pos = n / 2;
  while (max - min > 1) {
    if (S[pos] == num) {
      return true;
    }
    if (S[pos] > num) {
      max = pos;
      pos -= (min + pos) / 2 + (min + pos) % 2;
    } else {
      min = pos;
      pos += (max - pos) / 2 + (max - pos) % 2;
    }
  }
  return false;
}

int main() {

  int n, q;
  int S[100000];
  int T[50000];
  int cnt = 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 (search(S, n, T[i]))
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool search(int S[], int n, int num) {
  int min = 0;
  int max = n - 1;
  int pos = n / 2;
  while (min < max) {
    if (S[pos] == num) {
      return true;
    }

    if (S[pos] > num) {
      max = pos;
      pos -= (min + pos) / 2 + (min + pos) % 2;
    } else {
      min = pos;
      pos += (max - pos) / 2 + (max - pos) % 2;
    }
  }
  return false;
}

int main() {

  int n, q;
  int S[100000];
  int T[50000];
  int cnt = 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 (search(S, n, T[i]))
      cnt++;
  }

  cout << cnt << endl;

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

#include <cstdio>

int A[100000], n;

int binarysearch(int key) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (right + left) / 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() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", A + i);
  }

  int q;
  scanf("%d", &q);

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int binarysearch(int *A, int n, int key) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (right + left) / 2;

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

int main() {
  int n;
  scanf("%d", &n);

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

  int q;
  scanf("%d", &q);

  int key, sum = 0;
  for (int 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, verdict: Time Limit Exceeded

#include <stdio.h>
int n, q, i, j, cou, a, b, d, m;
bool w;
int main() {
  scanf("%d", &n);
  int s[n];
  for (j = 0; j < n; j++) {
    scanf("%d", &s[j]);
  }
  scanf("%d", &q);
  int t[q];
  for (i = 0; i < q; i++) {
    scanf("%d", &t[i]);
  }
  cou = 0;

  for (i = 0; i < q; i++) {
    a = n;
    b = 0;
    d = 1;
    w = true;
    while ((a - b) / 2 >= d && w == true) {
      m = (a + b) / 2;
      if (t[i] < s[m]) {
        a = m;
      } else if (t[i] > s[m]) {
        b = m;
      } else
        w = false;
    }
    if (s[m] == t[i] || s[a] == t[i])
      cou = cou + 1;
  }
  printf("%d\\n", cou);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
int n, q, i, j, cou, a, b, d, m;
bool w;
int main() {
  scanf("%d", &n);
  int s[n];
  for (j = 0; j < n; j++) {
    scanf("%d", &s[j]);
  }
  scanf("%d", &q);
  int t[q];
  for (i = 0; i < q; i++) {
    scanf("%d", &t[i]);
  }
  cou = 0;

  for (i = 0; i < q; i++) {
    a = n;
    b = 0;
    d = 0.5;
    w = true;
    while ((a - b) / 2 >= d && w == true) {
      m = (a + b) / 2;
      if (t[i] < s[m]) {
        a = m;
      } else if (t[i] > s[m]) {
        b = m;
      } else
        w = false;
    }
    if (s[m] == t[i])
      cou = cou + 1;
  }
  printf("%d", cou);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, q, S[100000], T, res = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  sort(S, S + n);
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T;
    if (binary_search(S, S + n, T))
      res++;
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, q, S[10000], T, res = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  sort(S, S + n);
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T;
    if (binary_search(S, S + n, T))
      res++;
  }
  cout << res << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

int binary_search(int start, int end, long int *s, int key) {
  // cout << "binary_search with " << start << " " << end << " " << key << endl;
  int match = 0;
  if (start != end) {
    int x;
    x = start + floor((end - start) / 2);
    if (s[x] == key) {
      match = 1;
    } else if (s[x] > key) {
      match = binary_search(start, max(x - 1, start), s, key);
    } else {
      match = binary_search(min(x + 1, end), end, s, key);
    }
  } else {
    if (s[start] == key)
      match = 1;
  }
  // cout << "return " << match << endl;
  return match;
}

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

  int count = 0;
  for (int i = 0; i < q; ++i) {
    count += binary_search(0, n - 1, s, t[i]);
    // cout << "count is " << count << endl;
  }

  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

int binary_search(int start, int end, long int *s, int key) {
  // cout << "binary_search with " << start << " " << end << " " << key << endl;
  int match = 0;
  if (start != end) {
    int x;
    x = start + floor((end - start) / 2);
    if (s[x] == key) {
      match = 1;
    } else if (s[x] > key) {
      match = binary_search(start, x - 1, s, key);
    } else {
      match = binary_search(x + 1, end, s, key);
    }
  } else {
    if (s[start] == key)
      match = 1;
  }
  // cout << "return " << match << endl;
  return match;
}

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

  int count = 0;
  for (int i = 0; i < q; ++i) {
    count += binary_search(0, n - 1, s, t[i]);
    // cout << "count is " << count << endl;
  }

  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

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


Error: 0

Fixed submission:

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

#include <iostream>
#include <string>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  cout << count << endl;

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

#include <iostream>
#include <vector>

using namespace std;

bool find(const vector<int> &S, int begin, int end, int x) {
  if (end - begin == 1) {
    return S[begin] == x;
  }
  int mid = (begin + end) / 2;
  if (x < S[mid]) {
    return find(S, begin, mid, x);
  } else if (S[mid] < x) {
    return find(S, mid, end, x);
  } else {
    return true;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  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 count = 0;
  for (int i = 0; i < q; ++i) {
    if (find(S, 0, S.size(), T[i])) {
      ++count;
    }
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

bool find(const vector<int> &S, int begin, int end, int x) {
  if (end - begin <= 0) {
    return false;
  }
  int mid = (begin + end) / 2;
  if (x < S[mid]) {
    return find(S, begin, mid, x);
  } else if (S[mid] < x) {
    return find(S, mid, end, x);
  } else {
    return true;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  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 count = 0;
  for (int i = 0; i < q; ++i) {
    if (find(S, 0, S.size(), T[i])) {
      ++count;
    }
  }
  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

bool Binary(int x, int s[], int n) {
  int b = 0;
  int t = n - 1;

  while (b <= t) {
    int mid = (b + t) / 2;

    if (s[mid] == x) {
      return true;
    } else if (s[mid] < x) {
      b = mid + 1;
    } else {
      t = mid - 1;
    }
  }

  return false;
}

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 count = 0;
  for (int i = 0; i < q; i++) {
    if (Binary(t[i], s, n) != false) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool Linear(int x, int s[], int n) {
  for (int i = 0; i < n; i++) {
    if (s[i] == x)
      return true;
  }

  return false;
}

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 count = 0;
  for (int i = 0; i < q; i++) {
    if (Linear(t[i], s, n) != 0) {
      count++;
    }
  }

  cout << count << endl;

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

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

bool binary(vector<int> v, int x) {
  int l, r, med;
  l = 0;
  r = v.size() - 1;
  while (l < r) {
    if (v[r] == x or v[l] == x)
      return true;
    med = (r + l) / 2;
    if (x > v[med])
      l = med + 1;
    else if (x < v[med])
      r = med - 1;
    else
      return true;
  }
  return false;
}

int main() {
  int n, q, c = 0;
  vector<int> s, t;

  // input
  cin >> n;
  s.resize(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  cin >> q;
  t.resize(q);
  for (int i = 0; i < q; i++)
    cin >> t[i];

  // process
  for (int i = 0; i < q; i++)
    if (binary(s, t[i]))
      c++;
  cout << c << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
void print(std::vector<int> &values) {
  for (int x : values) {
    std::cout << x << " ";
  }
  std::cout << std::endl;
}
// Time Limit Exceeded 1.98s

bool binarySearch(int x, vector<int> list, int start, int end) {
  int pivot = (int)(start + end) / 2.0;
  int length = end - start;
  if (length == -1) {
    return false;
  } else if (length == 0) {
    if (x == list[pivot]) {
      return true;
    } else {
      return false;
    }
  } else {
    if (x == list[pivot]) {
      return true;
    } else if (x > list[pivot]) {
      return binarySearch(x, list, pivot + 1, end);
    } else {
      return binarySearch(x, list, start, pivot - 1);
    }
  }
}

/*
bool binarySearch()
{
  int left=0;
  int right=n;

  while(left<right){
    int mid=(int)(left+right)/2
      if(list[mid]==
      }
*/
int main() {
  int n, q;
  cin >> n;
  vector<int> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  // sort(s.begin(),s.end());

  vector<int> t(q);
  for (int i = 0; i < q; i++) {
    cin >> t[i];
  }
  int cnt = 0;
  for (int i = 0; i < q; i++) {
    if (binarySearch(t[i], s, 0, n - 1) == true) {
      // cout<<t[i]<<"=>true"<<endl;
      cnt++;
    } else {
      // cout<<t[i]<<"=>false"<<endl;
    }
    // cout<<cnt<<endl;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

int main() {
  int r = 0, n, m, a, A[100000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a;
    if (binarySearch(A, n, a))
      r++;
  }
  cout << r << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int b, i, r = 0, n, m, a[100000];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> m;
  for (int j = 0; j < m; j++) {
    cin >> b;
    for (int l = 0; l < i; l++) {
      if (a[l] == b) {
        r++;
        break;
      }
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int linear(int a[], int key, int n);
int main() {
  int i, j, n, q, c = 0;
  int s[100000], t[100000];

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

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

  for (i = 0; i < q; i++) {
    c += linear(s, t[i], n);
  }
  cout << c << endl;
}
int linear(int a[], int key, int n) {
  int left = 0;
  int right = n;
  int mid;
  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;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int linear(int a[], int key, int n);
int main() {
  int i, j, n, q, c = 0;
  int s[1000000], t[1000000];

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

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

  for (i = 0; i < q; i++) {
    c += linear(s, t[i], n);
  }
  cout << c << endl;
}
int linear(int a[], int key, int n) {
  int i = 0;
  a[n] = key;
  while (a[i] != key) {
    i++;
  }
  if (i >= n) {
    return 0;
  } else {
    return 1;
  }
}
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 <iostream>
#include <vector>
using namespace std;

int main() {
  long n, q, C = 0;
  cin >> n;
  vector<long> S(n);
  for (long i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  vector<long> T(q);
  for (long i = 0; i < q; i++)
    cin >> T[i];
  for (long i = 0; i < q; i++) {
    for (long j = 0; j < n; j++) {
      if (T[i] == S[j]) {
        C++;
        break;
      }
      if (T[i] < S[j]) {
        break;
      }
    }
  }
  cout << C << endl;
  return 0;
}