Say you have an array of positive integers, manipulate them so that the concatenation of the integers of the resultant array is the largest number possible. Ex: {9,1,95,17,5}, r
Interesting question. I suppose you could start with the simplest approach first, which would use brute force to create all possible numbers using recursion (depending on the problem & language this might have to be changed to iteration) and keep track of which one is the largest. For example {1, 83, 91} would give you { 18391, 19183, 83191, 83911, 91183, 91831 } from which you could determine 91831 as the largest number.
Using a solution which sorts the original numbers and concatenates the numbers in order has a pitfall in that if you have something like { 9, 82, 99 }, the sorted array would be { 99, 82, 9 }. However, this would result in 99829, when the largest number would actually be 99982.
Since the brute force solution works, but may not be optimal in terms of performance, it might be good to check out ways to optimize the solution (after profiling the original solution, of course). For example, you could start with a simple ordering scheme by multiplying individual digits of a number by the place they would occupy.
{ 9, 98, 95 }
The above set will result in a 5 digit number. We'll apply a simple formula which multiplies the individual digit by its place (1 for 1's place, 2 for 10's place, etc.) and sums them like so:
9 -> 9 * 5
98 -> 9 * 5 + 8 * 4
95 -> 9 * 5 + 5 * 4
which results in
9 -> 45
98 -> 77
95 -> 65
Now, as humans, we know that the 9 should come first, not 98 or 95. One way to fix this would them be to favor single digit numbers if the first digits of the candidates are identical (ie, favor 9 or 98/95/etc.). Generalizing a bit, you might choose the candidate with fewer digits each time if the digits from the left are larger or equivalent (if the number of digits is equal, use the above formula). If we have { 9871, 986 }, 9871 would have a higher value, but we would look at 986 and see that it has fewer digits.
9 8 7 1
| | | |
9 8 6
8 matches, continue, 7 is larger, so ignore 986 (9871986 versus the smaller 9869871). If the set were { 9861, 987 } instead:
9 8 6 1
| | | |
9 8 7
8 matches, continue, 7 is larger, so choose 987 (9879861 versus the smaller 9861987).
So testing this using the following set:
{ 7, 61, 811, 99 }
The result will be an 8 digit number. Applying the placement formula gives:
7 -> 7 * 8 = 56
61 -> 6 * 8 + 1 * 7
811 -> 8 * 8 + 1 * 7 + 1 * 6 = 77
99 -> 9 * 8 + 9 + 7 = 135
So 99 looks like it will go first, but now let's apply the second part of the algorithm by selecting the numbers with fewer digits:
7
7 is not identical with 9, of course, so we are left with 99 as the first number.
9 9 _ _ _ _ _
Next iteration:
7 -> 7 * 6 = 42
61 -> 6 * 6 + 1 * 5 = 41
811 -> 8 * 6 + 1 * 5 + 1 * 4 = 57
811 has the highest value and both 61 an 7 do not have identical digits from left to right so we insert 811.
9 9 8 1 1 _ _ _
Next iteration:
7 -> 7 * 3 = 21
61 -> 6 * 3 + 1 * 2 = 20
7 has the higher value and nothing has fewer digits--insert:
9 9 8 1 1 7 _ _
Next iteration:
Only one number (61) is left, so we'll insert it
9 9 8 1 1 7 6 1 -> 99811761
and get the biggest number! Note that if 61 had been something like 81, it would have correctly ended up in 811's spot -> 99818117 instead of the incorrect 99811817.