Takeoff

There are three planes AB, and Plane will takeoff on every 

pth day i.e. p2p3p 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 nint pint qint r)
{
    int cnt = 0;
    for (int i = 1i <= ni++)
    {
        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 = 10p = 2q = 3r = 4;

    cout << takeOff(npqr<< "\n";

    return 0;
}


No comments:

Post a Comment