Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
Given a set of nine single (not necessarily distinct) digits, say 0, 0, 1, 3, 4, 6, 7, 8, 9, it is possible to form many distinct times in a 12 hour time format HH:MM:SS, such as 10:36:40 or 01:39:46 by using each of the digits only once. The objective is to find the maximum possible valid time (00:00:01 to 12:00:00) that can be formed using some six of the nine digits exactly once. In this case, it is 10:49:38.

What I have tried:

A line consisting of a sequence of 9 (not necessarily distinct) single digits (any of 0-9) separated by commas. The sequence will be non-decreasing
Posted
Updated 29-Mar-21 18:19pm
Comments
RickZeeland 22-Jul-17 1:22am    
Tip: write a function e.g. Between(min, max) to test each part of the time (hours, minutes, seconds) separately.

Team with your classmate Write a C program to form the maximum possible time in the HH:MM:SS format using any six of nine given single digits (not necessarily distinct)[^]

Take a sheet of paper and a pencil. Use your brain and solve the problem by hand. Since Harry Potter didn't gave you the solution, there is no magic, and the solution didn't jump to your face.
You solved the problem by following a procedure, that procedure is your algorithm. You need to write down the steps in a mechanical manner (computer like). The program will follow those steps.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
 
Share this answer
 
v2
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
This is from an exam conducted by Tata consultancy services. CodeVita and this question is the first question there if I'm not wrong. Written the exam and i solved this taking the digits differently. H1 H2 : M1 M2 : S1 S2. This is a hint for you. Other than that you need to push your brain now!
 
Share this answer
 
C++
#include<stdio.h>
main()
{
	int a[100],i,temp[100],HMS[6],x,z;
	//printf("enter:");
	for(i=0;i<9;i++)		
	{
	scanf("%1d,",&a[i]);
	temp[i]=0;
	}
	for(i=0;i<=9;i++)	
	{
		if(a[i]==0)
			temp[0]++;
		else if(a[i]==1)
			temp[1]++;
		else if(a[i]==2)
			temp[2]++;
		else if(a[i]==3)
			temp[3]++;
		else if(a[i]==4)
			temp[4]++;
		else if(a[i]==5)
			temp[5]++;
		else if(a[i]==6)
			temp[6]++;
		else if(a[i]==7)
			temp[7]++;
		else if(a[i]==8)
			temp[8]++;
		else if(a[i]==9)
			temp[9]++;
	}
	if(temp[0]>0||temp[1]>0)	
	{
		if(temp[1]==0)			
		{
			HMS[0]=0;		
			temp[0]--;		
			for(x=9;x>0;x--)
			{						
				if(temp[x]!=0)
				{
					HMS[1]=x;
					temp[x]--;
					break;
				}
			}
				
		}
		else
		{
			HMS[0]=1;
			temp[1]--;
			for(x=2;x>0;x--)
			{
				if(temp[x]!=0)
				{
					HMS[1]=x;
					temp[x]--;
					break;
				}
			}
		}
		if(HMS[0]==1&&HMS[1]!=2)
		{
			for(x=5;x>0;x--)
				{
					if(temp[x]!=0)
					{
						HMS[2]=x;
						temp[x]--;
						break;
					}
				}
			for(x=9;x>0;x--)
				{
					if(temp[x]!=0)
					{
						HMS[3]=x;
						temp[x]--;
						break;
					}
				}
			for(x=5;x>0;x--)
				{
					if(temp[x]!=0)
					{
						HMS[4]=x;
						temp[x]--;
						break;
					}
				}
			for(x=9;x>0;x--)
				{
					if(temp[x]!=0)
					{
						HMS[5]=x;
						temp[x]--;
						break;
					}
				}
		}
		else	
		{
			if(temp[0]==4)
			HMS[2]=HMS[3]=HMS[4]=HMS[5]=0;
			else
			goto z;
		}
		printf("%d%d:%d%d:%d%d",HMS[0],HMS[1],HMS[2],HMS[3],HMS[4],HMS[5]);
	}
	else 
	z:	printf("\nimpossible\n");
	
}
 
