Using any preferred language create a method which will read the strArr parameter being passed which will represent a full day and will be filled with events that span from time X to time Y In the day. The format of each event will be hh:mm AM/PM-hh:mm AM/PM.
For example, strArr may be ["10:00AM-12:30PM","02:00PM-02:45PM","09:1OAM- 09:50AM")
Your program will have to output the longest amount of free time available between the start of your first event and the end of your last event in the format: hh:mm. The start event should be the earliest event in the day and the latest event should be thelatest event in the day
The output for the previous input would therefore be 01:30 (with the earliest event in the day starting at 09:10AM and the latest event ending at 02:45PM). The input will contain at least 3 events and the events may be out of order.
Answers
Program to output the longest amount of free time available between the start of your first event and the end of your last event.
The below program is in C++ language.
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
#include <sstream.h>
using namespace std;
// Function to determine the time difference between the events
double timeDifference(string event1, string event2, string time1, string time2)
{
double liveTime, upcomingTime;
event1[2] = event2[2] = '.';
istringstream(event1) >> liveTime;
istringstream(event2) >> upcomingTime;
if (event1.substr(event1.length() - 2) != "00" && event2.substr(event2.length() - 2) == "00")
{
upcomingTime+= 0.60;
upcomingTime-= 1;
}
// Calculating the difference in time
if (time1 == time2 && (event1.substr(0, 2) == "12" && event2.substr(0, 2) != "12"))
{
return (upcomingTime+ 12.00) - liveTime;
}
else if (time1 == time2)
{
return upcomingTime- liveTime;
}
else
{
return (upcomingTime+ 12.00) - liveTime;
}
}
string LongestFreeTime(string strArr[], int size) {
int index;
string live1, live2, live3, temp1, temp2, temp3;
bool swap;
// Loop to arrange the events
do
{
swap = false;
for (int x = 0; x < size-1; x++)
{
live1= strArr[x].substr(0, 2); // current hour
live2= strArr[x].substr(5, 2); // current time frame
live3= strArr[x].substr(3, 2); // current minute
temp1= strArr[x+1].substr(0, 2); // next hour
temp2 = strArr[x+1].substr(5, 2); // next time frame
temp3 = strArr[x + 1].substr(3, 2); // next minute
// Conditions to compare time if early or late in the day
if (live1 == temp1 && live2 == temp2 && live3> temp3)
{
temp1 = strArr[x];
strArr[x] = strArr[x + 1];
strArr[x + 1] = temp1;
swap = true;
}
if ((temp1 < live1 && live1 != "12") && ((live2 == "AM" && temp2 == "AM") || (live2 == "PM" && temp2 == "PM")))
{
temp1= strArr[x];
strArr[x] = strArr[x + 1];
strArr[x + 1] = temp1;
swap = true;
}
else if ((temp1> live1 && temp1 == "12") && ((live2 == "AM" && temp2 == "AM") || (live2 == "PM" && temp2 == "PM")))
{
temp1 = strArr[x];
strArr[x] = strArr[x + 1];
strArr[x + 1] = temp1;
swap = true;
}
else if ((temp1 < live1 || temp1 > live1 ) && live2 == "PM" && temp2 == "AM")
{
temp1 = strArr[x];
strArr[x] = strArr[x + 1];
strArr[x + 1] = temp1;
swap = true;
}
}
} while (swap);
double value;
double highest = 0;
// Loop to find the highest free time
for (int y = 0; y < size-1; y++)
{
value = timeDifference(strArr[y].substr(8, 5), strArr[y + 1].substr(0, 5), strArr[y].substr(strArr[y].length() - 2), strArr[y+1].substr(5,2));
if (value > highest)
{
highest = value;
}
}
// Convert the highest difference value to a string
stringstream convert;
convert << fixed << setprecision(2) << highest;
string freeTime = convert.str();
if (freeTime.length() != 5)
{
freeTime.insert(0, "0");
}
freeTime[2] = ':';
return freeTime;
}
int main() {
string A[] = { "12:15PM-02:00PM", "09:00AM-10:00AM", "10:30AM-12:00PM" };
string B[] = { "12:15PM-02:00PM", "09:00AM-12:11PM", "02:02PM-04:00PM" };
string C[] = { "10:00AM-12:30PM", "02:00PM-02:45PM", "09:10AM-09:50AM" };
string D[] = { "06:00AM-08:00PM", "09:09PM-09:11PM", "08:10PM-09:00PM", "08:02PM-08:04PM" };
cout << LongestFreeTime(A, sizeof(A) / sizeof(A[0])) << endl;
cout << LongestFreeTime(B, sizeof(B) / sizeof(B[0])) << endl;
cout << LongestFreeTime(C, sizeof(C) / sizeof(C[0])) << endl;
cout << LongestFreeTime(D, sizeof(D) / sizeof(D[0])) << endl;
return 0;
}
#SPJ3