/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scn = new Scanner(System.in);
int tc = scn.nextInt();
int max = Integer.MIN_VALUE;
int flag = 0;
for(int t = 0; t<tc; t++){
int a = scn.nextInt();
int b = scn.nextInt();
if(a - b > max){
flag = 1;
max = a-b;
}
if(b - a > max){
flag = 2;
max = b-a;
}
}
System.out.print(flag+" "+max);
}
}
Can someone help me out in finding error within my code. it is working fine for given sample test case. however it is giving wrong answer when i submit it.
T = int(input())
max=0
at = None
for i in range(T):
(a,b) = map(int,input().split())
if abs(a-b) > max:
max = abs(a-b)
if (a-b) > 0:
at = True
else:
at = False
if at == True:
print(“1”,max)
elif at == None:
x =0
else:
print(“2”,max)
why this is wrong
this is running for the given test cases.
and working for evey case i can think of.
What is wrong with my code provided test case works fine but gives wrong answer on submission ?
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.hasNextInt()?sc.nextInt():0;
int c = -1;//Stores the maximum lead player1 will have
int d = -1;//Stores the maximum lead player2 will have
for(int i=0;i<n;i++){
int a = sc.hasNextInt()?sc.nextInt():0;//player1 score for nth round
int b = sc.hasNextInt()?sc.nextInt():0;//player2 score for nth round
int lead = Math.abs(a-b);
//c or d get assigned lead if current lead attained by them is higher than what they previously have
c = a>b && lead>c?lead:c;
d = b>a && lead>d?lead:d;
}
System.out.println((c>d?1:2)+" "+(c>d?c:d));
}
}
I am getting right answer as i run custom input but when i submit it is showing me runtime error can anyone tellme whats wrong . #include
using namespace std;
int maxlead(int n, int a[])
{
int max=a[0];
for(int i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
return max;
}
int main()
{
int a[10],b[10],n,sum1=0,sum2=0,i,lead[10],leader,max1,max2;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i] >>b[i];
leader=a[i]-b[i];
if(leader>=0)
{
lead[i]=a[i]-b[i];
sum1=sum1+lead[i];
}
else
{
lead[i]=b[i]-a[i];
sum2=sum2+lead[i];
}
}
if(sum1>sum2)
{
max1=maxlead(n,lead);
cout<<"\n1 “<<max1;
}
else
{
max2=maxlead(n,lead);
cout<<”\n2 "<<max2;
}
return 0;
}
The question really has unclear explanation about what it means to say. Lets explain this question clearly to you with ans too…
Taking the example given the question :
the input given is:
5
140 82
89 134
90 110
112 106
88 90
In the round 1, the player 1 scores 140 points and player 2 scores 82 so the cumulative score of player 1 becomes 140 (as 0 + 140 = 140 ) and the cumulative score of player 2 becomes 82 (0+82).
now as cumulative score of player 1 is greater then player 2 then we will store the lead score of player 1 ( which is 58 as 140-82 = 58) and 58>0(see note). so the lead score of player 1 is now 58.
Note : Here the default lead scores of player 1 and 2 were 0.
now in round 2, player 1 score is 89 and player 2 score is 134. so the cumulative score of player 1 becomes 229 ( 140 + 89) and the cumulative score of player 2 becomes 216 (82+134). Now as cumulative score of player 1 is greater then the cumulative score of player2 so, first we will calculate the difference of cumulative scores of player 1 and player 2 and check whether the difference is greater than lead score of player 1 (as cumulative score of player 1 was greater then the cumulative score of player 2 in the second round ). as 13 (229-216) is less than 58, so the lead score of player 1 remains 58.
In the third round, the cumulative score of player 1 becomes 319 (229+90) and cumulative score of player 2 becomes 326 (216+110). now as cumulative score of player 2 is greater then player 1 so now we compare the lead score of player 2 with the difference of cumulative score of player 2 and player 1 (which is 326-319 =7 ). now as lead score of player 2 ( which is 0 ) is less than 7 so the lead score of player 2 is updated to 7.
now in the forth round, the cumulative score of player 1 becomes 431(319+112) and cumulative score of player 2 becomes 432 (326+106). now as cumulative score of player 2 is greater then player 1 so now we compare the lead score of player 2 with the difference of cumulative score of player 2 and player 1 (which is 432-431 =1 ). now as lead score of player 2 ( which is 7 ) is greater than 1 so the lead score of player 2 is not changed.
Now, in the fifth round, the cumulative score of player 1 becomes 519 (431+88) and cumulative score of player 2 becomes 522 (432+90). now as cumulative score of player 2 is greater then player 1 so now we compare the lead score of player 2 with the difference of cumulative score of player 2 and player 1 (which is 522-519 =3 ). now as lead score of player 2 ( which is 7 ) is greater than 3 so the lead score of player 2 is not changed .
now at last we compare the lead scores of both the players and as lead score of player 1 which is 58 is greater than lead score of player 2 which is 7 so player 1 is the winner.
’ ’ ’ #include
using namespace std;
int main()
{
int rounds, ld, mxld = 0, p1, p2;
int p = 0;
cin >> rounds;
while (rounds–&&rounds<10000)
{
cin >> p1 >> p2;
if (p1 > p2)
{
ld = p1 - p2;
if (ld > mxld)
{
mxld = ld;
p = 1;
}
}
else
{
ld = p2 - p1;
if (ld > mxld)
{
mxld = ld;
p = 2;
}
}
}
cout << p << " " << mxld;
return 1;
’ ’ ’
Why does my code show runtime error? (I am aware of the fact that i have not taken into account the cumulative point scoring idea but i just would like to know what’s up with runtime error issue)
Here is my simple code for the problem TLG, using Python 3.
import math
n = int(input())
player = 0
max_score = 0
for _ in range(n):
s, t = map(int, input().split())
diff_score = int(math.fabs(s-t))
if max_score < diff_score:
max_score = diff_score
if s < t:
player = 2
else:
player = 1
print(player, max_score)
My submission status shows “Wrong Answer”!
Would anybody like to help me, what is wrong with the code??
int main() {
int T;
cin>>T;
int first[T];
int last[T];
for(int i=0; i<T; i++){
int N, M;
cin>>N>>M;
first[i]=N;
last[i]=M;
}
int high[T];
int highl[T];
int a=0;
int b=0;
for(int j=0; j<T; j++){
if(first[j]>last[j]){
high[a]=(first[j]-last[j]);
a+=1;
}
int main() {
int t;
cin>>t;
int p1;
int p2;
int max=0;
int lead=0;
while(t){
cin>>p1>>p2;
if(p1>p2 && abs(p1-p2)>max){
max=p1-p2;
lead=1;
}
if(p2>p1 && abs(p1-p2)>max){
max=p2-p1;
lead=2;
#include<bits/stdc++.h>
using namespace std;
int main() {
long long x=0,y=0;
int t;
cin>>t;
while(t–){
long long a,b;
cin>>a>>b;
if(a>b)
x = max(a-b,x);
else
y = max(b-a,y);
}
if(x>y)
cout<<1<<" “<<x<<endl;
else
cout<<2<<” "<<y<<endl;
return 0;
}