Language: C++

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 5;

int a[maxn<<1];
int next[maxn];
int num[maxn];
int q[maxn], top;
int main() {
    int n;
    long long k;
    scanf("%d%lld", &n, &k);
    for(int i=0;i<n;i++)
    {
        scanf("%d", a + i);
        a[i+n] = a[i];
    }
    memset(next,-1,sizeof(next));
    for(int i=2*n-1;i>=0;i--)
    {
        next[i] = num[a[i]];
        num[a[i]] = i;
    }
    for(int i=0;i<n;i++)
    {
        if(next[i] >= n) next[i] -= n;
    }
    int cnt=1, now=0, current;
    memset(num, 0, sizeof(num));
    while(1)
    {
        //printf("%dss %d\n",now, cnt);
        if(num[now]){
            current = cnt - num[now];
            break;
        }
        else{
            num[now] = cnt;
            if(next[now] <= now){
                cnt++;
            }
            now = next[now];
            now++;
            if(now>=n){
                now=0;
                cnt++;
            }
        }
    }
    if(k>=cnt){
        k -= cnt;
        k++;
        k %= current;
    }
    else{
        now = 0;
    }
    //printf("%lld %d %d\n", k, current, cnt);
    top = 0;
    cnt = 0;
    while(cnt < k)
    {
        if(cnt < k-1||(next[now] > now)){
            if(next[now] <= now) cnt++;
            now = next[now];
        }
        else{
            q[top++] = a[now];
        }
        now++;
        if(now>=n){
            now=0;
            cnt++;
        }
    }
    for(int i=0;i<top;i++)
    {
        printf("%d%c",q[i], i==top-1?'\n':' ');
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:20:12: error: reference to 'next' is ambiguous
   20 |     memset(next,-1,sizeof(next));
      |            ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:20:27: error: reference to 'next' is ambiguous
   20 |     memset(next,-1,sizeof(next));
      |                           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:23:9: error: reference to 'next' is ambiguous
   23 |         next[i] = num[a[i]];
      |         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:28:12: error: reference to 'next' is ambiguous
   28 |         if(next[i] >= n) next[i] -= n;
      |            ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:28:26: error: reference to 'next' is ambiguous
   28 |         if(next[i] >= n) next[i] -= n;
      |                          ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:41:16: error: reference to 'next' is ambiguous
   41 |             if(next[now] <= now){
      |                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:44:19: error: reference to 'next' is ambiguous
   44 |             now = next[now];
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:65:24: error: reference to 'next' is ambiguous
   65 |         if(cnt < k-1||(next[now] > now)){
      |                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:66:16: error: reference to 'next' is ambiguous
   66 |             if(next[now] <= now) cnt++;
      |                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~
a.cc:67:19: error: reference to 'next' is ambiguous
   67 |             now = next[now];
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [200005]'
    8 | int next[maxn];
      |     ^~~~