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 Takeoff problem solution

YASH PAL, 31 July 2024
In this HackerEarth Takeoff problem solution, There are three planes A, B, and C. Plane A will takeoff on every pth day i.e. p, 2p, 3p and so on. Plane B will takeoff on every qth day and plane C will takeoff on every rth day. There is only one runway and the takeoff timing is same for each of the three planes on each day. Your task is to find out the maximum number of flights that will successfully takeoff in the period of N days.
HackerEarth Takeoff problem solution

HackerEarth Takeoff problem solution.

#include <bits/stdc++.h>

using namespace std;

const int N = 1E5 + 5;
int f[N];

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while(t --) {
int n, p, q, r;
cin >> n >> p >> q >> r;
memset(f, 0, sizeof f);
for(int i = p; i <= n; i += p)
f[i] ++;
for(int i = q; i <= n; i += q)
f[i] ++;
for(int i = r; i <= n; i += r)
f[i] ++;
int cnt = 0;
for(int i = 1; i <= n; i ++)
if(f[i] == 1)
cnt ++;
cout << cnt << 'n';
}
return 0;
}

Second solution

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "n"
#define eps 0.00000001
LL pow(LL a,LL b,LL m){LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
int t;
cin >> t;
assert(t >= 1 && t <= 10);
while(t--){
int n , p , q , r;
cin >> n >> p >> q >> r;
assert(n >= 1 && n <= 100000);
assert(p >= 1 && p <= 100000);
assert(q >= 1 && q <= 100000);
assert(r >= 1 && r <= 100000);
int idx = p , ans = 0;;
while(idx <= n){
if(idx % q != 0 && idx % r != 0){
++ans;
}
idx += p;
}
idx = q;
while(idx <= n){
if(idx % p != 0 && idx % r != 0){
++ans;
}
idx += q;
}
idx = r;
while(idx <= n){
if(idx % q != 0 && idx % p != 0){
++ans;
}
idx += r;
}
cout << ans << endl;
}
}
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