

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to count how many minutes we have to wait to meet at least one swimmer
Suppose we have four numbers p, a, b and c. There is a pool and three swimmers are there. They take a, b and c minutes to cross the pool and come back, respectively. So the first swimmer will be at the left side of pool after 0, a, 2a, 3a,... minutes after the start time. The second will be at 0, b, 2b, 3b,... minutes and for the third one 0, c, 2c, 3c, ... If we visit the pool after p minutes they have started swimming we have to find how much time we have to wait at minimum to get at least one of the swimmers at the left side of pool.
So, if the input is like p = 2; a = 6; b = 10; c = 9, then the output will be because at 2 we are near the pool, the first swimmer will come back at 6 so we have to wait 4 units of time.
steps
To solve this, we will follow these steps −
(decrease p by 1) return minimum of (a - (p mod a + 1)), (b - (p mod b + 1)) and (c - (p mod c + 1))
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int p, int a, int b, int c) { p--; return min(a - (p % a + 1), min(b - (p % b + 1), c - (p % c + 1))); } int main() { int p = 2; int a = 6; int b = 10; int c = 9; cout << solve(p, a, b, c) << endl; }
Input
2, 6, 10, 9
Output
4
- Related Questions & Answers
- C++ program to count at least how many operations needed for given conditions
- C# program to check if two lists have at-least one element common
- Python program to check if two lists have at least one common element
- Count divisors of n that have at-least one digit common with n in Java
- How many destructors can we have in one class in C#?
- C++ program to check how many students have greater score than first one
- C++ program to count minimum how many minutes after there will be no new angry students
- Program to find k where k elements have value at least k in Python
- One-to-Many or Many-to-One Relationship in DBMS
- How to remove rows that contain at least one 0 in R?
- How to extract columns having at least one non-duplicate in R?
- Program to find elements from list which have occurred at least k times in Python
- Java regex program to verify whether a String contains at least one alphanumeric character.
- Convert inputs to arrays with at least one dimension in Numpy
- How to aggregate two lists if at least one element matches in MongoDB?