Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given four digits, find the maximum valid time that can be displayed on a digita

ID: 3828746 • Letter: G

Question

Given four digits, find the maximum valid time that can be displayed on a digital clock (in 24-hour format) using those digits in C++. 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 MaxTime should return "23:18". Given 2, 4, 0, 0 the function 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. Try to achieve O(n) if possible rather than O(n^2) solution.

Explanation / Answer

code:

#include<cstdio>
#include<string.h>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

int formdig(int a,int b,int c,int d)
{
   int res;
   res = a*1000+b*100+c*10+d;
   return res;
}
int getsort(int n)
{
   vector<int> in;
   while(n!=0)
   {
       in.push_back(n%10);
       n=n/10;
   }
   sort(in.begin(),in.end(), std::greater<int>());
   return in[0]*1000 + in[1]*100+ in[2]*10+in[3];
}
bool isvalid(int n)
{
   int last2=0;
   last2= last2+ n%10;
   n = n/10;
   last2= last2 + (n%10)*10;
   if(last2>60)
       return false;
   return true;
}
int maxtime(int a,int b,int c,int d)
{
   vector<int> input;
   input.push_back(a);
   input.push_back(b);
   input.push_back(c);
   input.push_back(d);
   sort(input.begin(),input.end(), std::greater<int>());
   int i=2400;
   for(i=2400;i>=0;i--)
   {

if (formdig(input[0],input[1],input[2],input[3])==getsort(i)&& isvalid(i))
    return i;
   }
   cout<<"not possible";
   return -1;
}
int main ( int argc, char *argv[] )
{
cout<<maxtime(3,0,7,0);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote