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 Insertion Sort Advanced Analysis 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 – Insertion Sort Advanced Analysis– Hacker Rank Solution
Insertion Sort Advanced Analysis– Hacker Rank Solution
Problem:
Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, arrays may be too large for us to wait around for insertion sort to finish. Is there some other way we can calculate the number of shifts an insertion sort performs when sorting an array?
If is the number of elements over which the element of the array has to shift, then the total number of shifts will be … + .
Example
Array Shifts [4,3,2,1] [3,4,2,1] 1 [2,3,4,1] 2 [1,2,3,4] 3 Total shifts = 1 + 2 + 3 = 6
Function description
Complete the insertionSort function in the editor below.
insertionSort has the following parameter(s):
- int arr[n]: an array of integers
Returns
– int: the number of shifts required to sort the array
Input Format
The first line contains a single integer , the number of queries to perform.
The following pairs of lines are as follows:
- The first line contains an integer , the length of .
- The second line contains space-separated integers .
Constraints
![Insertion Sort Advanced Analysis in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 image 123](https://technorj.com/wp-content/uploads/2021/12/image-123.png)
Sample Input
2 5 1 1 1 2 2 5 2 1 3 1 2
Sample Output
0 4
Explanation
The first query is already sorted, so there is no need to shift any elements. In the second case, it will proceed in the following way.
Array: 2 1 3 1 2 -> 1 2 3 1 2 -> 1 1 2 3 2 -> 1 1 2 2 3 Moves: - 1 - 2 - 1 = 4
Insertion Sort Advanced Analysis – Hacker Rank Solution
import java.io.*; import java.util.*; public class Solution { private static final int MAXVAL = 10000000; private static int[] array = new int[MAXVAL+1]; public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); int testCaseCount = sc.nextInt(); for (int i = 0; i < testCaseCount; i++) { int size = sc.nextInt(); long sum = 0; Arrays.fill(array, 0); for (int j = 0; j < size; j++) { sum += assign(sc.nextInt(), array, j); } System.out.println(sum); } } private static int assign(int x, int[] prefixSums, int current) { int n = read(prefixSums, x); update(prefixSums, x); return current-n; } private static int read(int[] prefixSums, int x) { int nrt=0; while(x>0) { nrt += prefixSums[x]; x -= (x&(-x)); } return nrt; } private static void update(int[] prefixSums, int x) { while(x <= MAXVAL) { prefixSums[x]++; x += (x&(-x)); } } }