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 Ikshu and his machine gun problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Ikshu and his machine gun problem solution To celebrate his new year Ikshu went to play “beyond to beyond” (abbreviated as BOB) with his friends. BOB can be described as follows :
  1. It consists of Infinite boxes arranged one over another, Lowest box being 1.
  2. Given a list ‘L’ of indices, Ikshu needs to knockoff L[i]th box for 0<=i<|L| where |L| denoted the size of L.
  3. A box gun is used which can knock off a box at a given index. For example : you can give command to box gun to knock off box at position 2.
  4. The command given to the machine should be in increasing order.
NOTE: After knocking off a particular box, all the boxes above it come down by one unit and you can knockoff boxes in any order.
 
You need to find the commands that Ikshu should give to his machine guns in order to knock off all the boxes at given indices.
 
 
HackerEarth Ikshu and his machine gun problem solution

 

 

HackerEarth Ikshu and his machine gun problem solution.

#include <bits/stdc++.h>
using namespace std;

int main(){
ios_base::sync_with_stdio(false);
int t;
cin >> t;
vector<int> val(t);
for(int i=0;i<t;i++){
cin >> val[i];
}
sort(val.begin(), val.end());
int sub = 0;
for(int i=0;i<t;i++){
val[i] -= sub;
cout << val[i] << " ";
sub++;
}
cout << endl;
}
 
 

Second solution

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<functional>
#include<numeric>
using namespace std;
#define FOR(i,a,b) for(i= a ; i < b ; ++i)
#define rep(i,n) FOR(i,0,n)
#define si(n) scanf("%d",&n)
int arr[1000006];
int main()
{
int n,i;
si(n);
rep(i,n)
si(arr[i]);
sort(arr,arr+n);
rep(i,n)
{
printf("%d",arr[i]-i);
if(i!=n-1)
printf(" ");
else
printf("n");
}
return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

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