Problem p02964 (C++)

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

using namespace std;
inline void prep() {
  cin.tie(0);
  cin.sync_with_stdio(0);
};
const double pi = acos(-1.);
// printf(%.12lf\n,mid);
//  __builtin_popcount(int) count 1's in binary
//  get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;

long long mymod(long long num) {
  if (num < 0 && !(num % mod == 0)) {
    if (mod == 1) {
      return 0;
    }
    return mod - abs(num) % mod;
  } else {
    return num % mod;
  }
}

/*string sentence = And I feel fine...;
    istringstream iss(sentence);
*/

double eps = .000000001;
set<int> nums[200001];
int main() {
  prep();
  int n, k;
  cin >> n >> k;
  int arr[n];

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    nums[arr[i]].insert(i);
  }
  if (n == 1) {
    if (k % 2 == 1) {
      cout << arr[0];
    }
    return 0;
  }
  int graph[200001];
  int graph2[200001];
  memset(graph, -1, sizeof(int) * 200001);
  memset(graph2, -1, sizeof(int) * 200001);
  for (int i = 0; i < n; i++) {
    set<int> mine = nums[arr[i]];
    set<int>::iterator it = mine.upper_bound(i);
    if (it == mine.end()) {
      graph[i] = *(mine.begin());
    } else {
      graph[i] = *it;
      graph2[i] = *it;
    }
  }

  int cur = (graph[0] + 1) % n;
  int times = 1 + (cur == 1);
  while (cur != 0) {
    int before = cur;
    if (graph[cur] <= before) {
      // cout << graph[cur] << " " << before << endl;
      times++;
    }
    cur = (graph[cur] + 1) % n;
  }
  // cout << times << endl;
  if (k % times == 0) {
    return 0;
  }
  int other = k % times;
  cur = 0;
  times = 0;
  while (times < other - 1) {
    int before = cur;
    if (graph[cur] <= before) {
      times++;
    }
    cur = (graph[cur] + 1) % n;
  }
  vector<int> res;
  while (cur < n) {
    int next = graph2[cur];
    if (next == -1) {
      res.push_back(arr[cur]);
      cur++;
    } else {
      cur = graph2[cur] + 1;
    }
  }
  for (int i = 0; i < res.size(); i++) {
    cout << res[i] << " ";
  }
  return 0;
}

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

using namespace std;
inline void prep() {
  cin.tie(0);
  cin.sync_with_stdio(0);
};
const double pi = acos(-1.);
// printf(%.12lf\n,mid);
//  __builtin_popcount(int) count 1's in binary
//  get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;

long long mymod(long long num) {
  if (num < 0 && !(num % mod == 0)) {
    if (mod == 1) {
      return 0;
    }
    return mod - abs(num) % mod;
  } else {
    return num % mod;
  }
}

/*string sentence = And I feel fine...;
    istringstream iss(sentence);
*/

double eps = .000000001;
set<int> nums[200001];
int main() {
  prep();
  long long n, k;
  cin >> n >> k;
  int arr[n];

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    nums[arr[i]].insert(i);
  }
  if (n == 1) {
    if (k % 2 == 1) {
      cout << arr[0];
    }
    return 0;
  }
  int graph[200001];
  int graph2[200001];
  memset(graph, -1, sizeof(int) * 200001);
  memset(graph2, -1, sizeof(int) * 200001);
  for (int i = 0; i < n; i++) {
    set<int> mine = nums[arr[i]];
    set<int>::iterator it = mine.upper_bound(i);
    if (it == mine.end()) {
      graph[i] = *(mine.begin());
    } else {
      graph[i] = *it;
      graph2[i] = *it;
    }
  }

  int cur = (graph[0] + 1) % n;
  int times = 1 + (cur == 1);
  while (cur != 0) {
    int before = cur;
    if (graph[cur] <= before) {
      // cout << graph[cur] << " " << before << endl;
      times++;
    }
    cur = (graph[cur] + 1) % n;
  }
  // cout << times << endl;
  if (k % times == 0) {
    return 0;
  }
  int other = k % times;
  cur = 0;
  times = 0;
  while (times < other - 1) {
    int before = cur;
    if (graph[cur] <= before) {
      times++;
    }
    cur = (graph[cur] + 1) % n;
  }
  vector<int> res;
  while (cur < n) {
    int next = graph2[cur];
    if (next == -1) {
      res.push_back(arr[cur]);
      cur++;
    } else {
      cur = graph2[cur] + 1;
    }
  }
  for (int i = 0; i < res.size(); i++) {
    cout << res[i] << " ";
  }
  return 0;
}