Https://cses.fi/problemset/task/2163

what approach can be used to solve the below problem

1 Like

I have used the below approach to solve the problem.

#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;

set<int> set;

for (int i = 1; i <= n; ++i) set.insert(i);

int k = 2;
int multiply = 2;
int last = 0;
while (!set.empty())
{
    last = *set.rbegin();
    while (k <= n)
    {
        cout << k << " ";
        set.erase(k);
        k += multiply;
    }
    multiply *= 2;

    if (!set.empty()) {
        k = *set.rbegin() != last && set.size() > 1 ? *++set.begin() : k = *set.begin();
        }
} 

}