alexjercan/bugnet huggingface dataset:
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int num[n], ans;
    for (int i = 0; i < n; i++)
      cin >> num[i];
    cin >> ans;
    int serch, kawa[2] = {0, n - 1};
    for (int i = 1; kawa[1] >= kawa[0]; i++) {
      serch = (kawa[0] + kawa[1]) / 2;
      if (num[serch] == ans) {
        cout << i << endl;
        break;
      }
      if (num[serch] > ans)
        kawa[1] = serch - 1;
      else
        kawa[0] = serch + 1;
      if (kawa[0] > kawa[1])
        cout << i << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int num[n], ans;
    for (int i = 0; i < n; i++)
      cin >> num[i];
    cin >> ans;
    int serch, kawa[2] = {0, n - 1};
    for (int i = 1;; i++) {
      serch = (kawa[0] + kawa[1]) / 2;
      if (num[serch] == ans || kawa[1] - kawa[0] == 0) {
        cout << i << endl;
        break;
      }
      if (num[serch] > ans)
        kawa[1] = serch - 1;
      else
        kawa[0] = serch + 1;
    }
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int num[101], r, l, c;
  int kazu, target, t;
  int i;

  while (1) {
    t = 0;
    cin >> kazu;

    if (kazu == 0) {
      break;
    }

    for (i = 0; i < kazu; i++) {
      cin >> num[i];
    }

    cin >> target;

    l = 0;
    r = (kazu - 1);
    t = 0;
    while (r >= l) {
      c = (l + r) / 2;
      t++;
      if (num[c] == target) {
        break;
      } else if (r <= l) {
        break;
      } else if (num[c] < target) {
        l = c + 1;
      } else {
        r = c - 1;
      }
    }

    cout << t << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int num[101], r, l, c;
  int kazu, target, t;
  int i;

  while (1) {
    t = 0;
    cin >> kazu;

    if (kazu == 0) {
      break;
    }

    for (i = 0; i < kazu; i++) {
      cin >> num[i];
    }

    cin >> target;

    l = 0;
    r = (kazu - 1);
    t = 0;
    while (1) {
      c = (l + r) / 2;
      t++;
      if (num[c] == target) {
        break;
      } else if (r == l) {
        break;
      } else if (num[c] < target) {
        l = c + 1;
      } else {
        r = c - 1;
      }
    }

    cout << t << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

const int MAX = 101;
int data[MAX];
int n;
int ans;
int wanted;

void solve() {
  int start = 0;
  int end = n - 1;
  // sort(data,data+n);
  while (1) {
    if (start > end)
      break;
    ans++;
    int now = (start + end) / 2;
    // cout <<"now  "  <<now << endl;
    if (data[now] == wanted)
      break;
    if (data[now] > wanted)
      end = now - 1;
    else
      start = now + 1;
  }
}

int main() {
  while (cin >> n && n) {
    ans = 0;
    for (int i = 0; i < n; i++)
      cin >> data[i];
    cin >> wanted;
    solve();
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

const int MAX = 101;
int data[MAX];
int n;
int ans;
int wanted;

void solve() {
  int start = 0;
  int end = n - 1;
  // sort(data,data+n);
  while (1) {
    ans++;
    int now = (start + end) / 2;
    // cout <<"now  "  <<now << endl;
    if (data[now] == wanted || start > end)
      break;
    if (data[now] > wanted)
      end = now;
    else
      start = now + 1;
  }
}

int main() {
  while (cin >> n && n) {
    ans = 0;
    for (int i = 0; i < n; i++)
      cin >> data[i];
    cin >> wanted;
    solve();
    cout << ans << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  while (cin >> N, N) {
    vector<int> v(N);
    for (auto &i : v)
      cin >> i;

    int x;
    cin >> x;
    int l = 0, r = N - 1;

    int count = 0;
    while (true) {
      int m = (r + l) / 2;
      count++;
      if (v[m] == x)
        break;

      if (v[m] < x) {
        l = m + 1;
      } else {
        r = m - 1;
      }

      if (l > r)
        break;
    }

    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  while (cin >> N, N) {
    vector<int> v(N);
    for (auto &i : v)
      cin >> i;

    int x;
    cin >> x;
    int l = 0, r = N - 1;

    int count = 0;
    while (true) {
      int m = (r + l) / 2;
      count++;
      if (v[m] == x)
        break;
      if (l == r)
        break;

      if (v[m] < x) {
        l = m + 1;
      } else {
        r = m - 1;
      }
    }

    cout << count << endl;
  }

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

#include <iostream>

int search(int *n, int key, int p, int l, int count) {
  int c = (p + l) / 2;
  if (n[c] == key)
    return count;
  else if (p > l)
    return count - 1;
  else if (n[c] < key)
    return search(n, key, c + 1, l, count + 1);
  else
    return search(n, key, p, c - 1, count + 1);
}

int main() {
  int *data, n, key;
  while (true) {
    std::cin >> n;
    if (n == 0)
      return 0;
    data = new int[n];
    for (int i = 0; i < n; i++)
      std::cin >> data[i];
    std::cin >> key;
    std::cout << search(data, key, 0, n - 1, 1) << std::endl;
    delete[] data;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

int search(int *n, int key, int p, int l, int count) {
  int c = (p + l) / 2;
  if (n[c] == key || p == l)
    return count;
  else if (n[c] < key)
    return search(n, key, c + 1, l, count + 1);
  else
    return search(n, key, p, c - 1, count + 1);
}

int main() {
  int *data, n, key;
  while (true) {
    std::cin >> n;
    if (n == 0)
      return 0;
    data = new int[n];
    for (int i = 0; i < n; i++)
      std::cin >> data[i];
    std::cin >> key;
    std::cout << search(data, key, 0, n - 1, 1) << std::endl;
    delete[] data;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    int A[100], Target;
    for (int i = 0; i < N; i++)
      cin >> A[i];
    cin >> Target;

    int l = 0, r = N - 1, piv;
    int cnt = 0;
    while (r - l >= 0) {
      cnt++;
      int mid = (l + r) / 2;
      if (A[mid] > Target)
        r = mid - 1;
      else if (A[mid] < Target)
        l = mid + 1;
      else
        break;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    int A[100], Target;
    for (int i = 0; i < N; i++)
      cin >> A[i];
    cin >> Target;

    int l = 0, r = N - 1, piv;
    int cnt = 0;
    while (true) {
      cnt++;
      if (l == r)
        break;
      int mid = (l + r) / 2;
      if (A[mid] > Target)
        r = mid - 1;
      else if (A[mid] < Target)
        l = mid + 1;
      else
        break;
    }
    printf("\\t%d\\n", cnt);
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int i;
  int a[100], n, p;
  int mn, mx;
  while (cin >> n && n) {
    for (i = 0; i < n; i++)
      cin >> a[i];
    cin >> p;
    mn = 0;
    mx = n - 1;
    for (i = 1;; i++) {
      if (0) {
      } else if (a[(mn + mx) / 2] == p) {
        break;
      } else if (a[(mn + mx) / 2] < p) {
        if (mn == mx)
          break;
        mn = (mn + mx) / 2 + 1;
      } else if (a[(mn + mx) / 2] > p) {
        if (mn == mx || mn == (mn + mx) / 2)
          break;
        mx = (mn + mx) / 2 - 1;
      }
    }
    cout << i << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i;
  int a[100], p, n, c, mn, mx;
  for (;;) {
    cin >> n;
    if (n == 0)
      break;
    for (i = 0; i < n; i++)
      cin >> a[i];
    cin >> p;
    mn = 0;
    mx = n - 1;
    for (c = 1;; c++) {
      if (mn == mx)
        break;
      if (a[(mn + mx) / 2] == p)
        break;
      else if (a[(mn + mx) / 2] > p)
        mx = (mn + mx) / 2 - 1;
      else
        mn = (mn + mx) / 2 + 1;
    }
    cout << c << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

int main() {
  int array[1000];
  int n;

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

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

    int l = 0;
    int r = n - 1;
    int cnt = 0;
    while (1) {
      int m = (l + r) / 2;

      cnt++;

      if (q == array[m]) {
        break;
      }

      else if (l == r) {
        break;
      }

      else if (q < array[m]) {
        r = m - 1;
        if (r < l) {
          break;
        }
      }

      else if (q > array[m]) {
        l = m + 1;
        if (l > r) {
          break;
        }
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

int main() {
  int array[1000];
  int n;

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

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

    int l = 0;
    int r = n - 1;
    int cnt = 0;
    while (1) {
      int m = (l + r) / 2;

      cnt++;

      if (q == array[m]) {
        break;
      }

      else if (l == r) {
        break;
      }

      else if (q < array[m]) {
        r = m - 1;
      }

      else if (q > array[m]) {
        l = m + 1;
      }
    }

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

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

//
//
#include <iostream>
#include <vector>

using namespace std;

int nibo(int, vector<int>);

int main(void) {
  int n;
  vector<int> ans;

  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    vector<int> data(n + 1, 0);
    for (int i = 0; i < n + 1; i++) {
      cin >> data[i];
    }
    cout << nibo(n, data) << endl;
  }

  return 0;
}

int nibo(int n, vector<int> data) {
  int times = 0;
  int right = n - 1;
  int left = 0;
  int mid;
  int target = data[n];
  while ((right - left) >= 0) {
    times++;
    mid = (right + left) / 2;
    if (data[mid] == target) {
      break;
    }
    if (data[mid] < target) {
      left = mid + 1;
    }
    if (data[mid] > target) {
      right = mid - 1;
    }
  }
  return times;
}

Error: TLE

Fixed submission:

//
//
#include <iostream>
#include <vector>

using namespace std;

int nibo(int, vector<int>);

int main(void) {
  int n;
  vector<int> ans;

  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    vector<int> data(n + 1, 0);
    for (int i = 0; i < n + 1; i++) {
      cin >> data[i];
    }
    cout << nibo(n, data) << endl;
  }

  return 0;
}

int nibo(int n, vector<int> data) {
  int times = 0;
  int right = n - 1;
  int left = 0;
  int mid;
  int target = data[n];

  while (true) {
    times++;
    mid = (right + left) / 2;
    if ((right - left) == 0) {
      break;
    }
    if (data[mid] == target) {
      break;
    }
    if (data[mid] < target) {
      left = mid + 1;
    }
    if (data[mid] > target) {
      right = mid - 1;
    }
  }

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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    vector<long long int> data(n);
    int left = 0;
    int right = n - 1;
    for (int i = 0; i < n; i++) {
      cin >> data[i];
    }
    int counter = 0;
    int tansa;
    cin >> tansa;
    while (true) {
      int half = (left + right) / 2;
      counter++;
      if (left == right) {
        break;
      }
      if (data[half] == tansa) {
        break;
      } else if (data[half] < tansa) {
        left = half + 1;
      } else {
        right = half - 1;
      }
      if (left > right) {
        break;
      }
    }
    cout << counter << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    vector<long long int> data(n);
    int left = 0;
    int right = n - 1;
    for (int i = 0; i < n; i++) {
      cin >> data[i];
    }
    int counter = 0;
    int tansa;
    cin >> tansa;
    while (true) {
      int half = (left + right) / 2;
      counter++;
      if (left == right) {
        break;
      }
      if (data[half] == tansa) {
        break;
      } else if (data[half] < tansa) {
        left = half + 1;
      } else {
        right = half - 1;
      }
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {

  int n;
  while (cin >> n && n != 0) {
    vector<int> nums;
    int t;
    for (int i = 0; i < n; i++) {
      cin >> t;
      nums.push_back(t);
    }
    int s;
    cin >> s;
    int ub = n - 1;
    int lb = 0;
    int cnt = 0;
    while (1) {
      if (lb < 0 || ub < 0 || ub >= n || lb >= n || ub < lb)
        break;
      cnt++;
      int mid = ub + lb;
      mid /= 2;
      if (nums[mid] == s || ub == lb) {
        break;
      } else if (nums[mid] > s) {
        ub = mid - 1;
      } else if (nums[mid] < s) {
        lb = mid + 1;
      }
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {

  int n;
  while (cin >> n && n != 0) {
    vector<int> nums;
    int t;
    for (int i = 0; i < n; i++) {
      cin >> t;
      nums.push_back(t);
    }
    int s;
    cin >> s;
    int ub = n - 1;
    int lb = 0;
    int cnt = 0;
    while (1) {
      if (lb < 0 || ub < 0 || ub >= n || lb >= n)
        break;
      int mid = ub + lb;
      mid /= 2;
      cnt++;
      if (nums[mid] == s || ub == lb) {
        break;
      } else if (nums[mid] > s) {
        ub = mid - 1;
      } else if (nums[mid] < s) {
        lb = mid + 1;
      }
    }
    cout << cnt << endl;
  }

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

#include <stdio.h>

int main(void) {
  int x[100] = {0};
  int st, fn, a, n, i, ty, kai;
  while (0 == 0) {
    scanf("%d", &n);
    if (n == 0)
      break;
    kai = 1;
    st = 0;
    fn = n - 1;
    for (i = 0; i < n; i++)
      scanf("%d", &x[i]);
    scanf("%d", &a);
    while (0 == 0) {
      ty = (st + fn) / 2;
      if (a == x[ty] || fn - st == 0)
        break;
      else if (fn - st < 0) {
        kai--;
        break;
      } else if (a > x[ty])
        st = ty + 1;
      else
        fn = ty - 1;
      kai++;
    }
    printf("%d\\n", kai);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  int x[100] = {0};
  int st, fn, a, n, i, ty, kai;
  while (0 == 0) {
    scanf("%d", &n);
    if (n == 0)
      break;
    kai = 1;
    st = 0;
    fn = n - 1;
    for (i = 0; i < n; i++)
      scanf("%d", &x[i]);
    scanf("%d", &a);
    while (a != x[ty] || fn - st > 0) {
      ty = (st + fn) / 2;
      if (a > x[ty])
        st = ty + 1;
      else
        fn = ty - 1;
      kai++;
    }
    printf("%d\\n", kai);
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int n[101];
int m;
int ans;
int N;

void func(int j, int k) {
  if (j > k) {
    return;
  } else {
    ans++;
    if (n[(j + k) / 2] < m) {
      func((j + k) / 2 + 1, k);
    } else if (n[(j + k) / 2] > m) {
      func(j, (j + k) / 2 - 1);
    }
  }
  return;
}

int main() {

  while (1) {
    cin >> N;
    if (N == 0) {
      break;
    }
    ans = 0;
    for (int i = 0; i < N; i++) {
      cin >> n[i];
    }
    cin >> m;
    func(0, N - 1);
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n[101];
int m;
int ans;
int N;

void func(int j, int k) {
  ans++;
  if (n[(j + k) / 2] == m || j == k) {
    return;
  } else {
    if (n[(j + k) / 2] < m) {
      func((j + k) / 2 + 1, k);
    } else if (n[(j + k) / 2] > m) {
      func(j, (j + k) / 2 - 1);
    }
  }
  return;
}

int main() {

  while (1) {
    cin >> N;
    if (N == 0) {
      break;
    }
    ans = 0;
    for (int i = 0; i < N; i++) {
      cin >> n[i];
    }
    cin >> m;
    func(0, N - 1);
    cout << ans << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define INF 1000000001
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
int main() {
  int n;
  while (cin >> n && n) {
    int lb = -1, ub = n, a[n], cnt = 0, k;
    REP(i, n) cin >> a[i];
    cin >> k;
    while (ub - lb > 1) {
      int mid = (lb + ub) / 2;
      cnt++;
      if (k == a[mid])
        break;
      if (a[mid] > k)
        ub = mid;
      else
        lb = mid;
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define INF 1000000001
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
int main() {
  int n;
  while (cin >> n && n) {
    int r = n - 1, l = 0, a[n], cnt = 0, k;
    REP(i, n) cin >> a[i];
    cin >> k;
    while (r != l) {
      int mid = (r + l) / 2;
      cnt++;
      if (k == a[mid])
        break;
      if (k > a[mid])
        l = mid + 1;
      else
        r = mid - 1;
    }
    cout << cnt + 1 << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int a[100];
int n, N;

int binary_search(int N) {

  int r = n - 1;
  int l = 0;
  int cnt = 0;

  while (r >= l) {
    int x = (r + l) / 2;
    cnt++;
    if (a[x] < N)
      l = x + 1;
    else if (N < a[x])
      r = x - 1;
    else
      break;
  }
  return cnt;
}

int main(void) {

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

    cin >> N;

    cout << binary_search(N) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int a[100000];
int n;

int binary_search(int N) {

  int r = n;
  int l = 0;
  int cnt = 0;
  int x = l;

  while (1) {

    x = (r + l) / 2;

    if (a[x] == N) {
      // cout << "match" << endl;
      cnt++;
      break;
    } else if (a[x] < N) {
      // cout << "right" << endl;
      cnt++;
      l = x;
    } else if (N < a[x]) {
      // cout << "left" << endl;
      cnt++;
      r = x;
    }
  }
  return cnt;
}

int main(void) {

  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int N;
    cin >> N;
    sort(a, a + n);
    cout << binary_search(N) << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

signed main() {
  int n, x;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vi d(n);
    rep(i, 0, n) { cin >> d[i]; }
    cin >> x;
    int cnt = 0, l = 0, r = n - 1;
    while (l <= r) {
      cnt++;
      int mid = (l + r) / 2;
      if (d[mid] > x) {
        r = mid - 1;
      } else if (d[mid] < x) {
        l = mid + 1;
      } else {
        break;
      }
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

signed main() {
  int n, x;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vi d(n);
    rep(i, 0, n) { cin >> d[i]; }
    cin >> x;
    int cnt = 0, l = 0, r = n - 1;
    while (1) {
      int mid = (l + r) / 2;
      cnt++;
      if (r == l)
        break;
      if (d[mid] > x) {
        r = mid - 1;
      } else if (d[mid] < x) {
        l = mid + 1;
      } else if (d[mid] == x) {
        break;
      }
    }
    cout << cnt << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int T[100010];
int find(int m, int x) {
  int r = 0, l = m - 1, ans = 0;
  while (1) {
    if (r > l)
      break;
    ans++;
    if (T[(r + l) / 2] == x)
      return ans;
    if (T[(r + l) / 2] > x)
      l = (r + l) / 2 - 1;
    else if (T[(r + l) / 2] < x)
      r = (r + l) / 2 + 1;
  }
  return ans;
}
int main() {
  int n, k, num, ans;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> T[i];
    }
    cin >> k;
    ans = find(n, k);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int T[100010];
int find(int m, int x) {
  int r = 0, l = m - 1, ans = 0;
  while (1) {
    ans++;
    if (T[(r + l) / 2] == x)
      return ans;
    else if (r == l)
      break;
    if (T[(r + l) / 2] > x)
      l = (r + l) / 2 - 1;
    else if (T[(r + l) / 2] < x)
      r = (r + l) / 2 + 1;
  }
  return ans;
}
int main() {
  int n, k, num, ans;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> T[i];
    }
    cin >> k;
    ans = find(n, k);
    cout << ans << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int hi, lo, m, n, a, i, k, d[100];
  while (cin >> n && n != 0) {
    for (i = 0; i < n; i++)
      cin >> d[i];
    cin >> a;
    hi = n - 1;
    lo = k = 0;
    while (hi >= lo) {
      k++;
      m = (hi + lo) / 2;
      if (d[m] < a)
        lo = m + 1;
      else if (d[m] > a)
        hi = m - 1;
      else
        break;
      if (hi == lo) {
        k++;
        break;
      }
    }
    cout << k << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int hi, lo, m, n, a, i, k, d[100];
  while (cin >> n && n != 0) {
    for (i = 0; i < n; i++)
      cin >> d[i];
    cin >> a;
    hi = n - 1;
    lo = k = 0;
    while (true) {
      k++;
      m = (hi + lo) / 2;
      if (d[m] < a)
        lo = m + 1;
      else if (d[m] > a)
        hi = m - 1;
      else
        break;
      if (hi == lo) {
        k++;
        break;
      }
    }
    cout << k << endl;
  }
  return 0;
}