Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Insertion Sort Advanced Analysis problem solution

YASH PAL, 31 July 202423 January 2026

HackerRank Insertion Sort Advanced Analysis problem solution – In this HackerRank Insertion Sort Advanced Analysis problem we have given an array and we need to calculate the number of shifts an insertion sort performs when sorting an array.

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?

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

HackerRank Insertion Sort Advanced Analysis problem solution

HackerRank Insertion Sort Advanced Analysis problem solution in Python.

from array import array
from bisect import bisect_right

t = int(input())
for _ in range(t):
    n = int(input())
    arr = array('I', [int(i) for i in input().split()])
    sarr = array('I', [arr[0]])
    result = 0
    for i in range(1, n):
        e = arr[i]
        j = bisect_right(sarr, e)       
        sarr.insert(j, e)
        result += i - j
    print(result)            

Insertion Sort Advanced Analysis problem solution in Java.

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

Problem solution in C++ programming.

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std ;
#define MAXN 100002
#define MAX 1000002
int n,a[MAXN],c[MAX] ;
int main()
{
 int runs ;
 scanf("%d",&runs) ;
 while(runs--)
 {
  scanf("%d",&n) ;
  for(int i = 0;i < n;i++) scanf("%d",&a[i]) ;
  long long ret = 1LL * n * (n - 1) / 2 ;
  memset(c,0,sizeof c) ;
  for(int i = 0;i < n;i++)
  {
   for(int j = a[i];j > 0;j -= j & -j) ret -= c[j] ;
   for(int j = a[i];j < MAX;j += j & -j) c[j]++ ;  
  }
  cout << ret << endl ;
 }
 return 0 ;
}

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
long long inv(int *A, int N)
{
	if(N <= 1) return 0;
	int m = N/2;
	long long ans = inv(A, m) + inv(A + m, N - m);
	static int tmp[100000];
	int left = 0, right = m, i;
	for(i = 0; i < N; i ++)
	{
		int min = 1000001;
		if(m - left > 0 && min > A[left])
			min = A[left++];
		if(N - right > 0 && min > A[right])
		{
			if(min != 1000001) left --;
			min = A[right++];
			ans += m - left;
		}
		tmp[i] = min;
	}
	memcpy(A, tmp, sizeof(int)*N);
	return ans;
}
void solve()
{
	int N;
	static int A[1000005];
	scanf("%d",&N);
	int i;
	for(i = 0; i < N; i ++) scanf("%d",A + i);
	printf("%lldn",inv(A,N));
}
int main()
{
	int K;
	scanf("%d",&K);
	while(K--) solve();
	
	return 0;
}
Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes