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

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

const int maxn = 1005;
int a[maxn];

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  ios::sync_with_stdio(false);
  int n, x;
  while (cin >> n >> x) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      a[i] = abs(a[i] - x);
    }
    int ans = a[0];
    for (int i = 1; i < n; i++) {
      ans = gcd(ans, a[i]);
    }
    cout << ans << endl;
  }
  return 0;
}


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

const int maxn = 100005;
int a[maxn];

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  ios::sync_with_stdio(false);
  int n, x;
  while (cin >> n >> x) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      a[i] = abs(a[i] - x);
    }
    int ans = a[0];
    for (int i = 1; i < n; i++) {
      ans = gcd(ans, a[i]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03262 (C++)

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

#define MAXN 10000
int N, X;
int pos[MAXN + 5];

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  scanf("%d%d", &N, &X);
  for (int i = 1; i <= N; i++)
    scanf("%d", &pos[i]);
  sort(pos + 1, pos + N + 1);
  int i = 1, Ans = 0;
  while (i <= N && pos[i] < X)
    i++;
  for (int j = N; j >= i; j--)
    pos[j + 1] = pos[j];
  pos[i] = X, N++;
  for (int i = 2; i <= N; i++)
    Ans = gcd(Ans, pos[i] - pos[i - 1]);
  printf("%d", Ans);
}


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

#define MAXN 100000
int N, X;
int pos[MAXN + 5];

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  scanf("%d%d", &N, &X);
  for (int i = 1; i <= N; i++)
    scanf("%d", &pos[i]);
  sort(pos + 1, pos + N + 1);
  int i = 1, Ans = 0;
  while (i <= N && pos[i] < X)
    i++;
  for (int j = N; j >= i; j--)
    pos[j + 1] = pos[j];
  pos[i] = X, N++;
  for (int i = 2; i <= N; i++)
    Ans = gcd(Ans, pos[i] - pos[i - 1]);
  printf("%d", Ans);
}
Problem p03262 (C++)

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

long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

int main() {
  int N;
  long long X;
  cin >> N >> X;
  vector<long long> x;
  for (int i = 0; i < N; ++i)
    cin >> x[i], x[i] -= X;
  long long g = abs(x[0]);
  for (int i = 0; i < N; ++i)
    g = GCD(g, abs(x[i]));
  cout << g << endl;
}

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

long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

