iisc.mukesh
BAN USERpublic int[] getSum(int[]a , int[]b ){
int n1 = a.length();
int n2 = b.length();
int i = n1-1;
int j = n2-2;
int max = Math.max(n1,n2)+1;
int[]res = new int[max];
int c = 0 ; int k = max-1;
while(i >= 0 && j >= 0 ){
int s = a[i]+b[j]+c;
if(s > 9){
c = 1; res[k--] = s%10;
}else{
c = 0 ;
res[k--] = s;
}
--i ; --j;
}
if( i >= 0 ){
while( i >= 0 ){
int s = a[i]+c;
if(s > 9){
c = 1; res[k--] = s%10;
}else{
c = 0 ;
res[k--] = s;
}
--i ;
}
}
if( j >= 0 ){
while( j >= 0 ){
int s = b[j]+c;
if(s > 9){
c = 1; res[k--] = s%10;
}else{
c = 0 ;
res[k--] = s;
}
--j ;
}
}
if(c > 0 ) res[0] = c;
return res;
Convert integers to string. String s1 comes before s2 in concatenation if (s1+s2) > (s2+s1).
Code :
public class Solution {
public int getMaxNumber(int[] inputArray){
int n = inputArray.length;
String[] temp = new String[n];
for(int i = 0 ; i < n ; ++i ){
temp[i] = Integer.toString(inputArray[i]);
}
Arrays.sort(temp,new Comparator<String>(){
public int compare(String s1, String s2){
String a = s1+s2;
String b = s2+s1;
return b.compareTo(a);
}
});
String res = "";
for(String t : temp){
res+= t;
}
return Integer.parseInt(res);
}
public static void main(String[]s){
Solution ob = new Solution();
int[] ar = {9,91,907};
System.out.println(ob.getMaxNumber(ar));
}
}
}
- iisc.mukesh November 30, 2015