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 Counting Sort 2 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 – Counting Sort 2 – Hacker Rank Solution
Counting Sort 2 – Hacker Rank Solution
Problem:
Often, when a list is sorted, the elements being sorted are just keys to other values. For example, if you are sorting files by their size, the sizes need to stay connected to their respective files. You cannot just take the size numbers and output them in order, you need to output all the required file information.
The counting sort is used if you just need to sort a list of integers. Rather than using a comparison, you create an integer array whose index range covers the entire range of values in your array to sort. Each time a value occurs in the original array, you increment the counter at that index. At the end, run through your counting array, printing the value of each non-zero valued index that number of times.
For example, consider an array . All of the values are in the range , so create an array of zeroes, . The results of each iteration follow:
i arr[i] result 0 1 [0, 1, 0, 0] 1 1 [0, 2, 0, 0] 2 3 [0, 2, 0, 1] 3 2 [0, 2, 1, 1] 4 1 [0, 3, 1, 1]
Now we can print the sorted array: .
Challenge
Given an unsorted list of integers, use the counting sort method to sort the list and then print the sorted list.
Hint: You can use your previous code that counted the items to print out the actual values in order.
Function Description
Complete the countingSort function in the editor below. It should return the original array, sorted ascending, as an array of integers.
countingSort has the following parameter(s):
- arr: an array of integers
Input Format
The first line contains an integer , the length of . The next line contains space-separated integers where .
Constraints
Output Format
Print the sorted list as a single line of space-separated integers.
Sample Input
100 63 25 73 1 98 73 56 84 86 57 16 83 8 25 81 56 9 53 98 67 99 12 83 89 80 91 39 86 76 85 74 39 25 90 59 10 94 32 44 3 89 30 27 79 46 96 27 32 18 21 92 69 81 40 40 34 68 78 24 87 42 69 23 41 78 22 6 90 99 89 50 30 20 1 43 3 70 95 33 46 44 9 69 48 33 60 65 16 82 67 61 32 21 79 75 75 13 87 70 33
Sample Output
1 1 3 3 6 8 9 9 10 12 13 16 16 18 20 21 21 22 23 24 25 25 25 27 27 30 30 32 32 32 33 33 33 34 39 39 40 40 41 42 43 44 44 46 46 48 50 53 56 56 57 59 60 61 63 65 67 67 68 69 69 69 70 70 73 73 74 75 75 76 78 78 79 79 80 81 81 82 83 83 84 85 86 86 87 87 89 89 89 90 90 91 92 94 95 96 98 98 99 99
Explanation
Once our counting array has been filled, loop from index to the end, printing each value times.
Counting Sort 2 – Hacker Rank Solution
import java.util.Scanner; /** * @author Techno-RJ * */ public class CountingSort2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int a[] = new int[N]; int b[] = new int[100]; int c[] = new int[N + 1]; for (int i = 0; i < N; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < N; i++) { b[a[i]]++; } for (int i = 1; i < 100; i++) { b[i] = b[i] + b[i - 1]; } for (int j = 0; j < N; j++) { c[b[a[j]]] = a[j]; b[a[j]] = b[a[j]] - 1; } for (int j = 1; j <= N; j++) { System.out.print(c[j] + " "); } sc.close(); } }
order cialis 5mg online order tadalafil 40mg sale best ed pills online
Seating at Di Bruno Bros. in the Italian MarketPhiladelphia’s Italian Market is among the oldest and biggest open-air markets in the nation.
cefadroxil 250mg pills combivir tablet buy propecia paypal
estrace 1mg without prescription estradiol price order prazosin 2mg generic
diflucan 100mg oral buy fluconazole without a prescription buy generic cipro
mebendazole 100mg uk purchase mebendazole generic tadalafil 20mg sale
flagyl 200mg sale purchase cephalexin generic order cephalexin 250mg online
generic avana purchase tadalafil sale voltaren 50mg price
buy cleocin 300mg pills can i buy ed pills over the counter best ed pills online
buy indomethacin 75mg online cheap order cefixime 100mg online cheap order cefixime 100mg for sale
nolvadex pills nolvadex brand ceftin online order
oral amoxicillin 250mg order amoxicillin 500mg generic cost biaxin 500mg
how to buy bimatoprost buy generic trazodone 100mg trazodone drug
clonidine price buy meclizine 25 mg pills spiriva 9 mcg sale
minocycline 100mg for sale buy pioglitazone 15mg pills buy actos sale
arava online buy arava online cheap azulfidine 500 mg uk
accutane oral order amoxil 1000mg generic buy generic azithromycin over the counter
ivermectin virus buy stromectol 3mg sale order prednisone pill
buy azithromycin 250mg pill buy generic prednisolone 20mg neurontin 100mg brand
order lasix online cheap ventolin canada purchase albuterol pill
buy generic ramipril over the counter buy amaryl 4mg online cheap etoricoxib for sale
buy levitra 20mg pill brand zanaflex hydroxychloroquine 400mg over the counter
mesalamine online order irbesartan buy online avapro 150mg for sale
vardenafil order online hydroxychloroquine price hydroxychloroquine brand
how to buy temovate purchase amiodarone for sale how to buy amiodarone
order olmesartan pills order divalproex 250mg pill buy divalproex 500mg for sale
where can i buy clobetasol order temovate order generic cordarone 100mg
oral diamox 250 mg order imuran online cheap imuran 25mg usa
lanoxin brand lanoxin 250 mg drug buy molnupiravir 200 mg pill
order carvedilol 6.25mg pills aralen us aralen 250mg ca