Input Format

The first line consists of a single integer, , denoting the number of integer strings.
Each line  of the  subsequent lines contains a real number denoting the value of .

Constraints

  • Each  has at most  digits.

Output Format

Locked stub code in the editor will print the contents of array  to stdout. You are only responsible for reordering the array's elements.


hackerrank java bigdecimal solution


Java BigDecimal problem solution | HackerRank

import java.math.BigDecimal;
import java.util.*;
class Solution {
    public static void main(String []args){
        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();
        }

        for(int i = 0;i<n;i++){
            BigDecimal max = new BigDecimal(s[i]);
            int idx = i;
            for(int j = i+1;j<n;j++)
            {
                BigDecimal curr = new BigDecimal(s[j]);
                if(curr.compareTo(max) == 1){
                    max=curr;
                    idx=j;
                }
            }
            String temp = s[i];
            s[i] = s[idx];
            s[idx] = temp;
        }

        for(int i = 0;i<n;i++){
            System.out.println(s[i]);
        }

    }
}