Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;
#define int long long
#define rep(s, i, n) for (int i = s; i < n; i++)
#define c(n) cout << n << endl;
#define ic(n)                                                                  \
  int n;                                                                       \
  cin >> n;
#define sc(s)                                                                  \
  string s;                                                                    \
  cin >> s;
#define mod 1000000007
#define inf 1000000000000000007
#define f first
#define s second
#define mini(c, a, b) *min_element(c + a, c + b)
#define maxi(c, a, b) *max_element(c + a, c + b)
int keta(int x) {
  rep(0, i, 30) {
    if (x < 10) {
      return i + 1;
    }
    x = x / 10;
  }
}
int gcd(int x, int y) {
  int aa = x, bb = y;
  rep(0, i, 1000) {
    aa = aa % bb;
    if (aa == 0) {
      return bb;
    }
    bb = bb % aa;
    if (bb == 0) {
      return aa;
    }
  }
}
int lcm(int x, int y) {
  int aa = x, bb = y;
  rep(0, i, 1000) {
    aa = aa % bb;
    if (aa == 0) {
      return x / bb * y;
    }
    bb = bb % aa;
    if (bb == 0) {
      return x / aa * y;
    }
  }
}
char mem[214514];
char t[214514];
char c[214514];
string s[214514];
int a[214514], b[214514];
signed main() {
  ic(n) ic(x) rep(0, i, n) {
    cin >> a[i];
    a[i] -= x;
  }
  if (n == 1) {
    c(a[0]) return 0;
  }
  sort(a, a + n);
  rep(0, i, n - 1) b[i] = a[i + 1] - a[i];
  int ans = gcd(b[0], b[1]);
  rep(1, i, n) { ans = gcd(b[i], ans); }
  c(ans)
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;
#define int long long
#define rep(s, i, n) for (int i = s; i < n; i++)
#define c(n) cout << n << endl;
#define ic(n)                                                                  \
  int n;                                                                       \
  cin >> n;
#define sc(s)                                                                  \
  string s;                                                                    \
  cin >> s;
#define mod 1000000007
#define inf 1000000000000000007
#define f first
#define s second
#define mini(c, a, b) *min_element(c + a, c + b)
#define maxi(c, a, b) *max_element(c + a, c + b)
int keta(int x) {
  rep(0, i, 30) {
    if (x < 10) {
      return i + 1;
    }
    x = x / 10;
  }
}
int gcd(int x, int y) {
  int aa = x, bb = y;
  rep(0, i, 1000) {
    aa = aa % bb;
    if (aa == 0) {
      return bb;
    }
    bb = bb % aa;
    if (bb == 0) {
      return aa;
    }
  }
}
int lcm(int x, int y) {
  int aa = x, bb = y;
  rep(0, i, 1000) {
    aa = aa % bb;
    if (aa == 0) {
      return x / bb * y;
    }
    bb = bb % aa;
    if (bb == 0) {
      return x / aa * y;
    }
  }
}
char mem[214514];
char t[214514];
char c[214514];
string s[214514];
int a[214514], b[214514];
signed main() {
  ic(n) ic(x) rep(0, i, n) {
    cin >> a[i];
    a[i] -= x;
  }
  if (n == 1) {
    c(abs(a[0])) return 0;
  }
  if (n == 2) {
    c(gcd(a[0], a[1]));
    return 0;
  }
  sort(a, a + n);
  rep(0, i, n - 1) b[i] = a[i + 1] - a[i];
  int ans = gcd(b[0], b[1]);
  rep(1, i, n) { ans = gcd(b[i], ans); }
  c(ans)
}