Java BigDecimal Hackerrank Solution
For Explanation Watch Video:
Sample Input
9
-100
50
0
56.6
90
0.12
.12
02.34
000.000
Sample Output
90
56.6
50
02.34
0.12
.12
0
000.000
-100
Code:
import java.math.BigDecimal;import java.util.*;class Solution{
public static void main(String []args){ //Input Scanner sc= new Scanner(System.in); int n=sc.nextInt(); String []s=new String[n+2]; for(int i=0;i<n;i++){ s[i]=sc.next(); } sc.close();
Comparator<String> customComp = new Comparator<String>() { public int compare(String a,String b){ BigDecimal a1 = new BigDecimal(a); BigDecimal b1 = new BigDecimal(b); return b1.compareTo(a1); } }; Arrays.sort(s,0,n,customComp); //Output for(int i=0;i<n;i++) { System.out.println(s[i]); } }
}
Comments
Post a Comment