Puneet A
BAN USERint a;
cin>>a;
int b=a;
int c[10];
for(int i=0;i<10;i++)
c[i]=0;
int x=-1,y=-1;
i=0;
while(b!=0)
{
i++;
x=b%10;
c[x]++;
b/=10;
if(y>x)
break;
y=x;
}
if(x==y)
cout<<"No. is largest";
else
{
for(int j=++x;j<10;j++)
if(c[j]>0)
{
b=b*10+j;
c[j]--;
i--;
break;
}
int k=0;
for(j=0;j<i && k<10;)
{
if(c[k]>0)
{
b=b*10+k;
c[k]--;
j++;
}
else
k++;
}
cout<<b;
}
As I think we can use it like Insertion Sort with Linked List.
Just take 2 teams first, say T1 and T2 in their order of winning. Then take T3. There are 3 cases for positioning it starting from left.
1. It has lost the game with T1, then sequence would be T3,T1,T2.
2. Won with T1 and lost with T2, then sequence would be T1,T3,T2.
3. Won with T1 and T2, then sequence would be T1,T2,T3.
In similar manner we can check for other teams and position those.
I have chosen linked list so as to make positioning easy in case new team is to be positioned in between.
Also, I believe that in some scenarios depending on teams, there can be multiple solutions depending on the order in which we start traversing the teams.
I have used here the counting sort to store all the digits till I get the two digits in descending order from right to left.
- Puneet A August 05, 2013