Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Ikshu and his machine gun problem solution

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

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes