FBMT - Editorial

I have this code in java

import java.io.*;

public class Main
{
    static void logInput(String[] log,int n) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a=0,b=0;
        for (int i = 0; i < n; i++)
        {
            String s = br.readLine();
            if (log[0]==null)
            {
                a++;
                log[0] = s;
            }
            else if (log[0].equals(s))
                a++;
            else if (log[1]==null)
            {
                b++;
                log[1] = s;
            }
            else if (log[1].equals(s))
                b++;
        }
        if (a>b)
            System.out.println(log[0]);
        else if (a<b)
            System.out.println(log[1]);
        else if(a==b)
            System.out.println("Draw");
    }

    static void testCase() throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] log = new String[2];
        logInput(log,n);
    }

    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for (int i = 0; i < t; i++)
        {
            testCase();
        }
        br.close();
    }
}

and I get a nzec now if I combine all the function i.e do everything in main() I get correct ans

import java.io.*;
 
public class Main
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for (int i = 0; i < t; i++)
        {
            int n = Integer.parseInt(br.readLine());
            String[] log = new String[2];
            int a=0,b=0;
            for (int k = 0; k < n; k++)
            {
                String s = br.readLine();
                if (log[0]==null)
                {
                    a++;
                    log[0] = s;
                }
                else if (log[0].equals(s))
                    a++;
                else if (log[1]==null)
                {
                    b++;
                    log[1] = s;
                }
                else if (log[1].equals(s))
                    b++;
            }
            if (a>b)
                System.out.println(log[0]);
            else if (a<b)
                System.out.println(log[1]);
            else if(a==b)
                System.out.println("Draw");
        }
        br.close();
    }
} 

I don’t understand how dividing it into functions can cause a nzec do I need to catch the exceptions thrown by other methods except main()?

Hello:

My code works on online IDE but is not getting accepted and saying it’s NZEC error in Python

¿Can anybody tell me why?

n1=int(input().strip())
for i in range(n1):
    arr=[]
    n2 = int(input().strip())
    for j in range(n2):
        arr_t = str(input().strip())
        arr.append(arr_t)
    arr_uniq=list(set(arr))
    if (arr.count(arr_uniq[0])>arr.count(arr_uniq[1])):
        print(arr_uniq[0])
    elif (arr.count(arr_uniq[0])<arr.count(arr_uniq[1])):
        print(arr_uniq[1])
    else:
        print("Draw")

its showing run time error?what is the error?its working well for test cases.
#include
#include
using namespace std;

int main()
{
int t,i,x;
cin>>t;
while(t–)
{
int count1=0,count2=0;
cin>>x;
string s[x];
for(i=0;i<x;i++)
cin>>s[i];
string a=s[0];
string b;
for(i=0;i<x;i++)
{
if(s[i]==a)
count1++;
else
{
b=s[i];
count2++;
}
}
if(count1>count2)
cout<<a<<endl;
else if(count2>count1)
cout<<b<<endl;
else
cout<<“Draw”<<endl;
}
return 0;
}

I am getting WA in here. the code works fine with no error. Help is appreciated !!.
i request codechef team to give data for different test case which is input for our submission.
here’s my code

https://www.codechef.com/viewsolution/17078404

Nice Editorial

1 Like

#include

//my solution in c++enter code here

 using namespace std;

int main()
{
long long int t;
cin>>t;

for(long long i=0;i<t;i++){
    long long int n;
    cin>>n;
    long long x=0,y=0;
    string temp,b,temp1;
    for(long long j=0;j<n;j++){
        if(x==0){
            cin>>b;
            temp=b;
            x=x+1;//x=1
            continue;
        }
        if(x>=1){
            cin>>b;
            if(b!=temp){
                y=y+1;
                temp1=b;
                
            }
            if(b==temp){
                x=x+1;
                
            }
            
        }
        
        
        
    }
    if(x>y){cout<<temp<<endl;};
    if(x==y){cout<<"Draw"<<endl;};
    if(x<y){cout<<temp1<<endl;};
    
    
}

}

Keep in mind, N can be 0 too and then when you do this

t1=str[0];

t2=str[1];

is not valid!! and that’s the reason you getting NZEC

And first and second String can be same too so if you have input

5

fe

fe

rem

rem

rem

your code’s not going to work

you can check out my code if you want

CodeChef: Practical coding for everyone hope this helps!!

n can be 0, so if it is then you’re asking for a string

cin >> s

which is not valid

I corrected your solution here’s the AC link to your solution

https://www.codechef.com/viewsolution/16680712

N = 0 can also be present, in that case while loop runs will give you tle, as it runs for 2*INT_MAX iterations, after which it loops back to 0 and breaks.

Input files are large and cin/cout are the slowest input/output streams. So, use the lines “ios_base::sync_with_stdio(false);” in your code. See editorialist submission for details.

I had used fast input output during the contest but it is also tle
https://www.codechef.com/viewsolution/16644901

okay I found the solution there was a memory leak which happens due to multiple buffer reader objects sharing the same input stream so I declared a static field like

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

and it worked fine

check the logic the brackets are a bit messed up .its showing runtime error on codechef but working well on codeblocks

Hello, my python code is working for custom input case but is not getting accepted and showing “Wrong Answer”.Can anyone tell me my mistake, please?
Thanks in advance!
[Pythoncode]:
https://www.codechef.com/viewsolution/23700466

1 Like
t = int(input())
while(t>0):
n = int(input())
if(n!=0):
    str1 = ""
    str2 = ""
    str1 = input()
    count1 = 1
    count2 = 0
    n -= 1
    if(n>=1):
        while(n>0):
            str2 = input()
            if(str2 != str1):
                count2 += 1
            elif(str2 == str1):
                count1 += 1
            n -= 1
    if(count1 > count2):
        print(str1)
    elif(count1 == count2):
        print("Draw")
    else:
        print(str2)
    t -= 1
else:
    print("Draw")
    t -= 1

Can someone provide me test case that fails, seems to be working all the time :smiley:
It’s not getting accepting I am getting Wrong Answer, also the problem needs to be clear with n = 0 as inout the output should be draw. It could have been clear when no teams are given as the question says there are atleast two teams, highly contradicting.

i do not know much about python
But there are maps in c++ which have good application in the respected problem.

You can check if there exists some ds in python functioning similiar to maps in c++
There might be coz python is such a broad language.

Try this TT bro:
It may help

Sample input:
2
1
cbt
0

Sample output

cbt
Draw

#include
using namespace std;
int main(){
int testcases;
cin >> testcases;
while(testcases>0){
int numofentries,firstteamwins=0,secondteamwins=0;
cin >> numofentries;
string firstteam;
string secondteam;
string answer;
if(numofentries>0){
cin >> firstteam;
firstteamwins++;
for(int i=0;i<numofentries-1;i++){
string winstring;
cin>> winstring;
if(winstring==firstteam){
firstteamwins++;
}
else{
secondteam=winstring;
secondteamwins++;
}
}
if(firstteamwins>secondteamwins){
answer=firstteam;
}
else if(secondteamwins>firstteamwins){
answer=secondteam;
}
else if(secondteamwins==firstteamwins){
answer=“Draw”;
}
cout << answer<<"\n";
}
else{
cout << “Draw”<<"\n";
}
testcases–;
}
return 0;
}
This is my approach.
It should be clarified what should be the output in case of n=0.
Caused me lots of wa on this question.