Share this answer
 
v3
Comments
jeron1 25-Jul-17 10:05am    
If you believe that you are helping, you would be wrong.
For minimum time, the trick is to find all the permutations of the given string of 6 chars in a lexicographical order and perform check on them starting from the lexicographically shortest one(reverse in case of maximum time). Here is the working code.

Code link.
 
Share this answer
 
v4
Just call the run method of timemachine class

C#
using System;
using System.Linq;


    public class amaz
    {
        public string run(int a, int b, int c, int d, int e, int f)
        {
            if (a > 9 || b > 9 || c > 9 || d > 9 || e > 9 || f > 9)
            {
                return "Impossible";
            }
            int[] input = { a,b,c,d,e,f};
            return GetTime(input);
        }
        private string GetTime(int[] input)
        {
            int numIndex = -1;
            if (!validateHour(input))
                return "Impossible";
            //set H1
            int h1 = 0;
            int h2 = 0;
            if (input.Contains(1) && input.Contains(2)) {
                h1 = 1;
                numIndex = Array.IndexOf(input, h1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();

                h2 = 2;
                numIndex = Array.IndexOf(input, h2);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(0))
            {
                h1 = 0;
                numIndex = Array.IndexOf(input, h1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();

                h2 = input.First();
                numIndex = Array.IndexOf(input, h2);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(1))
            {
                h1 = 1;
                numIndex = Array.IndexOf(input, h1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();

                h2 = input.First();
                numIndex = Array.IndexOf(input, h2);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(2))
            {
                h1 = 2;
                numIndex = Array.IndexOf(input, h1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
                if (input.Contains(0))
                {
                    h2 = 0;
                    numIndex = Array.IndexOf(input, h2);
                    input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
                }
                else if (input.Contains(1))
                {
                    h2 = 1;
                    numIndex = Array.IndexOf(input, h2);
                    input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
                }
                else if (input.Contains(2))
                {
                    h2 = 2;
                    numIndex = Array.IndexOf(input, h2);
                    input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
                }
                else if (input.Contains(3))
                {
                    h2 = 3;
                    numIndex = Array.IndexOf(input, h2);
                    input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
                }

            }

            //set M1
            if (!validateMinAndSec(input))
                return "Impossible";
            int m1 = 0;
            if (input.Contains(0))
            {
                m1 = 0;
                numIndex = Array.IndexOf(input, m1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(1))
            {
                m1 = 1;
                numIndex = Array.IndexOf(input, m1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(2))
            {
                m1 = 2;
                numIndex = Array.IndexOf(input, m1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(3))
            {
                m1 = 3;
                numIndex = Array.IndexOf(input, m1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(4))
            {
                m1 = 4;
                numIndex = Array.IndexOf(input, m1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(5))
            {
                m1 = 5;
                numIndex = Array.IndexOf(input, m1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }


            int m2 = 0;
            m2 = input.First();
            numIndex = Array.IndexOf(input, m2);
            input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();

            int s1 = 0;
            if (!validateMinAndSec(input))
                return "Impossible";
            if (input.Contains(0))
            {
                s1 = 0;
                numIndex = Array.IndexOf(input, s1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(1))
            {
                s1 = 1;
                numIndex = Array.IndexOf(input, s1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(2))
            {
                s1 = 2;
                numIndex = Array.IndexOf(input, s1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(3))
            {
                s1 = 3;
                numIndex = Array.IndexOf(input, s1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(4))
            {
                s1 = 4;
                numIndex = Array.IndexOf(input, s1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }
            else if (input.Contains(5))
            {
                s1 = 5;
                numIndex = Array.IndexOf(input, s1);
                input = input.Where((val, idx) => idx != numIndex).OrderBy(x => x).ToArray();
            }

            int s2 = 0;
            s2 = input.First();

            return h1.ToString() + h2.ToString() + ":" + m1.ToString() + m2.ToString() + ":" + s1.ToString() + s2.ToString();
        }
        private bool validateHour(int[] input)
        {
            if (input.Contains(1) || input.Contains(0)|| input.Contains(2))
                return true;
            return false;
        }
        private bool validateMinAndSec(int[] input)
        {
            if (input.Contains(0) || input.Contains(1) || input.Contains(2) || input.Contains(3) || input.Contains(4) || input.Contains(5))
                return true;
            return false;
        }
      
    }
 
Share this answer
 
Comments
Patrice T 1-Jul-18 8:43am    
This was 1 year hold HomeWork, not sure someone still waiting for the answer.
Try to answer actual questions instead.
On first read, I am not sure your code will succeed with 0, 1, 2, 7, 8, 9
#include<stdio.h>
int main()
{
int a[20],i,h1,h2,m1,m2,s1,s2;
for(i=0;i<10;i++)
scanf("%d\n",&a[i]);
for(i=0;i<10;i++)
while(a[i]>=1&&a[i]<=9){
m2=a[i];
break;
}
for(i=0;i<9;i++)
while(a[i]>=1&&a[i]<=9){
s2=a[i];
break;
}
for(i=0;i<8;i++)
while(a[i]>=1&&a[i]<=5){
m1=a[i];
break;
}
for(i=0;i<7;i++)
while(a[i]>=m1){
a[i]=a[i+1];
break;
}
for(i=0;i<6;i++)
while(a[i]>=1&&a[i]<=5){
s1=a[i];
break;
}
for(i=0;i<5;i++)
while(a[i]>=s1){
a[i]=a[i+1];
break;
}

for(i=0;i<4;i++)
while(a[i]>=0&&a[i]<=2){
h2=a[i];
break;
}
for(i=0;i<3;i++)
while(a[i]==h2){
a[i]=a[i+1];
break;
}
for(i=0;i<2;i++)
while(a[i]==0||a[i]==1){
h1=a[i];
break;
}
if(h1>=2||a[0]>=a[5]||a[1]>=a[6]||a[2]>=a[7]||a[3]>=a[8]||a[4]>=a[9])
printf("impossible");
else
printf("%d%d:%d%d:%d%d",h1,h2,m1,m2,s1,s2);
return 0;
}
 
Share this answer
 
Comments
CHill60 16-Jul-18 9:35am    
I sincerely hope the OP isn't still waiting for someone to do their homework for them after an entire year.
You are not helping anyone by posting homework solutions.
Stick to answering new posts where the OP still needs help
Patrice T 16-Jul-18 10:11am    
This code is not even a valid answer.
<pre>private String solution(int i, int j, int k, int l, int m, int n) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(i);
		list.add(j);
		list.add(k);
		list.add(l);
		list.add(m);
		list.add(n);
		Collections.sort(list);

		// first two needs to form HH

		// hence HH = list [0] and list[1]

		if (list.get(3) > 5) { // place where 1 needs to be swaped with 3rd
			int temp = list.get(1);
			list.set(1, list.get(3));
			list.set(3, temp);
		}
		System.out.println(list);
		String HH = "" + list.get(0) + list.get(1);

		if (Integer.parseInt(HH) > 23) {
			return "NOT POSSIBLE";
		}
		// now check phisiblility of SS after MM
		if ((list.get(2) * 10 + list.get(3)) < 60 && (list.get(4) * 10 + list.get(5)) < 60) {

		} else if ((list.get(2) * 10 + list.get(3)) < 60 && (list.get(4) * 10 + list.get(5)) > 59) {
			// swap //3 and 4
			System.out.println(list);
			int temp = list.get(3);
			list.set(3, list.get(4));
			list.set(4, temp);
			System.out.println(list);
		} else {
			return "NOT POSSIBLE";
		}
		return HH + ":" + list.get(2) + list.get(3) + ":" + list.get(4) + list.get(5);

	}
 
Share this answer
 
Comments
Patrice T 11-Aug-18 16:47pm    
The question is 9 digits and C language !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900