Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
Programmingoneonone

HackerRank Insertion Sort Advanced Analysis problem solution

YASH PAL, 31 July 2024

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.

HackerRank Insertion Sort Advanced Analysis problem solution

Problem solution in Python programming.

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)            

Problem solution in Java programming.

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

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes