Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  int i, j, k;
  long n, x;
  vector<long> num;
  vector<long> sasa;

  cin >> n >> x;

  for (i = 0; i < n; i++) {
    long nn;
    cin >> nn;

    num.push_back(nn);
  }

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

  long minn = 99999999999;

  for (i = 0; i < n - 1; i++) {
    long sa = num[i + 1] - num[i];
    auto itr = find(sasa.begin(), sasa.end(), sa);
    if (itr == sasa.end()) {
      sasa.push_back(sa);
    }
    minn = min(sa, minn);
  }

  sort(sasa.begin(), sasa.end());
  long minn2 = 9999999999;

  long saa1 = -1, saa2 = -1;

  if (x < num[0]) {
    saa1 = num[0] - x;
  } else if (x > num[n - 1]) {
    saa2 = x - num[n - 1];
  } else {

    for (i = 0; i < n; i++) {
      if (num[i] > x) {
        saa1 = x - num[i - 1];
        saa2 = num[i] - x;
        break;
      }
    }
  }

  for (i = 0; i < n; i++) {
    // long sa = abs(num[i] - x);
    long sa;

    if (num[i] >= x) {
      sa = num[i] - x;
    } else {
      sa = x - num[i];
    }

    if (sa == 0)
      continue;

    minn2 = min(sa, minn2);
  }

  minn2 = min(minn, minn2);
  bool flag2 = false;

  while (true) {

    flag2 = false;

    for (i = 0; i < n - 1; i++) {
      if (sasa[i] % minn2 != 0) {
        minn2--;
        flag2 = true;
        break;
      }
    }

    if (flag2 == false)
      break;
  }

  if (saa1 != -1 && saa2 == -1) {
    while (true) {
      bool flag3 = false;

      for (i = 0; i < n - 1; i++) {
        if (saa1 % minn2 != 0) {
          minn2--;
          flag3 = true;
          break;
        }
      }

      if (flag3 == false)
        break;
    }
  } else if (saa1 == -1 && saa2 != -1) {
    while (true) {
      bool flag3 = false;

      for (i = 0; i < n - 1; i++) {
        if (saa2 % minn2 != 0) {
          minn2--;
          flag3 = true;
          break;
        }
      }

      if (flag3 == false)
        break;
    }
  } else if (saa1 != -1 && saa2 != -1) {
    while (true) {
      bool flag3 = false;

      for (i = 0; i < n - 1; i++) {
        if (saa1 % minn2 != 0 || saa2 % minn2 != 0) {
          minn2--;
          flag3 = true;
          break;
        }
      }

      if (flag3 == false)
        break;
    }
  }

  cout << minn2 << endl;

  getchar();
  getchar();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  int i, j, k;
  long n, x;
  vector<long> num;
  vector<long> sasa;

  cin >> n >> x;

  for (i = 0; i < n; i++) {
    long nn;
    cin >> nn;

    num.push_back(nn);
  }

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

  long minn = 99999999999;

  for (i = 0; i < n - 1; i++) {
    long sa = num[i + 1] - num[i];
    auto itr = find(sasa.begin(), sasa.end(), sa);
    if (itr == sasa.end()) {
      sasa.push_back(sa);
    }
    minn = min(sa, minn);
  }

  sort(sasa.begin(), sasa.end());
  long minn2 = 9999999999;

  long saa1 = -1, saa2 = -1;

  if (x < num[0]) {
    saa1 = num[0] - x;
  } else if (x > num[n - 1]) {
    saa2 = x - num[n - 1];
  } else {

    for (i = 0; i < n; i++) {
      if (num[i] > x) {
        saa1 = x - num[i - 1];
        saa2 = num[i] - x;
        break;
      }
    }
  }

  for (i = 0; i < n; i++) {
    // long sa = abs(num[i] - x);
    long sa;

    if (num[i] >= x) {
      sa = num[i] - x;
    } else {
      sa = x - num[i];
    }

    if (sa == 0)
      continue;

    minn2 = min(sa, minn2);
  }

  minn2 = min(minn, minn2);
  bool flag2 = false;

  while (true) {

    flag2 = false;

    for (i = 0; i < sasa.size(); i++) {
      if (sasa[i] % minn2 != 0) {
        minn2--;
        flag2 = true;
        break;
      }
    }

    if (flag2 == false)
      break;
  }

  if (saa1 != -1 && saa2 == -1) {
    while (true) {
      bool flag3 = false;

      for (i = 0; i < n - 1; i++) {
        if (saa1 % minn2 != 0) {
          minn2--;
          flag3 = true;
          break;
        }
      }

      if (flag3 == false)
        break;
    }
  } else if (saa1 == -1 && saa2 != -1) {
    while (true) {
      bool flag3 = false;

      for (i = 0; i < n - 1; i++) {
        if (saa2 % minn2 != 0) {
          minn2--;
          flag3 = true;
          break;
        }
      }

      if (flag3 == false)
        break;
    }
  } else if (saa1 != -1 && saa2 != -1) {
    while (true) {
      bool flag3 = false;

      for (i = 0; i < n - 1; i++) {
        if (saa1 % minn2 != 0 || saa2 % minn2 != 0) {
          minn2--;
          flag3 = true;
          break;
        }
      }

      if (flag3 == false)
        break;
    }
  }

  cout << minn2 << endl;

  getchar();
  getchar();
  return 0;
}