Find the team with the maximum number of goals, or report if there is a “Draw”.
Explanation
The problem simply states to count the number of goals scored by each team and report the name of the winner team. Thus, if the first team scores x goals, then the other team scores (N - x) goals (because the total number of goals scored were N). Clearly, the winner is the one with the higher number of goals. To count the number of goals by the first team, we simply iterate through the list of team which scored the next goal and compare if it has the same name as the first team, then, we increment the counter. Now, we simply check using “If” conditions, whether the first team won or the second team won or there is a draw.
The time complexity of this approach will be the sum of length of strings in input (per test case) as each string comparison takes a total of O(\text{length of string}). This is the most optimal algorithm for even taking the input takes same time asymptotically.
For details, refer to the editorialist solution below.
Time Complexity
O(\sum_{i=1}^{i=N} |S_i|), per test case, where |S| = length of string S.
Hello, Can anyone please explain me why my solution is getting TLE. This solution is running perfectly in my PC as well as other online IDEs but in codechef IDE it is showing TLE.
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()?
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")
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
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.