Takeoff

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 take off on every qth day and plane C will take off on every rth day. There is only one runway and the takeoff timing is the 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.

Note: If there is a collision between the flights no flight will take off on that

Example:

Input:  n = 10, p = 2, q = 3, r = 4
Output: 4

Approach

C++

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

int takeOff(int n, int p, int q, int r)
{
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        if (i % p == 0 && i % q != 0 && i % r != 0)
            cnt++;
        else if (i % p != 0 && i % q == 0 && i % r != 0)
            cnt++;
        else if (i % p != 0 && i % q != 0 && i % r == 0)
            cnt++;
    }
    return cnt;
}
int main()
{

    int n = 10, p = 2, q = 3, r = 4;

    cout << takeOff(n, p, q, r) << "\n";

    return 0;
}


No comments:

Post a Comment