Java Saved Program: kind-city

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


import java.util.Scanner;

class digital {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String numStr = in.next();
String nums[] = numStr.split(",");

int a[] = new int[10];
for(int i=0;i<nums.length;i++){
a[Integer.parseInt(nums[i])]++;
}


System.out.println(findTime(a));

}

private static String findTime(int[] a) {

int h1=-1,h2=-1,m1=-1,m2=-1,s1=-1,s2=-1;


h1=findNearest(a,1);
h2=findNearest(a,2);
if(h1==1&&h2==2)
return ifPoss(a);
m1=findNearest(a,5);
m2=findNearest(a,9);
s1=findNearest(a,5);
s2=findNearest(a,9);

if(h1==-1||h2==-1||s1==-1||s2==-1||m1==-1||m2==-1)
return "Impossible";
else
return ""+h1+h2+":"+m1+m2+":"+s1+s2;

}

private static String ifPoss(int[] a) {
if(a[0]==4) return "12:00:00";
else return "Impossible";
}

private static int findNearest(int[] a, int i) {
for(;i>=0;i--)
if(a[i]!=0){ 
a[i]--;
return i;
}
return -1;
}

}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs