1. Given four digits, find the maximum valid time that can be displayed on a dig
ID: 3836445 • Letter: 1
Question
1. Given four digits, find the maximum valid time that can be displayed on a digital clock (in 24-hour format) using those digits. For example, given digits 1,8,3,2 the maximum valid time is "23:18". Note that "28:31" is not a valid time. Write a function: string MaxTime(int A, int B, int C, int D); that, given four integers A,B,C,D, returns the maximum valid time in string format "HH MM" or "NOT POSSIBLE" if it is not possible to display a valid time. Examples: given 1,8,3,2, the function Max Time shoud return "23:18". Given 2,4,0,0 the funtion should return "20:40" Given 3,0,7,0 the function should return "07:30" Given 9,1,9,7 the function should return "NOT POSSIBLE". Since there is no possible valid time. Assume that: A,B,C, D are integers with in the range 0..9] In your solution, focus on correctness as well as the performance of your solution. Tryto achieve o(n) if possible rather than O(nn2) solution.Explanation / Answer
Hi,
Here is the updated program :
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <stdio.h>
using namespace std;
void maxTime()
{
int a[4];
bool valid[4] = { true, true,true,true };
int hour = 0;
int minute = 0;
bool success = false;
cin >> a[0] >> a[1] >> a[2] >> a[3];
for (int h = 23; h > 0; h--)
{
for (int i = 0; i < 4; i++)
{
if (h / 10 == a[i] && valid[i])
{
valid[i] = false;
for (int j = 0; j < 4; j++)
{
if (h % 10 == a[j] && valid[j])
{
valid[j] = false;
hour = a[i] * 10 + a[j];
success = true;
break;
}
}
if (success != true)
{
valid[i] = true;
}
else
{
break;
}
}
}
if (success == true)
{
success = false;
break;
}
}
for (int m = 60; m > 0; m--)
{
for (int i = 0; i < 4; i++)
{
if (m / 10 == a[i] && valid[i])
{
valid[i] = false;
for (int j = 0; j < 4; j++)
{
if (m % 10 == a[j] && valid[j])
{
valid[j] = false;
minute = a[i] * 10 + a[j];
success = true;
break;
}
}
if (success != true)
{
valid[i] = true;
}
else
{
break;
}
}
}
if (success == true)
{
success = false;
break;
}
}
if ((valid[0] || valid[1] || valid[2] || valid[3]) != false)
{
cout << "NOT POSSIBLE";
}
else
{
cout << hour << ":" << minute << endl;
}
return;
}
void convertDate()
{
char inDay[4];
char inMonth[3];
char inYear[4];
int day, month, year;
char date[15];
char days[2];
cout << "Enter day, month and year" <<endl;
cin >> inDay >> inMonth >> inYear;
map<string, string> month_map;
month_map["Jan"] = "01";
month_map["Feb"] = "02";
month_map["Mar"] = "03";
month_map["Apr"] = "04";
month_map["May"] = "05";
month_map["Jun"] = "06";
month_map["Jul"] = "07";
month_map["Aug"] = "08";
month_map["Sep"] = "09";
month_map["Oct"] = "10";
month_map["Nov"] = "11";
month_map["Dec"] = "12";
day = atoi(inDay);
year = atoi(inYear);
int nDigits = floor(log10(abs(day))) + 1;
memset(date, 0, sizeof(date) );
strcat(date, (char *)(inYear) );
strcat(date, "-");
strcat(date, month_map[inMonth].c_str());
strcat(date, "-");
sprintf(days, "%d", day );
if (nDigits == 1) {
strcat(date, "0");
strcat(date, (char *)days );
}
else {
strcat(date, (char *)days );
}
cout<<date<<endl;
return;
}
int main()
{
while (1)
{
cout<<"1) Find maximum time. 2) Convert date. 3) Exit.";
int a;
cout<<" Enter your choice : ";
cout<<endl;
cin >> a;
switch(a) {
case 1:
maxTime();
break;
case 2:
convertDate();
break;
case 3:
exit(0);
break;
default:
cout<<" Enter the correct option " ;
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.