TLG - Editorial

#include
using namespace std;
int k;
int modulus1(int a)
{
if(a>0)
k=a;
else
k=-a;
return k;
}

    int main()
{
  int i,rounds,j,q,l,s;
        cin>>rounds;
  int playerone[rounds];//stores scores of player one in each round
  int playertwo[rounds];//stores scores of player two in each round
  int lead[rounds];//stores lead of player one over player 2
  int moduluslead[rounds];//stores modulus of lead
   s=0;
  for(i=0;i<rounds;i++)
  {
           cin>>playerone[i];
      cin>>playertwo[i];
      s=s+playerone[i]-playertwo[i];
      lead[i]=s;
      moduluslead[i]=modulus1(lead[i]);
  }
  j=moduluslead[0];
  q=0;
  for(i = 1;i<rounds; i++)//loop to find modulus of maximum lead
 {
     if(moduluslead[i]>j)
     {
         j=moduluslead[i];
         q=i;
     }
 }
 l=2;
 if (lead[q]>0)
          l=1;

#include

using namespace std;

int main() {
int n, a, b, max, player;
cin >> n;
if (n == 0)
{
cout << 0 << " " << 0 << “\n”;
}
else
{
int** p;
p = new int*[n];
for (int i = 0; i < n; i++)
{
p[i] = new int[2];
}

    int** win;
    win = new int*[n];
    for (int i = 0; i < n; i++)
    {
        win[i] = new int[2];
    }

    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        if ( i == 0)
        {
            p[i][0] = a;
            p[i][1] = b;
        }
        else
        {  
            p[i][0] = p[i-1][0] + a;
            p[i][1] = p[i-1][1] + b;
        }
    }

    for (int i = 0; i < n; i++)
    {
        if (p[i][0] > p[i][1])
        {
            win[i][0] = 1;
            int x = p[i][0] - p[i][1];
            win[i][1] = x;
        }
        else
        {
            win[i][0] = 2;
            int x = p[i][1] - p[i][0];
            win[i][1] = x;
        }
    }

    for (int i = 0; i < n; i++)
    {
        max = win[0][1];
        if (win[i][1] > max)
        {
            max = win[i][1];
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (win[i][1] == max)
        {
            player = win[i][0];
        }
    }
    cout << player << " " << max << "\n";
    delete [] win;
    delete [] p;
}

}

it is running properly for the sample input but i am unable to submit it

this code work fine in all custom test case, but not accepting and shown wrong answer, any body helping me out here, whats wrong in it.

#include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;

int maxdiff=0;
int player= 0;
int diff=0;

while(t--)
{
    int a,b;
    cin>>a>>b;
    
   
    if (a>b)
    {
        diff = a-b;
        if(diff>maxdiff)
        {
            maxdiff = diff;
            player =1;
           
        }
    }
    else
    {
        diff=b-a;
        if(diff>maxdiff)
        {
            maxdiff = diff;
            player =2;
           
        }
    }
    
}

cout<<player<<" "<<maxdiff<<endl;

return 0;

}[quote=“jaay, post:167, topic:8657, full:true”]
this code work fine in all custom test case, but not accepting and shown wrong answer, any body helping me out here, whats wrong in it.

#include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;

int maxdiff=0;
int player= 0;
int diff=0;

while(t--)
{
    int a,b;
    cin>>a>>b;
    
   
    if (a>b)
    {
        diff = a-b;
        if(diff>maxdiff)
        {
            maxdiff = diff;
            player =1;
           
        }
    }
    else
    {
        diff=b-a;
        if(diff>maxdiff)
        {
            maxdiff = diff;
            player =2;
           
        }
    }
    
}

cout<<player<<" "<<maxdiff<<endl;

return 0;

}
[/quote]

Please help! I am getting WA but not able to identify corner cases.

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t, max = 0;
cin >> t;
int a[t];
for (int i = 0; i < t; ++i) {
    int p1, p2;
    cin >> p1 >> p2;
    a[i] = p1 - p2;
    if (abs(a[i]) > abs(max)) {
        max = a[i];
    }
}
cout << (max < 0 ? "2 " : "1 ") << abs(max);
return 0;

}

For this test-case
5
50 20
10 90
80 20
90 10
50 20
Expected o/p :
1 120
Your o/p:
2 80

1 Like

Hi! finally solved it. The issue was with the initialization part and here is my new code:

#include<bits/stdc++.h>
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t, max = 0, a[1] = {0}, b[1] = {0};
cin >> t;
for (int i = 0; i < t; ++i) {
    int p1, p2;
    cin >> p1 >> p2;
    a[i] += p1;
    b[i] += p2;
    if (abs(a[i]-b[i]) > abs(max)) {
        max = a[i]-b[i];
    }
}
cout << (max < 0 ? "2 " : "1 ") << abs(max);
return 0;

}

1 Like

Great :+1:t3:

1 Like

this is my code for the TLG problem
it is showing wrong answer after submitting
please review it

try:
t=int(input())
score=[]
for i in range(t):
p1,p2=[int(x) for x in input().split()]

        if p1>p2:
              lead=p1-p2
              w=1
              score.append([w,lead])
        else:
              lead=p2-p1
              w=2
              score.append([w,lead])
  score.sort(key=lambda x: x[1])
  print(score[t-1][0],abs(score[t-1][1]))

except:
pass

#include <bits/stdc++.h>
using namespace std;

int main() {
int t;cin>>t;
vector<vector> a(t);
for(int i=0;i<t;i++){
int x,y;cin>>x>>y;
a[i].push_back(x);
a[i].push_back(y);
}

vector<vector<int>> b(t);
for(int i=0;i<t;i++){
      int k=abs(a[i][0]-a[i][1]);
      b[i].push_back(k);

      if(a[i][0]>a[i][1]){
           b[i].push_back(1);
      }
      else{
           b[i].push_back(2);
      }

}

sort(b.begin(),b.end(),[&](vector<int> &a,vector<int> &b){
   return a[0]>b[0];
});

cout<<b[0][1]<<" "<<b[0][0]<<endl;
return 0;

}

what is the problem in above code?