int main() {
  int N;
  long long X;
  cin >> N >> X;
  vector<long long> x(N);
  for (int i = 0; i < N; ++i)
    cin >> x[i], x[i] -= X;
  long long g = abs(x[0]);
  for (int i = 0; i < N; ++i)
    g = GCD(g, abs(x[i]));
  cout << g << endl;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, x, ans = 0;
long long a[10010];
int main() {
  scanf("%lld %lld", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] = abs(a[i] - x);
  }
  for (int i = 2; i <= n; i++)
    a[i] = __gcd(a[i], a[i - 1]);
  cout << a[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, x, ans = 0;
long long a[100010];
int main() {
  scanf("%lld %lld", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] = abs(a[i] - x);
  }
  for (int i = 2; i <= n; i++)
    a[i] = __gcd(a[i], a[i - 1]);
  cout << a[n] << endl;
  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
vector<long long> pt;
vector<long long> mid;
vector<long long> ans;

int gcd(long long u, long long v) {
  long long r;
  while (0 != v) {
    r = u % v;
    u = v;
    v = r; /* swap */
  }
  return u;
}

int main() {
  // input
  long long n, x;
  cin >> n >> x;

  for (long long i = 0; i < n; ++i) {
    long long tmp = 0;
    cin >> tmp;
    pt.push_back(tmp);
  }

  if (n == 1) {
    sort(pt.begin(), pt.end());
    cout << pt.at(1) - pt.at(0) << endl;
    return 0;
  }

  pt.push_back(x);
  sort(pt.begin(), pt.end());

  // 各差を取得
  for (long long i = 0; i < pt.size() - 1; ++i) {
    long long tmp = 0;
    tmp = pt.at(i + 1) - pt.at(i);
    mid.push_back(tmp);
  }

  // 各差の最大公約数
  for (long long i = 0; i < mid.size() - 1; ++i) {
    long long tmp = 0;
    tmp = gcd(mid.at(i + 1), mid.at(i));
    ans.push_back(tmp);
  }

  sort(ans.begin(), ans.end());

  cout << ans.at(0) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
vector<long long> pt;
vector<long long> mid;
vector<long long> ans;

int gcd(long long u, long long v) {
  long long r;
  while (0 != v) {
    r = u % v;
    u = v;
    v = r; /* swap */
  }
  return u;
}

int main() {
  // input
  long long n, x;
  cin >> n >> x;

  for (long long i = 0; i < n; ++i) {
    long long tmp = 0;
    cin >> tmp;
    pt.push_back(tmp);
  }

  if (n == 1) {
    sort(pt.begin(), pt.end());
    cout << abs(pt[0] - x) << endl;
    return 0;
  }

  pt.push_back(x);
  sort(pt.begin(), pt.end());

  // 各差を取得
  for (long long i = 0; i < pt.size() - 1; ++i) {
    long long tmp = 0;
    tmp = pt.at(i + 1) - pt.at(i);
    mid.push_back(tmp);
  }

  // 各差の最大公約数
  for (long long i = 0; i < mid.size() - 1; ++i) {
    long long tmp = 0;
    tmp = gcd(mid.at(i + 1), mid.at(i));
    ans.push_back(tmp);
  }

  sort(ans.begin(), ans.end());

  cout << ans.at(0) << endl;

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  vector<int> v;
  while (n--) {
    int h;
    cin >> h;
    v.push_back(abs(x - h));
  }
  if (v.size() == 1)
    cout << v[0] << "\n";
  else {
    int ans = __gcd(v[0], v[1]);
    for (int i = 2; i < v.size(); i++)
      ans = __gcd(ans, v[i]);
    cout << ans << "\n";
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n >> x;
  vector<int> v;
  while (n--) {
    int h;
    cin >> h;
    v.push_back(abs(x - h));
  }
  if (v.size() == 1)
    cout << v[0] << "\n";
  else {
    int ans = __gcd(v[0], v[1]);
    for (int i = 2; i < v.size(); i++)
      ans = __gcd(ans, v[i]);
    cout << ans << "\n";
  }

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define MOD 1000000007
#define inf 0x3f3f3f3f3f3f3f3f
#define pi acos(-1.0)
#define ri register int
#define int long long
#ifdef ONLINE_JUDGE
const int maxn = 3e5 + 10;
#else
const int maxn = 1e3 + 10;
#endif
using namespace std;
int N, X, arr[maxn];
signed main() {
  ios::sync_with_stdio(false);
  cin >> N >> X;
  for (ri i = 1; i <= N; ++i) {
    cin >> arr[i];
  }
  arr[++N] = X;
  sort(arr + 1, arr + 1 + N);
  vector<int> v;
  for (ri i = 2; i <= N; ++i) {
    v.push_back(arr[i] - arr[i - 1]);
  }
  int gcd = v[0];
  for (ri i = 1; i < v.size(); ++i) {
    gcd = __gcd(gcd, v[i]);
  }
  cout << gcd << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#define MOD 1000000007
#define inf 0x3f3f3f3f3f3f3f3f
#define pi acos(-1.0)
#define ri register int
#define int long long
#ifdef ONLINE_JUDGE
const int maxn = 3e5 + 10;
#else
const int maxn = 1e5 + 10;
#endif
using namespace std;
int N, X, arr[maxn];
signed main() {
  ios::sync_with_stdio(false);
  cin >> N >> X;
  for (ri i = 1; i <= N; ++i) {
    cin >> arr[i];
  }
  arr[++N] = X;
  sort(arr + 1, arr + 1 + N);
  vector<int> v;
  for (ri i = 2; i <= N; ++i) {
    v.push_back(arr[i] - arr[i - 1]);
  }
  int gcd = v[0];
  for (ri i = 1; i < v.size(); ++i) {
    gcd = __gcd(gcd, v[i]);
  }
  cout << gcd << endl;
  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repdm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

int main() {

  ll N, X;
  cin >> N >> X;

  ll x[N];
  vector<ll> d(N);
  rep(i, N) {
    cin >> x[i];
    d[i] = abs(x[i] - X);
  };
  ll ans = gcd(d[0], d[1]);
  repd(i, 2, N) { ans = gcd(ans, d[i]); }
  cout << ans;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repdm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

int main() {

  ll N, X;
  cin >> N >> X;

  ll x[N];
  vector<ll> d(N);
  rep(i, N) {
    cin >> x[i];
    d[i] = abs(x[i] - X);
  };

  if (d.size() == 1) {
    cout << d[0];
    return 0;
  }

  ll ans = gcd(d[0], d[1]);
  repd(i, 2, N) { ans = gcd(ans, d[i]); }
  cout << ans;

  return 0;
}
Problem p03262 (C++)

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

int N, X;
int x[100010];

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  cin >> N >> X;
  for (int i = 0; i < N; ++i)
    cin >> x[i];
  sort(begin(x), begin(x) + N);
  vector<int> s;
  for (int i = 0; i < N; ++i) {
    int t = 0;
    if (i) {
      s.emplace_back(abs(x[i] - x[i - 1]));
    }
    s.emplace_back(abs(x[i] - X));
  }
  sort(s.begin(), s.end());
  s.erase(unique(s.begin(), s.end()), s.end());
  vector<int> ss(s);
  for (auto a : s) {
    for (auto b : s) {
      if (a != b)
        ss.emplace_back(gcd(a, b));
    }
  }
  sort(ss.begin(), ss.end());
  ss.erase(unique(ss.begin(), ss.end()), ss.end());
  int D = 1;
  for (auto d : ss) {
    int m = X % d;
    bool b = true;
    for (int j = 0; j < N; ++j) {
      if (x[j] % d != m) {
        b = false;
        break;
      }
    }
    if (b) {
      D = max(D, d);
    }
  }
  cout << D << endl;
  return 0;
}

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

int N, X;
int x[100010];

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  cin >> N >> X;
  for (int i = 0; i < N; ++i)
    cin >> x[i];
  int D = abs(x[0] - X);
  for (int i = 1; i < N; ++i) {
    D = gcd(D, abs(x[i] - X));
  }
  cout << D << endl;
  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
const ll mod = 1e9 + 7;
#define rep(i, a, b) for (int i = a; i < b; i++)

int gcd(int, int);

int main() {

  int n, X;
  cin >> n >> X;

  vector<int> x(n);
  rep(i, 0, n) {
    cin >> x[i];
    x[i] = abs(X - x[i]);
  }

  int ans = gcd(x[0], x[1]);

  rep(i, 1, n) { ans = gcd(ans, x[i]); }

  cout << ans << endl;

  return 0;
}

// 最大公約数(ユークリッド互除法)
int gcd(int m, int n) {
  ll temp;
  while (m % n != 0) {
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
const ll mod = 1e9 + 7;
#define rep(i, a, b) for (int i = a; i < b; i++)

int gcd(int, int);

int main() {

  int n, X;
  cin >> n >> X;

  vector<int> x(n);
  rep(i, 0, n) {
    cin >> x[i];
    x[i] = abs(X - x[i]);
  }

  if (n == 1) {
    cout << x[0] << endl;
    return 0;
  }

  int ans = gcd(x[0], x[1]);

  rep(i, 1, n) { ans = gcd(ans, x[i]); }

  cout << ans << endl;

  return 0;
}

// 最大公約数(ユークリッド互除法)
int gcd(int m, int n) {
  ll temp;
  while (m % n != 0) {
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}
Problem p03262 (C++)

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

long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

int main() {
  long long N, X;
  cin >> N >> X;
  vector<long long> vec(N);

  for (long long i = 0; i < N; i++) {
    long long x;
    cin >> x;
    long long dist = x - X;
    if (dist < 0)
      dist = -dist;
    vec.at(i) = dist;
  }

  long long ans = GCD(vec.at(0), vec.at(1));
  for (int i = 0; i < N; i++) {
    ans = GCD(ans, vec.at(i));
  }
  cout << ans << endl;
}


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

long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

int main() {
  long long N, X;
  cin >> N >> X;
  vector<long long> vec(N);

  for (long long i = 0; i < N; i++) {
    long long x;
    cin >> x;
    long long dist = x - X;
    if (dist < 0)
      dist = -dist;
    vec.at(i) = dist;
  }

  long long ans = vec.at(0);
  for (int i = 0; i < N; i++) {
    ans = GCD(ans, vec.at(i));
  }
  cout << ans << endl;
}
Problem p03262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100001;

int smin(int x, int y) {
  if (x == y)
    return x;
  else if (x > y)
    return smin(x - y, y);
  else
    return smin(y - x, x);
}

int main() {
  int n;
  long long i, zmin, st, a[maxn] = {0};
  cin >> n >> st;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= st;
    if (a[i] < 0)
      a[i] = -a[i];
  }
  zmin = a[1];
  for (i = 2; i <= n; i++) {
    zmin = smin(zmin, a[i]);
    if (zmin == 1) {
      cout << '1';
      return 0;
    }
  }
  cout << zmin;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100001;

int smin(int x, int y) {
  if (x == y)
    return x;
  else if (x > y)
    return smin(x - y, y);
  else
    return smin(y - x, x);
}

int main() {
  int n;
  long long i, zmin, st, a[maxn] = {0};
  cin >> n >> st;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= st;
    if (a[i] < 0)
      a[i] = -a[i];
  }
  zmin = a[1];
  for (i = 2; i <= n; i++) {
    if (a[i] % zmin == 0)
      continue;
    zmin = smin(zmin, a[i]);
    if (zmin == 1) {
      cout << '1';
      return 0;
    }
  }
  cout << zmin;
  return 0;
}
Problem p03262 (C++)

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

#include <map>

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll ans, mx, sum, mn = 1e8;
int main() {
  ll n, x;
  cin >> n >> x;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  ans = a[1] - a[0];
  for (ll i = 2; i < n; i++) {
    ans = gcd(ans, a[i] - a[0]);
  }
  if (n == 2) {
    for (ll i = abs(x - a[0]); i >= 1; i--) {
      if ((a[1] - a[0]) % i == 0) {
        cout << i << endl;
        return 0;
      }
    }
  } else if (n == 1)
    cout << abs(x - a[0]);
  else
    cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <math.h>
#include <string>

#include <map>

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll ans, mx, sum, mn = 1e8;
int main() {
  ll n, x;
  cin >> n >> x;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  ans = a[1] - a[0];
  for (ll i = 2; i < n; i++) {
    ans = gcd(ans, a[i] - a[0]);
  }
  if (n == 2) {
    cout << gcd(a[0], a[1]);
  } else if (n == 1)
    cout << abs(x - a[0]);
  else
    cout << ans << endl;
}
Problem p03262 (C++)

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

long long int gcd(long long int m, long long int n) {
  if (0 == m || 0 == n)
    return 0;
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main() {
  int N;
  cin >> N;
  long long int X;
  cin >> X;
  long long int ans, x;
  cin >> ans;
  ans = abs(ans - X);
  for (int i = 1; i < N; i++) {
    cin >> x;
    x = abs(x - X);
    ans = gcd(ans, x);
  }
  cout << ans << endl;
}

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

long long int gcd(long long int m, long long int n) {
  if (n == 0)
    return m;
  else
    return gcd(n, m % n);
}

int main() {
  int N;
  cin >> N;
  long long int X;
  cin >> X;
  long long int ans, x;
  cin >> ans;
  ans = abs(ans - X);
  for (int i = 1; i < N; i++) {
    cin >> x;
    x = abs(x - X);
    ans = gcd(ans, x);
  }
  cout << ans << endl;
}
Problem p03262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a[100020], x, b[100020], sum = 1, lmin = 1000000000, j;
int main() {
  cin >> n >> x;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (a[i] > x)
      b[i] = a[i] - x;
    if (a[i] < x)
      b[i] = x - a[i];
    lmin = min(b[i], lmin);
  }
  sort(b + n, b + 1 + n);
  for (int i = 2; i <= lmin; i++) {
    for (j = 1; j <= n; j++) {
      if (b[j] % i != 0)
        break;
    }
    if (j > n)
      sum = i;
  }
  cout << sum;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[100020], x, b[100020], sum = 1, lmin = 1000000000, j;
int main() {
  cin >> n >> x;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  if (a[1] == 1000000000) {
    cout << 999999999;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] > x)
      b[i] = a[i] - x;
    if (a[i] < x)
      b[i] = x - a[i];
    lmin = min(b[i], lmin);
  }
  sort(b + n, b + 1 + n);
  for (int i = 2; i <= lmin; i++) {
    for (j = 1; j <= n; j++) {
      if (b[j] % i != 0)
        break;
    }
    if (j > n)
      sum = i;
  }
  cout << sum;
  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1.0e9
#define MOD 1.0e9 + 7
using namespace std;
typedef long long ll;

int N;
const int MAX_N = 100;
int x[MAX_N + 1];

int gcd(int a, int b) {
  int large = max(a, b);
  int small = min(a, b);
  if (small == 0)
    return large;
  else
    return gcd(small, large % small);
}

int main() {
  cin >> N;
  for (int i = 0; i < N + 1; i++)
    cin >> x[i];
  sort(x, x + N + 1);
  int diff[N];
  for (int i = 0; i < N; i++)
    diff[i] = x[i + 1] - x[i];
  int ans = diff[0];
  for (int i = 1; i < N; i++) {
    ans = gcd(ans, diff[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1.0e9
#define MOD 1.0e9 + 7
using namespace std;
typedef long long ll;

int N;
const int MAX_N = 100000;
int x[MAX_N + 1];

int gcd(int a, int b) {
  int large = max(a, b);
  int small = min(a, b);
  if (small == 0)
    return large;
  else
    return gcd(small, large % small);
}

int main() {
  cin >> N;
  for (int i = 0; i < N + 1; i++)
    cin >> x[i];
  sort(x, x + N + 1);
  int diff[N];
  for (int i = 0; i < N; i++)
    diff[i] = x[i + 1] - x[i];
  int ans = diff[0];
  for (int i = 1; i < N; i++) {
    ans = gcd(ans, diff[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03262 (C++)

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

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  if (b % a == 0)
    return a;
  int i, n, m;
  if (a < b) {
    n = b;
    m = a;
    i = a;
  } else {
    n = a;
    m = b;
    i = b;
  }
  for (; i > 1; i--) {
    if (n % i == 0 && m % i == 0)
      break;
  }
  return i;
}

int main() {
  int a, b;
  while (1) {
    cin >> a >> b;
    cout << gcd(a, b) << endl;
  }
  int n, X, d;
  cin >> n >> X;
  int *x = new int[n];
  cin >> x[0];
  d = abs(X - x[0]);
  for (int i = 1; i < n; i++) {
    cin >> x[i];
    d = gcd(d, abs(X - x[i]));
  }
  cout << d << endl;
  return 0;
}

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

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  if (b % a == 0)
    return a;
  int i, n, m;
  if (a < b) {
    n = b;
    m = a;
    i = a;
  } else {
    n = a;
    m = b;
    i = b;
  }
  for (; i > 1; i--) {
    if (n % i == 0 && m % i == 0)
      break;
  }
  return i;
}

int main() {
  int n, X, d;
  cin >> n >> X;
  int *x = new int[n];
  cin >> x[0];
  d = abs(X - x[0]);
  for (int i = 1; i < n; i++) {
    cin >> x[i];
    d = gcd(d, abs(X - x[i]));
  }
  cout << d << endl;
  return 0;
}
Problem p03262 (C++)

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

constexpr int ORD(int n) {
  int res = 1;
  for (int i = 0; i < n; i++) {
    res *= n;
  }
  res++;
  return res;
}

int xs[ORD(6)];

using namespace std;

int gcd_many(int *xs, int n) {
  int res = xs[0];
  for (int i = 0; i < n; i++) {
    res = __gcd(res, xs[i]);
  }
  return abs(res);
}

int main() {
  int N, X;
  scanf("%d %d", &N, &X);
  for (int i = 0; i < N; i++) {
    scanf("%d", xs + i);
    xs[i] -= X;
  }

  int res = gcd_many(xs, N);
  printf("%d\n", res);

  return 0;
}


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

constexpr int ORD(int n) {
  int res = 1;
  for (int i = 0; i < n; i++) {
    res *= 10;
  }
  res++;
  return res;
}

int xs[ORD(6)];

using namespace std;

int gcd_many(int *xs, int n) {
  int res = xs[0];
  for (int i = 0; i < n; i++) {
    res = __gcd(res, xs[i]);
  }
  return abs(res);
}

int main() {
  int N, X;
  scanf("%d %d", &N, &X);
  for (int i = 0; i < N; i++) {
    scanf("%d", xs + i);
    xs[i] -= X;
  }

  int res = gcd_many(xs, N);
  printf("%d\n", res);

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int solve(int m, int n) {
  if (m < n) {
    int tmp = m;
    m = n;
    n = tmp;
  }
  if (n == 0)
    return m;

  return solve(n, m - n);
}

int main() {
  int N, X;
  cin >> N >> X;

  int cities[100000];
  for (int i = 0; i < N; i++) {
    cin >> cities[i];
    cities[i] -= X;
    cities[i] = abs(cities[i]);
  }

  int a = cities[0];
  for (int i = 1; i < N; i++) {
    a = solve(a, cities[i]);
  }

  cout << a << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int solve(int m, int n) {
  if (m < n) {
    int tmp = m;
    m = n;
    n = tmp;
  }
  if (n == 0)
    return m;

  return solve(n, m % n);
}

int main() {
  int N, X;
  cin >> N >> X;

  int cities[100000];
  for (int i = 0; i < N; i++) {
    cin >> cities[i];
    cities[i] -= X;
    cities[i] = abs(cities[i]);
  }

  int a = cities[0];
  for (int i = 1; i < N; i++) {
    a = solve(a, cities[i]);
  }

  cout << a << endl;

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
using str = string;

#define rep(a, l, r) for (int a = (l); a < (r); a++)
#define get cin >>
#define put cout <<

int gcd_euclid(int u, int v) {
  int r;
  while (0 != v) {
    r = u % v;
    u = v;
    v = r; /* swap */
  }
  return u;
}

void run() {

  int n;
  get n;
  int x;
  get x;

  int city[x];

  rep(i, 0, n) { get city[i]; }

  vector<int> distance(n);

  rep(i, 0, n) {
    if (city[i] < x) {
      distance[i] = x - city[i];
    } else {
      distance[i] = city[i] - x;
    }
  }

  int gcd = *min_element(distance.begin(), distance.end());
  rep(i, 0, n) { gcd = gcd_euclid(gcd, distance[i]); }

  put gcd;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  run();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
using str = string;

#define rep(a, l, r) for (int a = (l); a < (r); a++)
#define get cin >>
#define put cout <<

int gcd_euclid(int u, int v) {
  int r;
  while (0 != v) {
    r = u % v;
    u = v;
    v = r; /* swap */
  }
  return u;
}

void run() {

  int n;
  get n;
  int x;
  get x;

  vector<int> city(n);

  rep(i, 0, n) { get city[i]; }

  vector<int> distance(n);

  rep(i, 0, n) {
    if (city[i] < x) {
      distance[i] = x - city[i];
    } else {
      distance[i] = city[i] - x;
    }
  }

  int gcd = *min_element(distance.begin(), distance.end());
  rep(i, 0, n) { gcd = gcd_euclid(gcd, distance[i]); }

  put gcd;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  run();
  return 0;
}
Problem p03262 (C++)

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

unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int N, X;
  cin >> N >> X;
  vector<int> city(N);
  for (int i = 0; i < N; ++i) {
    cin >> city.at(i);
  }
  city.push_back(X);
  sort(city.begin(), city.end());
  vector<int> dcity(N);
  for (int i = 0; i < N; ++i) {
    dcity.at(i) = city.at(i + 1) - city.at(i);
  }
  int GCD = gcd(dcity.at(0), dcity.at(1));
  for (int i = 0; i < N; ++i) {
    if (dcity.at(i))
      GCD = gcd(GCD, dcity.at(i));
  }
  cout << GCD << endl;
}

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

unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int N, X;
  cin >> N >> X;
  vector<int> city(N);
  for (int i = 0; i < N; ++i) {
    cin >> city.at(i);
  }
  city.push_back(X);
  sort(city.begin(), city.end());
  vector<int> dcity(N);
  for (int i = 0; i < N; ++i) {
    dcity.at(i) = city.at(i + 1) - city.at(i);
  }
  int GCD;
  if (N > 1) {
    GCD = gcd(dcity.at(0), dcity.at(1));
  } else {
    GCD = dcity.at(0);
  }
  for (int i = 0; i < N; ++i) {
    if (dcity.at(i))
      GCD = gcd(GCD, dcity.at(i));
  }
  cout << GCD << endl;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {

  long long n, x;
  long long a[10005] = {};
  long long b[10005] = {};
  cin >> n >> x;

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

  for (int i = 1; i <= n; i++) {
    b[i - 1] = a[i] - a[i - 1];
  }

  long long r;
  long long ans = 1000000005;
  for (int i = 1; i <= n; i++) {
    r = b[i] % b[i - 1];
    while (r != 0) {
      b[i] = b[i - 1];
      b[i - 1] = r;
      r = b[i] % b[i - 1];
    }
    if (ans > b[i - 1])
      ans = b[i - 1];
  }

  cout << ans;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {

  long long n, x;
  long long a[100050] = {};
  long long b[100050] = {};
  cin >> n >> x;

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

  for (int i = 1; i <= n; i++) {
    b[i - 1] = a[i] - a[i - 1];
  }

  long long r;
  long long ans = 1000000005;
  for (int i = 1; i <= n; i++) {
    r = b[i] % b[i - 1];
    while (r != 0) {
      b[i] = b[i - 1];
      b[i - 1] = r;
      r = b[i] % b[i - 1];
    }
    if (ans > b[i - 1])
      ans = b[i - 1];
  }

  cout << ans;
}
Problem p03262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
const ll mod = ll(1e9) + 7;
const int INF = int(1e9);
int gcd(int m, int n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}
int main() {
  int N, X;
  cin >> N >> X;
  vector<int> x(N);
  rep(i, N) cin >> x[i];
  rep(i, N) x[i] = abs(X - x[i]);
  int ans = x[0];
  rep(i, N) { ans = gcd(ans, x[i]); }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
const ll mod = ll(1e9) + 7;
const int INF = int(1e9);
int gcd(int m, int n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}
int main() {
  int N, X;
  cin >> N >> X;
  vector<int> x(N);
  rep(i, N) cin >> x[i];
  rep(i, N) x[i] = abs(X - x[i]);
  int ans = x[0];
  rep(i, N) {
    if (x[i] % ans == 0)
      continue;
    ans = gcd(ans, x[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03262 (C++)

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

int gcd(int P, int Q);

int main() {
  int N;
  cin >> N;
  N++;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  sort(A.begin(), A.end());

  int D = gcd((A.at(1) - A.at(0)), (A.at(2) - A.at(1)));
  int C;

  for (int i = 3; i < N; i++) {
    C = gcd((A.at(i - 1) - A.at(i - 2)), (A.at(i) - A.at(i - 1)));
    if (C < D) {
      D = C;
    }
  }

  cout << D << endl;
}

int gcd(int P, int Q) {
  int r = P % Q;
  if (r == 0) {
    return Q;
  }

  int rr = Q % r;
  if (rr == 0) {
    return r;
  }

  while ((r != 0) || (rr != 0)) {
    r = r % rr;
    if (r == 0) {
      return rr;
    }

    rr = rr % r;
    if (rr == 0) {
      return r;
    }
  }
}


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

int gcd(int P, int Q);

int main() {
  int N;
  cin >> N;
  N++;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  sort(A.begin(), A.end());

  if (N == 2) {
    cout << A.at(1) - A.at(0) << endl;
    exit(0);
  }

  int D = gcd((A.at(1) - A.at(0)), (A.at(2) - A.at(1)));
  int C;

  for (int i = 3; i < N; i++) {
    C = gcd((A.at(i - 1) - A.at(i - 2)), (A.at(i) - A.at(i - 1)));
    if (C < D) {
      D = C;
    }
  }

  cout << D << endl;
}

int gcd(int P, int Q) {
  int r = P % Q;
  if (r == 0) {
    return Q;
  }

  int rr = Q % r;
  if (rr == 0) {
    return r;
  }

  while ((r != 0) || (rr != 0)) {
    r = r % rr;
    if (r == 0) {
      return rr;
    }

    rr = rr % r;
    if (rr == 0) {
      return r;
    }
  }
}
Problem p03262 (C++)

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

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;

ll Gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);

  if (a % b == 0)
    return (b);

  else
    return (Gcd(b, a % b));
}

int main() {
  ll n, x;
  cin >> n >> x;
  ll dist[x];
  rep(i, n) {
    ll x_i;
    cin >> x_i;
    dist[i] = abs(x - x_i);
  }

  ll ans = dist[0];
  for (ll i = 1; i < n; ++i) {
    ans = Gcd(ans, dist[i]);
  }

  cout << ans << endl;
}

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

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;

ll Gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);

  if (a % b == 0)
    return (b);

  else
    return (Gcd(b, a % b));
}

int main() {
  ll n, x;
  cin >> n >> x;
  ll dist[n];
  rep(i, n) {
    ll x_i;
    cin >> x_i;
    dist[i] = abs(x - x_i);
  }

  ll ans = dist[0];
  for (ll i = 1; i < n; ++i) {
    ans = Gcd(ans, dist[i]);
  }

  cout << ans << endl;
}
Problem p03262 (C++)

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

unsigned GCD(unsigned u, unsigned v) {
  while (v != 0) {
    unsigned r = u % v;
    u = v;
    v = r;
  }
  return u;
}

int main() {
  int N, X, maxD = 0;
  std::cin >> N >> X;
  int a[N];

  for (int i = 0; i < N; i++)
    std::cin >> a[i];

  for (int i = 0; i < N; i++)
    maxD = std::max(maxD, a[i]);

  int minD = std::abs(X - maxD);

  for (int i = 0; i < N - 1; i++)
    for (int j = i + 1; j < N; j++)
      minD = GCD(GCD(std::abs(X - a[i]), std::abs(X - a[j])), minD);

  std::cout << minD << std::endl;

  return 0;
}

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

unsigned GCD(unsigned u, unsigned v) {
  while (v != 0) {
    unsigned r = u % v;
    u = v;
    v = r;
  }
  return u;
}

int main() {
  int N, X, maxD = 0;
  std::cin >> N >> X;
  int a[N];

  for (int i = 0; i < N; i++)
    std::cin >> a[i];

  for (int i = 0; i < N; i++)
    maxD = std::max(maxD, a[i]);

  int minD = std::abs(X - maxD);

  for (int i = 0; i < N; i++)
    minD = std::min(minD, std::abs(X - a[i]));

  for (int i = 0; i < N; i++)
    minD = GCD(std::abs(X - a[i]), minD);

  std::cout << minD << std::endl;

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
int yakusuu(int a, int b) {
  if (b % a == 0)
    return a;
  else
    return yakusuu(b % a, a);
}
int main() {
  int N, X, x[100000], baisuu, k, b = 0, a = 0;
  cin >> N >> X;
  cin >> x[0];
  x[0] -= X;
  if (x[0] < 0)
    x[0] *= -1;
  // baisuu=x[0];
  for (int i = 1; i < N; i++) {
    cin >> x[i];
    x[i] -= X;
    if (x[i] < 0)
      x[i] *= -1;
    // if(baisuu>x[i])baisuu=x[i];
  }
  baisuu = yakusuu(x[0], x[1]);
  for (int i = 2; i < N, baisuu != 1; i++) {
    b = yakusuu(x[i - 1], x[i]);
    baisuu = yakusuu(b, baisuu);
  }
  /*for(int i=0;i<N;i++){
      x[i]%=baisuu;
      if(x[i]==0)a++;
      if(a==N){
          cout<<baisuu<<endl;
          return 0;
      }
  }
  for(int i=0;i<N,baisuu!=1;i++){
      if(x[i]==0);
      else if(yakusuu(baisuu,x[i])==1){
          b=1;
          cout<<b<<endl;
          return 0;
      }
      else {
          baisuu=yakusuu(baisuu,x[i]);
          for(int i=0;i<N;i++){
              x[i]%=baisuu;
          }
      }
  }*/
  cout << baisuu << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int yakusuu(int a, int b) {
  if (b % a == 0)
    return a;
  else
    return yakusuu(b % a, a);
}
int main() {
  int N, X, x[100000], baisuu, k, b = 0, a = 0;
  cin >> N >> X;
  cin >> x[0];
  x[0] -= X;
  if (x[0] < 0)
    x[0] *= -1;
  // baisuu=x[0];
  for (int i = 1; i < N; i++) {
    cin >> x[i];
    x[i] -= X;
    if (x[i] < 0)
      x[i] *= -1;
    // if(baisuu>x[i])baisuu=x[i];
  }
  baisuu = yakusuu(x[0], x[1]);
  for (int i = 2; i < N && baisuu != 1; i++) {
    b = yakusuu(x[i - 1], x[i]);
    baisuu = yakusuu(b, baisuu);
  }
  /*for(int i=0;i<N;i++){
      x[i]%=baisuu;
      if(x[i]==0)a++;
      if(a==N){
          cout<<baisuu<<endl;
          return 0;
      }
  }
  for(int i=0;i<N,baisuu!=1;i++){
      if(x[i]==0);
      else if(yakusuu(baisuu,x[i])==1){
          b=1;
          cout<<b<<endl;
          return 0;
      }
      else {
          baisuu=yakusuu(baisuu,x[i]);
          for(int i=0;i<N;i++){
              x[i]%=baisuu;
          }
      }
  }*/
  cout << baisuu << endl;
  return 0;
}
Problem p03262 (C++)

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

#define N 10000

int n, X;
int x[N];

long long gcd(int u, int v) {
  int r;
  while (0 != v) {
    r = u % v;
    u = v;
    v = r;
  }
  return u;
}

int main() {
  int n;

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

  long long ans = abs(x[0] - X);
  if (n > 1) {
    for (int i = 1; i < n; i++) {
      ans = gcd(ans, abs(x[i] - X));
    }
  }

  cout << ans << endl;

  return 0;
}


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

#define N 100000

int n, X;
int x[N];

long long gcd(int u, int v) {
  int r;
  while (0 != v) {
    r = u % v;
    u = v;
    v = r;
  }
  return u;
}

int main() {
  int n;

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

  long long ans = abs(x[0] - X);
  if (n > 1) {
    for (int i = 1; i < n; i++) {
      ans = gcd(ans, abs(x[i] - X));
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03262 (Python)

Buggy submission (status: Runtime Error):
import math

N, X = map(int, input().split())
X_lst = list(map(int, input().split()))

if len(X_lst) == 1:
    print(abs(X_lst[0] - X))
    exit()

u = 0
if X_lst[0] == X:
    u = float("inf")

for i, x in enumerate(X_lst[1:], start=1):
    if x == X:
        u = float("inf")

    if X_lst[i - 1] < X < x:
        u = [X - u, x - X]

    if i == 1:
        g = X_lst[1] - X_lst[0]
    else:
        g = math.gcd(g, x - X_lst[i - 1])


if u == 0:
    u = [X - X_lst[-1]]
elif u != float("inf"):
    g = max(math.gcd(g, u[0]), math.gcd(g, u[1]))

print(g)


Fixed submission:
import math

N, X = map(int, input().split())
X_lst = list(map(int, input().split()))

if len(X_lst) == 1:
    print(abs(X_lst[0] - X))
    exit()

u = 0
if X_lst[0] == X:
    u = float("inf")

for i, x in enumerate(X_lst[1:], start=1):
    if x == X:
        u = float("inf")

    if X_lst[i - 1] < X < x:
        u = [X - X_lst[-1], x - X]

    if i == 1:
        g = X_lst[1] - X_lst[0]
    else:
        g = math.gcd(g, x - X_lst[i - 1])


if u == 0:
    u = [X - X_lst[-1]]
elif u != float("inf"):
    g = max(math.gcd(g, u[0]), math.gcd(g, u[1]))

print(g)
Problem p03262 (Python)

Buggy submission (status: Runtime Error):
from math import gcd
from functools import reduce


def gcd_list(numbers):
    return reduce(gcd, numbers)


n, x = map(int, input().split())

x_arr = [0] * n

x_arr = list(map(int, input().split()))

for i in range(len(x_arr)):
    x_arr[i] = abs(x_arr[i] - x)
print(gcd_list(x_arr))


Fixed submission:
from fractions import gcd
from functools import reduce


def gcd_list(numbers):
    return reduce(gcd, numbers)


n, x = map(int, input().split())

x_arr = [0] * n

x_arr = list(map(int, input().split()))

for i in range(len(x_arr)):
    x_arr[i] = abs(x_arr[i] - x)
print(gcd_list(x_arr))
Problem p03262 (Python)

Buggy submission (status: Runtime Error):
from fractions import gcd

N, X = map(int, input().split())
X_list = list(map(int, input().split()))

dX = [abs(X - x) for x in X_list]
if len(dX) == 1:
    print(dX[0])
    exit(0)

D = [abs(X_list[i] - X_list[i + 1]) for i in range(0, N - 1)]
res = gcd(D[0], D[1])
for x in D[2:]:
    res = gcd(res, x)

ans = 0
for d in dX:
    ans = max(gcd(res, d), ans)
print(ans)


Fixed submission:
from fractions import gcd

N, X = map(int, input().split())
X_list = list(map(int, input().split()))

dX = [abs(X - x) for x in X_list]
if len(dX) == 1:
    print(dX[0])
    exit(0)

D = [abs(X_list[i] - X_list[i + 1]) for i in range(0, N - 1)]
res = D[0]
for x in D[1:]:
    res = gcd(res, x)

ans = 0
for d in dX:
    ans = max(gcd(res, d), ans)
print(ans)
Problem p03262 (Python)

Buggy submission (status: Runtime Error):
n, s = map(int, input().split())
xxx = [abs(s - y) for y in map(int, input().split())]
d = 0
while True:
    d = min(xxx)
    xxx = [x % d for x in xxx]
    if all(x == 0 for x in xxx):
        break
print(d)


Fixed submission:
n, s = map(int, input().split())
xxx = [abs(s - y) for y in map(int, input().split())]
d = 0
while True:
    xxx = [x for x in xxx if x > 0]
    d = min(xxx)
    xxx = [x % d for x in xxx]
    if all(x == 0 for x in xxx):
        break
print(d)
Problem p03262 (Python)

Buggy submission (status: Runtime Error):
def gcd(a, b):
    if a > b:
        a, b = b, a
    while b > 0:
        a, b = b, a % b
    return a


def allgcd(d):
    if len(d) == 1:
        return d
    ret_gcd = d[0]
    for i in d:
        ret_gcd = gcd(ret_gcd, i)
    return ret_gcd


def maxNumVisitCity(N, X, x):
    if len(x) == 1:
        return abs(x[0] - X)

    d = []
    for i in x:
        d.append(abs(i - X))
    return allgcd(d)


def main():
    N, X = map(int, input().split())
    x = map(int, input().split())
    print(maxNumVisitCity(N, X, x))


if __name__ == "__main__":
    main()


Fixed submission:
def gcd(a, b):
    if a > b:
        a, b = b, a
    while b > 0:
        a, b = b, a % b
    return a


def allgcd(d):
    if len(d) == 1:
        return d
    ret_gcd = d[0]
    for i in d:
        ret_gcd = gcd(ret_gcd, i)
    return ret_gcd


def maxNumVisitCity(N, X, x):
    if len(x) == 1:
        return abs(x[0] - X)

    d = []
    for i in x:
        d.append(abs(i - X))
    return allgcd(d)


def main():
    N, X = map(int, input().split())
    x = list(map(int, input().split()))
    print(maxNumVisitCity(N, X, x))


if __name__ == "__main__":
    main()
Problem p03262 (Python)

Buggy submission (status: Time Limit Exceeded):
n, x = map(int, input().split())
li = list(map(int, input().split()))
t = []
for i in li:
    t.append(abs(i - x))
for j in range(min(t)):
    if all(k % (min(t) - j) == 0 for k in t):
        print(min(t) - j)
        exit()


Fixed submission:
n, x = map(int, input().split())
li = list(map(int, input().split()))
t = []
for i in li:
    t.append(abs(i - x))
divisors = []
for i in range(1, int(min(t) ** 0.5) + 1):
    if min(t) % i == 0:
        divisors.append(i)
        if i != min(t) // i:
            divisors.append(min(t) // i)
divisors.sort()
divisors = divisors[::-1]
for j in divisors:
    if all(k % j == 0 for k in t):
        print(j)
        exit()
Problem p03262 (Python)

Buggy submission (status: Runtime Error):
from functools import reduce
from math import gcd

X = int(input().split()[1])
x = [abs(int(x) - X) for x in input().split()]

print(reduce(gcd, x))


Fixed submission:
from functools import reduce
from fractions import gcd

X = int(input().split()[1])
x = [abs(int(x) - X) for x in input().split()]

print(reduce(gcd, x))
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, a[100007];
int gcd(int x, int y) {
  if (x == 0)
    return y;
  else
    return gcd(y, x % y);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> a[0];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a, a + n + 1);
  int g = 0;
  for (int i = 1; i <= n; i++) {
    g = gcd(g, a[i] - a[i - 1]);
  }
  cout << g << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[100007];
int gcd(int x, int y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> a[0];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a, a + n + 1);
  int g = 0;
  for (int i = 1; i <= n; i++) {
    g = gcd(g, a[i] - a[i - 1]);
  }
  cout << g << endl;
  return 0;
}
Problem p03262 (C++)

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

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int n, ans, x[n + 1];
  cin >> n;
  REP(i, n + 1) cin >> x[i];
  sort(x, x + n + 1);
  ans = x[1] - x[0];
  REP(i, n) ans = gcd(ans, x[i + 1] - x[i]);
  cout << ans;
}

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

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int n, ans, x[100001];
  cin >> n;
  REP(i, n + 1) cin >> x[i];
  sort(x, x + n + 1);
  ans = x[1] - x[0];
  REP(i, n) ans = gcd(ans, x[i + 1] - x[i]);
  cout << ans;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e10;
template <typename T> void cmax(T &a, T b) { a = max(a, b); }
template <typename T> void cmin(T &a, T b) { a = min(a, b); }
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

signed main() {
  int n, x;
  cin >> n >> x;

  vector<int> a(n);
  vector<int> dif(n - 1);
  rep(i, n) cin >> a[i];
  if (n == 1) {
    cout << abs(x - a[0]) << endl;
    return 0;
  }
  rep(i, n) dif[i] = abs(a[i] - x);

  int ans = dif[0];
  rep(i, n) ans = gcd(ans, dif[i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e10;
template <typename T> void cmax(T &a, T b) { a = max(a, b); }
template <typename T> void cmin(T &a, T b) { a = min(a, b); }
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

signed main() {
  int n, x;
  cin >> n >> x;

  vector<int> a(n);
  vector<int> dif(n);
  rep(i, n) cin >> a[i];
  if (n == 1) {
    cout << abs(x - a[0]) << endl;
    return 0;
  }
  rep(i, n) dif[i] = abs(a[i] - x);

  int ans = dif[0];
  rep(i, n) ans = gcd(ans, dif[i]);
  cout << ans << endl;
  return 0;
}
Problem p03262 (C++)

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

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> y(n);
  y[0] = x;
  for (int i = 1; i <= n; i++) {
    cin >> y[i];
  }

  sort(y.begin(), y.end());
  vector<int> dif(n);
  for (int i = 0; i < n; i++) {
    dif[i] = y[i + 1] - y[i];
  }

  int ans = dif[0];
  for (int i = 1; i < n; i++) {
    ans = gcd(ans, dif[i]);
  }
  cout << ans << endl;
}

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

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> y(n + 1);
  y[0] = x;
  for (int i = 1; i <= n; i++) {
    cin >> y[i];
  }

  sort(y.begin(), y.end());
  vector<int> dif(n);
  for (int i = 0; i < n; i++) {
    dif[i] = y[i + 1] - y[i];
  }

  int ans = dif[0];
  for (int i = 1; i < n; i++) {
    ans = gcd(ans, dif[i]);
  }
  cout << ans << endl;
}