Java BigDecimal HackerRank Solution with Explanation

Java BigDecimal - Hacker Rank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Java BigDecimal Problem with a very easy explanation. This is the 24th problem of Java on HackerRank. In this article, you will get more than one approach to solve this problem. So let's start-

{tocify} \$title={Table of Contents}

HackerRank Java BigDecimal - Problem Statement

Java's BigDecimal class can handle arbitrary-precision signed decimal numbers. Let's test your knowledge of them!

Given an array, s, of n real number strings, sort them in descending order — but wait, there's more! Each number must be printed in the exact same format as it was read from stdin, meaning that .1 is printed as .1, and 0.1 is printed as 0.1. If two numbers represent numerically equivalent values (e.g.,.1  ☰ 0.1 ), then they must be listed in the same order as they were received as input).

Complete the code in the unlocked section of the editor below. You must rearrange the array's elements according to the instructions above.

Input Format

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

Constraints

• 1 <= n <= 200
• Each Si has at most 300 digits.

Output Format

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

Sample Input

9 -100 50 0 56.6 90 0.12 .12 02.34 000.000 {codeBox}

Sample Output

90 56.6 50 02.34 0.12 .12 0 000.000 -100 {codeBox}

Java BigDecimal - Hacker Rank Solution

Approach I:

``````import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int i;
for(i=1;i<=10;i++)
{
System.out.println(+N +" x "+i+" = "+N*i);
}
scanner.close();
}
}``````

Approach II:

``````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();
Arrays.sort(s, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
if (s1 == null || s2 == null) {
return 0;
}
BigDecimal bd1 = new BigDecimal(s1);
BigDecimal bd2 = new BigDecimal(s2);
return bd2.compareTo(bd1);
}
});

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

Also Read:

Disclaimer: The above Problem (Java BigDecimal) is generated by Hackerrank but the Solution is Provided by MyeduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these solutions will pass all the test cases. Now visit Java BigDecimal HackerRank Problem and try to solve it again.

All the Best!