The code you provided seems to have a logical error. The problem lies in the initialization of the w variable. In the code, w is declared as a char type variable, but it is used to store the leader value, which should be an integer (either 1 or 2).
To fix the issue, you can change the data type of w from char to int. Here’s the corrected code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int l = 0;
int w = 0; // Change the data type of w from char to int
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
int lead = abs(a - b);
int maxi = max({a, b});
int leader; // Change the data type of leader from char to int
if (maxi == a) {
leader = 1;
}
else {
leader = 2;
}
if (l < lead) {
w = leader;
l = lead;
}
}
cout << w << " " << l << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int l = 0;
char w;
int sum1 = 0, sum2 = 0;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
sum1 += a;
sum2 += b;
int lead = abs(sum1 - sum2);
if (lead > l) {
l = lead;
if (sum1 > sum2) {
w = '1';
} else {
w = '2';
}
}
}
cout << w << " " << l << endl;
return 0;
}
In the modified code, I introduced two variables, sum1 and sum2, to keep track of the cumulative scores of players 1 and 2, respectively. Instead of comparing individual scores at each step, I calculate the cumulative scores and determine the lead based on the difference between them.