0

Largest Possible Number

Write a function that given a list of non negative integers, arranges them such that they form the largest possible number. For example, given [0, 1, 2, 3], the largest formed number is 3210. Input Format The first line contains one integer N,followed by a line containing N space separated integers. constraints 2 <= N <= 100 1 <= a[i] <= 10^8 Output Format A largest integer, that is formed by arranging the given numbers. Warning: Output may be larger use appropriate data types. Sample Input 4 0 1 2 3 Sample Output 3210 Explanation 3210 is the largest number formed by arranging [0, 1, 2, 3].

19th Nov 2017, 12:20 PM
Mohd Shahid Ameen
Mohd Shahid Ameen - avatar
4 odpowiedzi
+ 1
https://code.sololearn.com/cgQGAN45xbQm/?ref=app Note: It doesn't require to input the length, only the digits.
19th Nov 2017, 10:27 PM
Boris Batinkov
Boris Batinkov - avatar
0
public class Main { public static void main(String[] args) { int n; int count = 0; n = in.nextInt(); int[] intArray = new int[n]; while (stringNumber.equals("")) { stringNumber = in.nextLine(); } String strArray[] = stringNumber.split(" "); for (String s : strArray) { intArray[count++] = Integer.parseInt(s); } for (int i = 0; i < intArray.length; i++) { for (int j = 1; j < intArray.length - i; j++) { int largestLength; boolean isNum1LengthGreater = false; int[] num1 = convertToArray(intArray[j]); int[] num2 = convertToArray(intArray[j - 1]); if (num1.length >= num2.length) { largestLength = num1.length; isNum1LengthGreater = true; } else { largestLength = num2.length; } int k1 = 0; int k2 = 0; for (int k = 0; k < largestLength; k++) { if (isNum1LengthGreater) { if (k >= num2.length) { k1 = 0; } if (num1[k] == num2[k1]) { k1++; continue; } if (num1[k] > num2[k1++]) { int temp = intArray[j]; intArray[j] = intArray[j - 1]; intArray[j - 1] = temp; } break; } else { if (k >= num1.length) { k2 = 0; } if (num1[k2] == num2[k]) { k2++; continue; }
19th Nov 2017, 12:22 PM
Mohd Shahid Ameen
Mohd Shahid Ameen - avatar
- 1
any other optimize solution??
19th Nov 2017, 12:26 PM
Mohd Shahid Ameen
Mohd Shahid Ameen - avatar