public static void main(String[] args){
int a,b,t,pA=0,pB=0,currentLeadingPlayer=0,currentlead=0;
int winningLead=0,winningPlayer=0;
Scanner scanner = new Scanner(System.in);
t =scanner.nextInt();
while (t-->0){
a=scanner.nextInt();
b=scanner.nextInt();
pA +=a;//adding previous score of a
pB +=b;//adding previous score of b
// If confused why am i doing this look in comment section
if (pA>pB){
currentLeadingPlayer =1;
currentlead = pA-pB;
}else {
currentLeadingPlayer =2;
currentlead = pB-pA;
}
if (winningLead<currentlead){
winningLead = currentlead;
winningPlayer = currentLeadingPlayer;
}
}
System.out.println(winningPlayer+" "+winningLead);
}
}
/*
Go Look in problem Statement again the second table shows us that for every player the score is added from previous score
and then the lead is calculated on those updated scores
Based on lead which player is selected
*/
nf = input()
ni = int(nf)
if ni >= 1 and ni <= 10000 :
lead = []
lead1 = []
R = []
even = []
for x in range(1,ni+1):
splitted = input().split()
for y in splitted :
if (0 > int(y) or int(y) > 1000 ):
exit()
else :
R.append(int(y))
n = 0
while n < 2*int(nf):
even.append(n)
n = n + 2
n = ni
for y in even :
if R[y] > R[y+1]:
L = R[y] - R[y+1]
lead.append(L)
lead1.append(1)
else:
L = R[y+1] - R[y]
lead.append(L)
lead1.append(2)
maxi = max(lead)
z = lead.index(maxi)
m = lead1[z]
print(m,maxi)
nf = input()
ni = int(nf)
if ni >= 1 and ni <= 10000 :
lead = []
lead1 = []
R = []
even = []
for x in range(1,ni+1):
splitted = input().split()
for y in splitted :
if (0 > int(y) or int(y) > 1000 ):
exit()
else :
R.append(int(y))
n = 0
while n < 2*int(nf):
even.append(n)
n = n + 2
n = ni
for y in even :
if R[y] > R[y+1]:
L = R[y] - R[y+1]
lead.append(L)
lead1.append(1)
else:
L = R[y+1] - R[y]
lead.append(L)
lead1.append(2)
maxi = max(lead)
z = lead.index(maxi)
m = lead1[z]
print(m,maxi)
Can somebody help me with this code. It is giving correct answer with the given values in the question.But after submitting it shows wrong answer. Any help will be appreciated. Thank you
I checked for sample input given in the problem. Tried couple of examples myself and it works fine. Can you please point out what is wrong here which prevents my answer form getting accepted.
The question is a bit unclear. Actually your solution is calculating lead, in each round. But the correct solution requires you to calculate the cumulative lead after each round.
Please tell what is wrong with my approach. am getting correct answer in my IDE. and even in codechef it showing run and compilation is success but it is showing output not matching.
/* package codechef; // don’t place package name! */
/* Name of the class has to be “Main” only if the class is public. */
public class Main
{
static int n;
static Scanner scanner = new Scanner(System.in);
private static long[] a;
private static int X;
private static int X2;
public static void main(String[] args) {
int n;
int w = 0;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
a = new long[n];
for (int i = 0; i < n; i++) {
int A = scanner.nextInt();
int B = scanner.nextInt();
if (A > B) {
a[i] = A - B;
} else {
a[i] = B - A;
}
/*
* System.out.println("A > X + B: " + A + ", " + X + ", " + B); if (A > X + B)
* { X = A; w = 1; } else { X = B; w = 2; }
*
* System.out.println("A > X + B and W : " + A + ", " + X + ", " + B + ", " +
* w);
*/
if (A - B > X) {
X = A - B;
// w = 1;
} else {
X2 = -(A - B);
// w = 2;
}
if (X > X2) {
w = 1;
} else {
w = 2;
}
}
Arrays.sort(a);
System.out.print(w + " ");
System.out.println(a[n - 1]);
}
For everybody who is asking the reason for WA, the scores are cumulative, read the question once again. The lead is counted cumulatively not one at a time.
Hope it helps
whats wrong with my code when i run it against custom input it give write answer but when i submit it then wrong answer is display please if somebody know problem with my code please tell me . my code is this #include <stdio.h>
int main(void) {
// your code goes here
int n;
scanf("%d",&n);
int a[n],b[n],max=0,p=1,d;
for(int i=0; i<n; i++)
{
scanf("%d%d",&a[i],&b[i]);
d=a[i]-b[i];
if(abs(d)>max)
{
max = abs(d);
if(d<0) p=2;
else p=1;
}
}
printf("\n%d %d",p,max);
return 0;
//can u tell me what we have to print?? it is not clear in the question whether we have to print the result and lead of the round 1 or every round result or end result??
Not necessary to use use array instead could be done with just the use of simple variables.
ie; Scores of player 1 and 2 are ‘a’ and ‘b’ respectively
Just keep count of the lead ( lead+=a-b; ) every time you input the scores.
If lead is positive it means player 1 is leading and if negative then it means player 2 is leading.
Also store the maximum lead in some variable.
code: CodeChef: Practical coding for everyone