Header Ads Widget

Help Jarvis!

 Problem:-

Tony Stark is in the planet Titan crying for his friends are turning into ashes, and on earth mayhem has ensued since a lot of people are turning into ashes too. Some trains have been derailed in such a way that a lot of its coaches are thrown off in a random disarray like coach 3, 4 and 5 are thrown off in one place, coach 2 and 6 are thrown off in another place, etc.

S.H.I.E.L.D calls upon Hulk and jarvis to help them collect and connect some the thrown off coaches of those trains, but a train can only move if the collected coaches number are in a continuous manner (need not to be in order) ,like 1234, 2314, 4123, 2341  etc.

Help Jarvis write a program for hulk to decide whether collected coaches will move or not.

Input Format:

First ilne contains one number , denoting the number of test cases.

Next t lines contain sequence of the collected coach numbers (n)

Output Format:

YES or NO (In capitals)

Input constraints:

1 <= t <= 10000

1<= n <= 1000000

Sample Input
6
2415
4231
4125
5142
4132
2143
Sample Output
NO
YES
NO
NO
YES
YES
Time Limit: 0.12
Memory Limit: 256
Source Limit:
Explanation

4231
4132
2143

For the above sample inputs the train will move because the combination of the coach numbers are in a continuous sequence.

Code:-

#include<stdio.h>
#include<stdlib.h>
void main()
{
    int t,a[10],i,r,j,flag=0;
    char n[10],s[2];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",n);
        flag=0;
        for( j=0;j<10;j++)
         a[j]=0;
         for(int i=0;n[i];i++)
         {
             s[0]=n[i];
             s[1]='\0';
             a[atoi(s)]++;
         }
    
     for(int j=0;j<=9;j++)
     {
         if(a[j]==1)
         {
             while(a[j]==1 && j<=9)
             {
                 j++;
             }
             for(i=j;i<=9;i++)
             {
                 if(a[i]>=1)
                 {
                 flag=1;
                 break;
                 }
             }
         }
         else
         {
             if(a[j]>1)
             {
                 flag=1;
                 break;
             }
         }
     }
     if(flag==0)
     printf("YES\n");
     else
     printf("NO\n");
    }
}


Post a Comment

0 Comments