forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestNumberFormedFromAnArray.java
69 lines (61 loc) · 2.01 KB
/
LargestNumberFormedFromAnArray.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*https://practice.geeksforgeeks.org/problems/largest-number-formed-from-an-array1117/1/*/
class Solution {
String printLargest(String[] arr) {
// code here
Arrays.sort(arr, new Comparator<String>()
{
public int compare(String s1, String s2)
{
int i = 0, j = 0;
char ch1, ch2;
while (i < s1.length() && j < s2.length())
{
ch1 = s1.charAt(i);
ch2 = s2.charAt(j);
if (ch1 == ch2)
{
++i;
++j;
}
else
{
if (ch1 > ch2) return -1;
return 1;
}
}
if (i == s1.length() && j == s2.length()) return 0;
int res1 = Integer.parseInt(s1+s2);
int res2 = Integer.parseInt(s2+s1);
return res2-res1;
}
});
StringBuffer buffer = new StringBuffer("");
int i, n = arr.length;
for (i = 0; i < n; ++i) buffer.append(arr[i]);
return buffer.toString();
}
}
//another solution with almost same time
class Solution {
String printLargest(String[] arr) {
// code here
Arrays.sort(arr, new Comparator<String>()
{
public int compare(String X, String Y)
{
// first append Y at the end of X
String XY = X + Y;
// then append X at the end of Y
String YX = Y + X;
// Now see which of the two
// formed numbers
// is greater
return XY.compareTo(YX) > 0 ? -1 : 1;
}
});
StringBuffer buffer = new StringBuffer("");
int i, n = arr.length;
for (i = 0; i < n; ++i) buffer.append(arr[i]);
return buffer.toString();
}
}