Created
December 12, 2016 06:27
-
-
Save pungrue26/b6baf599fa0f3abc10af667b2089915c to your computer and use it in GitHub Desktop.
GeeksforGeeks, Largest Number formed from an Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class GFG { | |
public static void main (String[] args) { | |
Scanner in = new Scanner (System.in); | |
int t = in.nextInt(); | |
for (int i = 0; i < t; i++) { | |
int n = in.nextInt(); | |
String [] array = new String [n]; | |
for (int j = 0; j < n; j++) { | |
array[j] = String.valueOf(in.nextInt()); | |
} | |
System.out.println(form(array)); | |
} | |
in.close(); | |
} | |
private static String form (String [] array) { | |
Arrays.sort(array, new Comparator<String>(){ | |
@Override | |
public int compare(String s1, String s2) { | |
return comp(s1, s2); | |
} | |
}); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = array.length - 1; i >= 0; i--) { | |
sb.append(array[i]); | |
} | |
return sb.toString(); | |
} | |
private static int comp(String s1, String s2) { | |
int i1 = 0, i2 = 0; | |
while (i1 < s1.length() && i2 < s2.length()) { | |
if (s1.charAt(i1) < s2.charAt(i2)) { | |
return -1; | |
} else if (s1.charAt(i1) > s2.charAt(i2)) { | |
return 1; | |
} | |
i1++; | |
i2++; | |
} | |
if (i1 >= s1.length() && i2 >= s2.length()) { | |
return 0; | |
} else if (i1 >= s1.length()) { | |
return comp(s1, s2.substring(i2)); | |
} else { | |
return comp(s1.substring(i1), s2); | |
} | |
} | |
} |
public class Practice {
public static void findMin(List<Integer> myArrayList){
List<Integer>myList=new ArrayList<>();
for(int i=0;i<myArrayList.size();i++){
int value= myArrayList.get(i)%10;
myList.add(value);
}
System.out.println(myList);
}
public static void main(String args[]){
List<Integer> list= new ArrayList<>();
list.add(9);
list.add(5);
list.add(34);
list.add(30);
list.add(70);
list.add(3);
System.out.println(list);
Comparator myComparator= new Comparator(){
@Override
public int compare(Object t, Object t1) {
Integer int1= (Integer)(t);
Integer int2= (Integer)(t1);
Integer value1= Integer.parseInt(Integer.toString( int1)+Integer.toString( int2));
Integer value2= Integer.parseInt(Integer.toString( int2)+Integer.toString( int1));
System.out.println(value1+" "+value2);
return value1<value2? 1: value2<value1? -1:0;
//To change body of generated methods, choose Tools | Templates.
}
};
Collections.sort(list,myComparator);
System.out.println(list);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public class Practice {
}