#include<iostream>
#include<cstdlib>
using namespace std;
int table[10];
void largest_number(int a[],int len)
{
int rem[3] = {0};
for(int i=0;i<10;i++)
table[i] = 0;
int sum = 0;
for(int i=0;i<len;i++)
{
sum += a[i];
table[a[i]]++;
rem[a[i]%3]++;
}
if(sum%3 == 1)
{
if(rem[1] < 1 && rem[2]<2)
{ cout << "not possible \n";
return;
}
if(rem[1] > 0)
{
int remove_element = 1;
for(int i=0;i<10 && remove_element > 0;i++)
{
if(i%3 == 1 && table[i] > 0)
{ table[i]--;
remove_element--;
}
}
}
else
{
int remove_element = 2;
for(int i=0;i<10 && remove_element > 0;i++)
{
if(i%3 == 2 && table[i] > 0)
{ table[i]--;
remove_element--;
}
}
}
}
if(sum%3 == 2)
{
if(rem[2]<1 && rem[1]<2)
{ cout << "not possible \n";
return;
}
if(rem[2] > 0)
{
int remove_element = 1;
for(int i=0;i<10 && remove_element > 0;i++)
{
if(i%3 == 2 && table[i] > 0)
{ table[i]--;
remove_element--;
}
}
}
else
{
int remove_element = 2;
for(int i=0;i<10 && remove_element > 0;i++)
{
if(i%3 == 1 && table[i] > 0)
{ table[i]--;
remove_element--;
}
}
}
}
for(int i=9;i>=0;i--)
while(table[i]--)
cout << i << " ";
cout << endl;
}
int main()
{
int a[] = {8,1,7,6,0};
int len = sizeof(a)/sizeof(a[0]);
largest_number(a,len);
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGNzdGRsaWI+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IHRhYmxlWzEwXTsKCnZvaWQgbGFyZ2VzdF9udW1iZXIoaW50IGFbXSxpbnQgbGVuKQp7CglpbnQgcmVtWzNdID0gezB9OyAKCWZvcihpbnQgaT0wO2k8MTA7aSsrKQoJCXRhYmxlW2ldID0gMDsKCQoJaW50IHN1bSA9IDA7Cglmb3IoaW50IGk9MDtpPGxlbjtpKyspCgl7CgkJc3VtICs9IGFbaV07CgkJdGFibGVbYVtpXV0rKzsKCQ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