1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Online C++ compiler to run C++ program online
#include <iostream>
#include <bits/stdc++.h>
#include <vector>

using namespace std;

int countSetBits(int n)
{
    if (n == 0)
        return 0;
    else
        return 1 + countSetBits(n & (n - 1));
}

int main() {
    int N ;
    cin>>N;
    int counter=0;
    int bitSum=0;
    vector <int> A ;
    while (bitSum < N){
        counter ++;
        A.push_back(counter);
        bitSum+= countSetBits(counter);
       // cout<< counter << " sum " << bitSum << endl;
    }
  //  cout<<"end "<< counter << " sum " << bitSum <<endl; 
    if( bitSum == N){
        cout<< A.size() <<endl;
        for (  int i =A.size()-1; i>=0 ;i--){
            cout<<A[i]<<" ";
        }
        return 0;
    }
   // cout<<"not equal" << endl;
     for ( int i =A.size()-1; i>=0 ;i--){
          if(bitSum-countSetBits(A[i])>N){
              bitSum -=countSetBits(A[i]);
             A.erase(A.begin() + i);
          }
          if(bitSum-countSetBits(A[i]) == N){
              bitSum -= countSetBits(A[i]);
             A.erase(A.begin() + i);
             break;
          }
          continue;
    }
    
    cout<< A.size() <<endl;
    for (  int i =A.size()-1; i>=0 ;i--){
        cout<<A[i]<<" ";
    }
    
    return 0;
}