The code is working perfectly for small inputs but there is a runtime error for larger ones. the question was from hackerearth and it went like
You are given a set S consisting of non-negative powers of three S={1, 3, 9, 27} . Consider the sequence of all non-empty subsets of ordered by the value of the sum of their elements. You are also given a single element n. You are required to find the subset at the nth position in the sequence and print it in increasing order of its elements.
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
#define ll long long
ll poww(ll n,ll p)
{
ll power=1;
for(ll i=0;i<p;i++)
{
power=power*n;
}
return power;
}
int main(){
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
ll a[n];
for(ll i=0; i<n; i++){
a[i]=poww(3, i);
}
ll idx[n]={0};
ll i=0, count=0;
for(ll j=0; j<n; j++){
if(n & (1<<j)){
count++;
idx[i]=a[j];
i++;
}
}
cout<<count<<endl;
for(ll k=0; k<i; k++){
cout<<idx[k]<<" ";
}
cout<<endl;
}
return 0;
}