In this HackerRank Java BigDecimal problem, we have given an array of real number strings, sort them in descending order and print each number exact same format as it was read from stdin.

HackerRank Java BigDecimal problem solution

HackerRank Java BigDecimal problem solution

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]);
        }

    }
}