Friday 26 October 2018

Java program for building maximum possible time from 9 digit (TCS codevita question )

This is solution for TCS codevita practice round question, According to the question we have to get 9 digit input from user and for a maximum possible time ,if possible.


You must aware of basic concept of loop,array,e.t.c




import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        String str=sc.nextLine();
        String ch;
        int arInput[]=new int[9];
        boolean checkUse[]={false,false,false,false,
                                   false,false,false,false,false};
        int arTime[]={0,0,0};
        int a,b,i=10,j=10;
        boolean bol=true,pos=false;    
        for(a=0,b=0;a<17;a=a+2,b++){
        ch=""+str.charAt(a);
        arInput[b]=Integer.parseInt(ch);
        }
        
    //for checking and genrating 
    //maximum possible value for hour
    for(a=0;a<9;a++){
        if(checkUse[a]!=true){
          for(b=0;b<9;b++){
           if(checkUse[b]!=true){
             if(arInput[a]*10+arInput[b]<24&&a!=b){
               if(arInput[a]*10+arInput[b]>arTime[0]){                       arTime[0]=arInput[a]*10+arInput[b];
                            pos=true;
                            i=a;
                            j=b;
                        }
                    }
                }
            }
        }
    }
    
    if(i<9)checkUse[i]=true;
    if(j<9)checkUse[j]=true;
    i=10;// where i and j are index
    j=10;  // of array used once
   
    if(pos==true){
    pos=false;
      //for checking and genrating maximum
      //possible value for minute
    for(a=0;a<9;a++){
       if(checkUse[a]!=true){
          for(b=0;b<9;b++){
              if(checkUse[b]!=true){
                 if(arInput[a]*10+arInput[b<60&&a!=b){
                        if(arInput[a]*10+arInput[b]>arTime[1]){

                            arTime[1]=arInput[a]*10+arInput[b];
                            pos=true;
                            i=a;
                            j=b;
                            
                        }
                    }
                }
            }
        }
    }
    }
    if(i<9)checkUse[i]=true;
    if(j<9)checkUse[j]=true;
    // where i and j are index of array used once
    i=10;
    j=10;
    
    if(pos==true){
      pos=false;
    //for checking and genrating maximum possible value for seconds
    for(a=0;a<9;a++){
        if(checkUse[a]!=true){
            for(b=0;b<9;b++){
            if(checkUse[b]!=true){
                if(arInput[a]*10+arInput[b]<60&&a!=b){
                    if(arInput[a]*10+arInput[b]>arTime[2]){
                        arTime[2]=arInput[a]*10+arInput[b];
                            pos=true;
                            i=a;
                            j=b;
                        }
                    }
                }
            }
        }
    }
    }
    if(pos==true){ 
        System.out.print(arTime[0]+":"+arTime[1]+":"+arTime[2]);

    }
    else{
        System.out.print("Impossible");
    }

    }
    

}

No comments:

Post a Comment