Header Ad

HackerEarth Ikshu and his machine gun problem solution

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;
}

Post a Comment

0 Comments