HEADBOB - Editorial

PROBLEM LINK:

Practice
Contest

Author: Piyush Kumar
Tester: Minako Kojima
Editorialist: Pawel Kacprzak

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Adhoc

PROBLEM:

Indian people use only gestures identified by ‘I’ and ‘N’. Non Indian people use only gestures identified by ‘Y’ and ‘N’. Given a string consisting of only ‘Y’, ‘N’ and ‘I’, decide if the string represents an Indian person, non Indian person or it is impossible to determine which one it represents. For each of these decision answers are: “INDIAN”, “NOT INDIAN” and “NOT SURE”.

QUICK EXPLANATION:

You need to decide if the string contains at least one ‘Y’ or at least one ‘I’ or neither of these letters is presented in the string.

EXPLANATION:

Problem statement ensures that it is impossible to have both ‘Y’ and ‘I’ in the string.

Based on that fact we can easily compute the answer.

If the string contains at least one ‘I’, then any other letter in is it ‘I’ or ‘N’, so the person represented by this string is Indian, hence the answer is “INDIAN”.

On the other hand, if the string contains at least one ‘Y’, then any other letter in it is ‘Y’ or ‘N’, so the person represented by this string is non Indian, hence the answer is “NOT INDIAN”.

Otherwise, if there is no ‘I’ and ‘Y’ in the string, each letter in it is ‘N’, so we cannot be sure if it represents Indian or non Indian, hence the answer is “NOT SURE”.

In order to do that, you can iterate over the string in a single loop:

Pseudo Code

	found = 0
	for(i = 0; i < s.length(); ++i)
             if (s[i] != 'N')
		if(s[i] == 'Y') 
		    res = "NOT INDIAN"
		else if (s[i] == 'I')
		    res = "INDIAN"
		found = 1
		break
	if(found==0)
	    res = "NOT SURE"

	print res        

Complexity

O(n) since we just need a single pass over the string

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.

RELATED PROBLEMS:

2 Likes

found = 0
for(i = 0; i < s.length(); ++i)
if (s[i] != ‘N’)
if(s[i] == ‘Y’)
res = “NOT INDIAN”
else if (s[i] == ‘I’)
res = “INDIAN”
found = 1
break
if(found==0)
res = “NOT SURE”

print res

#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
char str[1005];
int len;
while(t–)
{
cin>>len;
cin>>str;
int k=0;
for(int k=0;k<len;k++)
hash[str[k]-‘A’]++;
if(hash[13]>0&&hash[8]>0&&hash[24]==0)
cout<<“INDIAN”<<endl;
else if(hash[13]>0&&hash[8]==0&&hash[24]>0)
cout<<“NOT INDIAN”<<endl;
else
cout<<“NOT SURE”<<endl;
}
}

Problem description is insufficient and has been described in very complex manner

I am not able to find error in my code. It seems to me correct, however I am getting WA. Anyone help please.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class HEADBOB {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        System.in));
        int t = Integer.parseInt(br.readLine());
        while(t-- > 0) {
            int size = Integer.parseInt(br.readLine());
            String s = br.readLine();
            boolean alln = false;
            boolean ind = false;
            for(int i = 0, j = s.length(); i < j ; ++i) {
                if(s.charAt(0) == 'N') {
                    alln = true;
                }
                if(alln && s.charAt(i) != 'N') {
                    alln = false;
                }
                if(s.charAt(i) == 'I') {
                    ind = true;
                    break;
                }

            }
            if(ind) {
                System.out.println("INDIAN");
                continue;
            }
            if(alln) {
                System.out.println("NOT SURE");
                continue;
            }
            System.out.println("NOT INDIAN");
        }
    }
}

@brij_raj you should keep

 
if(s.charAt(0) == 'N') {
     alln = true;
} 

outside the loop… because otherwise NNYNN test case fails

@https://discuss.codechef.com/users/277440/l_returns
Thanks.

1 Like

welcome :slight_smile:

help ??

WHAT IS WRONG IN THIS CODE

t= int(input(""))
a=0
b=0
for i in range(0,t):
str=input("")
for j in range(0,len(str)):
a=0
b=0
if (str[j]==‘Y’) :

        a+=1
        
        break

    elif(str[j]=='I') :
       
        b+=1
        break
     


if(a==1):
    print('NOT INDIAN')

elif(b==1):
    print('INDIAN')

else:
    print('NOT SURE')

HELLO EVERYONE.
MY OUTPUT IS CORRECT BUT ON SUBMITTING IT SAYS WRONG. HELP ME WITH THIS.

for _ in range(int(raw_input())):
        s=""
        fore=0
        ind=0
        n=int(raw_input())
        s=raw_input()
        for j in range(len(s)):
            if s[j]=="N":
                fore=1 
                ind=1 
            elif s[j]=="Y":
                fore=1 
                ind=0
            elif s[j]=="I":
                fore=0
                ind=1 
            else:
                fore=2
                ind=2
        if fore==1 and ind==0:
            print "NOT INDIAN"
        elif fore==0 and ind==1:
            print "INDIAN"
        else:
            print "NOT SURE"