Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Big Sorting in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Algorithm
The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.
Advantages of Algorithms:
- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.
Link for the Problem – Big Sorting– Hacker Rank Solution
Big Sorting – Hacker Rank Solution
Problem:
Consider an array of numeric strings where each string is a positive number with anywhere from to digits. Sort the array’s elements in non-decreasing, or ascending order of their integer values and return the sorted array.
Example
Return the array [‘1’, ‘3’, ‘150’, ‘200’].
Function Description
Complete the bigSorting function in the editor below.
bigSorting has the following parameter(s):
- string unsorted[n]: an unsorted array of integers as strings
Returns
- string[n]: the array sorted in numerical order
Input Format
The first line contains an integer, , the number of strings in .
Each of the subsequent lines contains an integer string, .
Constraints
- Each string is guaranteed to represent a positive integer.
- There will be no leading zeros.
- The total number of digits across all strings in is between and (inclusive).
Sample Input 0
6 31415926535897932384626433832795 1 3 10 3 5
Sample Output 0
1 3 3 5 10 31415926535897932384626433832795
Explanation 0
The initial array of strings is . When we order each string by the real-world integer value it represents, we get:
We then print each value on a new line, from smallest to largest.
Sample Input 1
8 1 2 100 12303479849857341718340192371 3084193741082937 3084193741082938 111 200
Sample Output 1
1 2 100 111 200 3084193741082937 3084193741082938 12303479849857341718340192371
Big Sorting – Hacker Rank Solution
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; /** * @author Techno-RJ * */ public class BigSorting { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String[] unsorted = new String[n]; for (int unsorted_i = 0; unsorted_i < n; unsorted_i++) { unsorted[unsorted_i] = in.next(); } Arrays.sort(unsorted, new Comparator<String>() { public int compare(String s1, String s2) { return compareStrings(s1, s2); } }); printArray(unsorted); in.close(); } private static int compareStrings(String s1, String s2) { if (s1.length() < s2.length()) { return -1; } else if (s1.length() > s2.length()) { return 1; } for (int k = 0; k < s1.length(); k++) { if ((int) s1.charAt(k) < (int) s2.charAt(k)) return -1; if ((int) s1.charAt(k) > (int) s2.charAt(k)) return 1; } return 0; } private static void printArray(String[] unsorted) { for (String string : unsorted) { System.out.println(string); } } }