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 Counter Strike problem solution

YASH PAL, 31 July 2024
In this HackerEarth Shil and Survival Game problem solution Navi is a counterstrike pro. He always says how good he is at counterstrike. After being tired of Navi, his friends decided to test his skills at shooting. They put M targets on an X – Y plane, each target is denoted by (X, Y) where X is x-coordinate and Y is y-coordinate. His friends also gave him N locations on the X – Y plane from where Navi can shoot the targets. Navi knows that he can shoot a target if the Manhattan distance between his location and target is ≤ D. If Navi can shoot more than half of the targets (for odd values of M check only for an integral part of half of M, say M = 3,2/3, 2/3 = 1 ) only then his friends believe that he is a pro at counter strike otherwise he is not.
HackerEarth Counter Strike problem solution

HackerEarth Counter-Strike problem solution.

#include <bits/stdc++.h>

using namespace std;

int abss(int x)
{
if (x < 0) return -x;
return x;
}

struct point {
int x,y;
} targets[1001], pos[1001];

int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n, m, i, j, ctr, d;
ctr = 0;
scanf("%d%d%d", &n, &m, &d);
for(i = 0; i < n; i++) {
scanf("%d%d", &pos[i].x, &pos[i].y);
}
for(i = 0; i < m; i++) {
scanf("%d%d", &targets[i].x, &targets[i].y);
}
for(j = 0; j < m; j++) {
for(i = 0; i < n; i++) {
if(abss(pos[i].x - targets[j].x) + abss(pos[i].y - targets[j].y) <= d) {
ctr++;
break; // so that a particular target will be counted once
}

}
}
if(ctr > (m)/2) printf("YESn");
else printf("NOn");
}
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std ;
#define LL long long int
#define ft first
#define sd second
#define PII pair<int,int>
#define MAXN 1000001
#define mp make_pair
#define f_in(st) freopen(st,"r",stdin)
#define f_out(st) freopen(st,"w",stdout)
#define sc(x) scanf("%d",&x)
#define pr(x) printf("%lldn",x)

int N,M,D,T;
vector<PII> target , position ;
vector<bool> V;

int dist(PII &a,PII &b){
return (abs(a.ft - b.ft) + abs(a.sd - b.sd)) ;
}

bool check(PII &a,PII &b){
return (dist(a,b) <= D) ;
}
int main(){
sc(T) ;
assert(T <= 6 && T >= 1) ;
while(T--){
sc(N) , sc(M) , sc(D) ;
assert(N <= 1000 && N >= 1) ;
assert(M <= 1000 && M >= 1) ;
assert(D <= 50000 && D >= 1) ;
position.resize(N+1) ;
target.resize(M+1) ;
V.resize(M+1) ;
fill(V.begin(),V.end(),0) ;
for(int i=1;i<=N;i++){
sc(position[i].ft) ;
sc(position[i].sd) ;
assert(position[i].ft <= 50000 && position[i].ft >= -50000) ;
assert(position[i].sd <= 50000 && position[i].sd >= -50000) ;
}
for(int i=1;i<=M;i++){
sc(target[i].ft) ;
sc(target[i].sd) ;
assert(target[i].ft <= 50000 && target[i].ft >= -50000) ;
assert(target[i].sd <= 50000 && target[i].sd >= -50000) ;
}
bool ok = 0 ;
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
if(check(position[i],target[j]))
V[j] = 1 ;
}
}
int cnt = 0 ;
for(int i=1;i<=M;i++){
if(V[i]) cnt ++ ;
}
if(cnt > M/2)
ok = 1 ;
puts(ok ? "YES" : "NO") ;
}
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