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

HackerEarth Swap It problem solution

YASH PAL, 31 July 202416 February 2026
In this HackerEarth Swap, It problem solution Bob loves sorting very much. He is always thinking of new ways to sort an array.His friend Ram gives him a challenging task.He gives Bob an array and an integer K.
 
The challenge is to produce the lexicographical minimal array after at most K-swaps.Only consecutive pairs of elements can be swapped.Help Bob in returning the lexicographical minimal array possible after at most K-swaps.
 
 
HackerEarth Swap It problem solution

 

 

HackerEarth Swap It problem solution.

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define all(a) a.begin(),a.end()
#define bitcnt(x) __builtin_popcountll(x)
#define MOD 5000000007
#define total 500005
#define M 1000000007
#define NIL 0
#define INF (1<<28)
typedef unsigned long long int uint64;
typedef long long int int64;

int main(){
freopen("input1.txt","r",stdin);
freopen("output.txt","w",stdout);
int t,n,i,k;
cin>>t;
while(t--){
cin>>n>>k;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++){
int pos=i;
for(int j=i+1;j<n;j++){
if((j-i)>k)
break;
if(a[j]<=a[pos])
pos=j;
}
for(int j=pos;j>i;j--)
a[j]=(a[j]+a[j-1])-(a[j-1]=a[j]);
k-=(pos-i);
}
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
fclose(stdout);
return 0;
}
 

Second solution

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<cassert>
#include<set>
#include<queue>
#include<map>

using namespace std;

#define vi vector < int >
#define pb push_back
#define mp make_pair
#define ll long long
#define llu unsigned long long
#define MOD 1000000007
#define INF 1000000000
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define all(x) x.begin(),x.end()

int a[1003];

int main()
{
int t,n,k,i,j;
scanf("%d",&t);
assert(1<=t && t<=10);
while(t--)
{
scanf("%d%d",&n,&k);
assert(1<=n && n<=1000);
assert(1<=k && k<=1000);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
assert(1<= a[i] && a[i] <= 1000000);
}
j = 0;
while(k>0 || j<n)
{
int mn = INF,id;
for(i=j;i<n;i++)
{
if(i-j <= k && mn > a[i])
mn = a[i] , id = i;
}
for(int x=id;x-1>=j;x--)
{
swap(a[x],a[x-1]);
}
k -= (id-j);
j++;
}
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("n");
}
return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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