-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLargest_number.java
53 lines (43 loc) · 1.21 KB
/
Largest_number.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
given an array of integer, you have to create the largest number using those elements
*/
import java.util.*;
import java.util.Comparator;
public class largest_number {
public static void main(String[] args) {
Integer arr[]={3,30,34,5,9};
String res="";
String a= "12";
List<Integer> list= Arrays.asList(arr);
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer x, Integer y) {
// Intentional: Reverse order for this demo
String p= x+"";
String q= y+"";
String PQ= p+q;
String QP= q+p;
return PQ.compareTo(QP)>0? -1:1;
}
});
Iterator itr = list.iterator();
int count_zeros=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==0)
count_zeros++;
}
while(itr.hasNext())
{
res+=itr.next();
}
System.out.println(count_zeros);
if(count_zeros==list.size())
System.out.println(0);
else
System.out.println(res);
}
}
System.out.println(res);
}
}