C++ code to count order collected when client calls

Suppose we have three numbers n, m and z. An office receives calls in every n minutes, and some deliveries come to office in every m minutes. Office is open for z minutes. We have to count the minimum number of orders are collected so there are no pending orders when client calls. Consider taking orders and talking with clients take exactly 1 minutes.

So, if the input is like n = 1; m = 2; z = 5, then the output will be 2, because we need to collect orders which comes in second and fourth minutes.


To solve this, we will follow these steps −

return z / ((n * m) / (gcd of n and m))


Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int n, int m, int z){
   return z / ((n * m) / __gcd(n, m));
int main(){
   int n = 1;
   int m = 2;
   int z = 5;
   cout << solve(n, m, z) << endl;


1, 2, 5