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
Programmingoneonone
Programmingoneonone

HackerEarth Swap It problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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