Skip to content

Commit 80a4649

Browse files
author
Ram swaroop
committed
coded + unit tested
1 parent debc964 commit 80a4649

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.ramswaroop.arrays;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
/**
7+
* Created by IntelliJ IDEA.
8+
*
9+
* @author: ramswaroop
10+
* @date: 11/1/15
11+
* @time: 8:53 PM
12+
*/
13+
public class ArrangeNosToFormBiggestNo {
14+
15+
/**
16+
* Sorts no.s in array {@param a} such that if you form a number {@code n}
17+
* by concatenating digits in order a[0]....a[size], it results being the
18+
* largest number possible.
19+
* <p/>
20+
* For example,
21+
* I/P: {54, 546, 548, 60}
22+
* O/P: {60, 548, 546, 54} i.e, 6054854654
23+
* <p/>
24+
* I/P: {1, 34, 3, 98, 9, 76, 45, 4}
25+
* O/P: {9, 98, 76, 45, 4, 34, 3, 1} i.e, 998764543431
26+
*
27+
* @param a
28+
* @return
29+
*/
30+
public static Integer[] arrangeArrayOfNosToFormBiggestNo(Integer[] a) {
31+
32+
Arrays.sort(a, new Comparator<Integer>() {
33+
@Override
34+
public int compare(Integer o1, Integer o2) {
35+
return Integer.parseInt(o1 + "" + o2) > Integer.parseInt(o2 + "" + o1) ? -1 : 1;
36+
}
37+
});
38+
39+
return a;
40+
}
41+
42+
public static void main(String a[]) {
43+
System.out.println(Arrays.toString(arrangeArrayOfNosToFormBiggestNo(new Integer[]{45, 567, 12, 1})));
44+
System.out.println(Arrays.toString(arrangeArrayOfNosToFormBiggestNo(new Integer[]{54, 546, 548, 60})));
45+
System.out.println(Arrays.toString(arrangeArrayOfNosToFormBiggestNo(new Integer[]{1, 34, 3, 98, 9, 76, 45, 4})));
46+
}
47+
}

0 commit comments

Comments
 (0)