amitp49
BAN USER
Consider every number as string.
Then sort (descending) them with any sorting method. (Bucket sort can also be used as our string will only contain 0-9)
Once sorted, append each string in order to result string.
Convert result string to number again, which is nothing but maximum possible number.
E.G. {21,9,23} -> {"21","9","23"} ->{"9","23","21"} -> {"92321"} -> 92321
#include <stdio.h>
#include <stdlib.h>
#define SIZEOF(a) sizeof(a) / sizeof((a)[0])
char order[26];
void generateOrder(char dict[])
{
int i=0;
for(i=0;i<26;i++)
{
order[ dict[i] - 'a' ] = i;
}
}
int position(char x)
{
return order[x-'a'];
}
int compare(void *a,void *b)
{
return position( *(char *)a) - position (*(char *)b );
}
int main(void) {
char dict[26] = { 'p','a','b','d','e',
'f','g','h','i','j',
'k','l','m','n','o',
'c','q','r','s','t',
'u','v','w','x','y',
'z'};
generateOrder(dict);
char str[6] = "sheep";
qsort(str,SIZEOF(str),sizeof(char),compare);
printf("%s",str);
return 0;
}
Here we don't have to do actual calculation, else we will never get answer in written test. Think of this as mathematical + logical question. Lets say we have all permutation of 1234. It will be 4! = 24. Now if we look at each column of this list carefully, every number will appear for 6 time.
- amitp49 June 26, 20131234
1324
2134
2314
3124
3214
...
Now if you just concentrate on 4th column, "4" is coming 6 times, same way if we list all number each number will come 6 time in 4th column. So sum of 4th column is (4*6 + 3*6 + 2*6 + 1*6) = 60.
Same way, for all other columns sum would be 60.
Now we are just one step far away from answer. And that is consider "carry". 4th column will give 6 as carry to 3rd columns, again in turn 3rd column will give 6 as carry to 2nd column and so on.
Therefore, final answer would be 66660.