Help me in solving LOGICIAN problem

My issue

can anyone tell for string 101 why the answer for last character is No instead of Yes

My code

import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0)
		{
		    int n = sc.nextInt();
		    String s=sc.next();
		    
		    for(int i=0; i<n; i++)
		    {
		        if(s.charAt(i)=='0') System.out.println("NO");
		        else if(s.charAt(i)=='1' && i<n-1) System.out.println("IDK");
		        else if(s.charAt(i)=='1' && i==n-1)
		            System.out.println("YES");
		    }
		}
	}
}

Problem Link: 3 Logicians Walk into a Bar Practice Coding Problem - CodeChef

hii
See i have solved the problem, but i prefer C++.
So i am providing you the solution, i hope u may understand it.

include
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
int n, count = 0;
cin>>n;
string s;
cin>>s;
for ( int i = 0; i < n-1; i++)
{
if ( s[i] == ‘1’ && count == 0 )
cout<<“IDK”<<endl;
else if ( s[i] == ‘1’ && count > 0)
{
cout<<“NO”<<endl;
count++;
}
else if ( s[i] == ‘0’)
{
cout<<“NO”<<endl;
count++;
}
}
if ( s[n-1] == ‘0’)
cout<<“NO”<<endl;
else if ( s[n-1] == ‘1’ && count > 0)
cout<<“NO”<<endl;
else if ( s[n-1] == ‘1’ && count == 0)
cout<<“YES”<<endl;
}
return 0;
}

take a look at this its an even easier approach and a smaller one

include
include
using namespace std;

int main() {
int N,T,i,j,k;
string S;
cin>>T;
while(T–)
{

bool flag=false;
    cin>>N;
    cin>>S;
   for(i=0;i<N-1;i++)
   {
       if(S[i]=='0')
       {
           for(j=i;j<N;j++)
           {
        flag=true;
        cout<<"NO"<<"\n";
           }

       }
       if(flag)
       break;
       cout<<"IDK"<<"\n";
   }
       if(flag== false && S[N-1]=='0')
       cout<<"NO"<<"\n";
       if(flag==false && S[N-1]=='1')
       cout<<"YES"<<"\n";
      
}
// your code goes here
return 0;

}

take a look at this more simple code though it is in cpp

include
include
using namespace std;

int main() {
int N,T,i,j,k;
string S;
cin>>T;
while(T–)
{

bool flag=false;
    cin>>N;
    cin>>S;
   for(i=0;i<N-1;i++)
   {
       if(S[i]=='0')
       {
           for(j=i;j<N;j++)
           {
        flag=true;
        cout<<"NO"<<"\n";
           }

       }
       if(flag)
       break;
       cout<<"IDK"<<"\n";
   }
       if(flag== false && S[N-1]=='0')
       cout<<"NO"<<"\n";
       if(flag==false && S[N-1]=='1')
       cout<<"YES"<<"\n";
      
}
// your code goes here
return 0;

}

@sunnykumar85 -

  1. First person will say IDK
  2. 2nd person will say NO - because he doesnt want beer
  3. The original question was ‘Does everyone want a beer?’ Now the 3rd person knows the 2nd person doesnt want beer - hence his answer will be NO