Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// ===============================================================

using namespace std;
using ll = long long;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;

// ===============================================================

ll gcd(ll a, ll b) // 最大公約数
{
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) // 最小公倍数
{
  return a * b / gcd(a, b);
}

ll box(double a) // doubleの切り捨て
{
  ll b = a;
  return b;
}

ll fff(double a) // doubleの四捨五入
{
  ll b = a + 0.5;
  return b;
}

ll sum(ll n) { // 整数sまでの合計
  if (n == 0) {
    return 0;
  }

  int s = sum(n - 1);
  return s + n;
}

bool prime(ll num) // 素数判定、primeならtrue,違うならfalse
{
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  // 素数である
  return true;
}

// ===============================================================

int main() {
  ll n, x;
  cin >> n >> x;
  vl a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1) {
    cout << abs(a[0] - x) << endl;
  } else {
    sort(a.begin(), a.end());
    ll count;
    vl b(n);
    ll junbi = 0;
    for (int i = 0; i < n - 1; i++) {
      if (a[i] < x && a[i + 1] > x) {
        b[i] = x - a[i];
        b[i + 1] = a[i + 1] - x;
        junbi++;
      }
      b[i + junbi] = a[i + 1] - a[i];
    }
    count = b[0];
    for (int i = 0; i < n; i++) {
      count = gcd(count, b[i]);
    }
    cout << count << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// ===============================================================

using namespace std;
using ll = long long;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;

// ===============================================================

ll gcd(ll a, ll b) // 最大公約数
{
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) // 最小公倍数
{
  return a * b / gcd(a, b);
}

ll box(double a) // doubleの切り捨て
{
  ll b = a;
  return b;
}

ll fff(double a) // doubleの四捨五入
{
  ll b = a + 0.5;
  return b;
}

ll sum(ll n) { // 整数sまでの合計
  if (n == 0) {
    return 0;
  }

  int s = sum(n - 1);
  return s + n;
}

bool prime(ll num) // 素数判定、primeならtrue,違うならfalse
{
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  // 素数である
  return true;
}

// ===============================================================

int main() {
  ll n, x;
  cin >> n >> x;
  vl a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1) {
    cout << abs(a[0] - x) << endl;
  } else {
    sort(a.begin(), a.end());
    ll count;
    vl b(n);
    ll junbi = 0;
    for (int i = 0; i < n - 1; i++) {
      if (x < a[n - 1] && x > a[0]) {
        if (a[i] < x && a[i + 1] > x) {
          b[i] = x - a[i];
          b[i + 1] = a[i + 1] - x;
          junbi++;
        }
      } else {
        if (x < a[n - 1]) {
          b[0] = a[0] - x;
          junbi++;
        } else {
          b[n - 1] = x - a[n - 1];
        }
      }
      b[i + junbi] = a[i + 1] - a[i];
    }
    count = b[0];
    for (int i = 0; i < n; i++) {
      count = gcd(count, b[i]);
    }
    cout << count << endl;
  }
}