Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C++ Program to find maximum possible smallest time gap between two pair of clock readings
Suppose we have an array D with N elements. Consider in a code festival there are N+1 participants including Amal. Amal checked and found that the time gap between the local times in his city and the i-th person's city was D[i] hours. The time gap between two cities: For any two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is minimum of d and 24−d hours.
Here, we are using 24-hour notation. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. We have to find the maximum possible value of s.
So, if the input is like D = [7, 12, 8], then the output will be 4, because the time gap between the second and third persons' cities is 4 hours.
Steps
To solve this, we will follow these steps −
n := size of D sort the array D Define an array t insert 0 at the end of t for initialize i := 0, when iExample
Let us see the following implementation to get better understanding −
#includeusing namespace std; int solve(vector D) { int n = D.size(); sort(D.begin(), D.end()); vector t; t.push_back(0); for (int i = 0; i D = { 7, 12, 8 }; cout Input
{ 7, 12, 8 }Output
4
