Why i am getting runtime error? (SIGSEGV) (LEADGAME)

my solution
question

You’re getting a runtime error on ideone because @line 8 you’re trying to read something into n whereas the standard input is empty. If you fill stdin with the sample input given in the question, it runs just fine and produces the correct output. Here’s the ideone link. However, when I submit your code on CodeChef, I’m bombarded with a series of WAs. ¯\_(ツ)_/¯

I have one idea! to bypass the run time error! (if your logic is right).there are few steps you follow then i surely challenge that you didn’t get any run time error!;
step 1:
first see constraint and see test cases.
step2: then apply “if”(conditional) statement on test cases.
example: 1<T<100 (contraint)
implement this in your code then …
if(t>=1 && t<=100)

then what should i do to get it correct in codechef

is my logic correct??

Your logic appears to be a little incorrect. Here’s a list of issues:

  1. @line 14 - The comparison a>b doesn’t help in finding out the winner. Instead you may use this:
score[i]=score[i-1]+(a-b);

and if score[i] is positive, Player 1 is the winner of round i or else Player 2 is.

  1. @lines 24, 25, 28 - score may store negative values but lead must be positive, so you may use the abs function for absolute values.

  2. @lines 27, 28 - You’re trying to access suspicious indices of score and player.

I’ve made the changes in your code with comments explaining why.

Here's the code
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    long long n,x;cin>>n;
    long long score[n+1];
    long long player[n];
      score[0]=0;
    for(int i=1;i<=n;i++){
        int a,b;cin>>a>>b;
        /*if(a>b){*/score[i]=score[i-1]/*-*/+(a-b);
        if(score[i]<0){/*score[i]=score[i]*(-1);} */
        player[i-1]=/*1*/2; // as b is the second operand of the subtraction (a-b) @line 14
       }
        else{/*score[i]=score[i-1]-(b-a);
        if(score[i]<0){score[i]=score[i]*(-1);}*/
        player[i-1]=/*2*/1; // as a is the first operand of the subtraction (a-b) @line 14
        }
    } long long max = score[0];
     for (int i = 1; i <= n; i++) {
        // if (score[i] > max){
        if (abs(score[i]) > max){ // lead is positive but score[i] may hold a negative value
            // max = score[i];
            max = abs(score[i]); // lead is positive but score[i] may hold a negative value
        }}
        for(int i=0;i</*=*/n;i++){ // player has valid indices 0, 1, 2, ..., n-1
            // if(max==score[i]){x=player[i-1];}
            if(max==abs(score[i+1])){x=player[i];} // player[i] corresponds to the score[i+1]
        }
        cout<< x<<" "<< max<<endl;

     return 0;
}

I hope this is clear now. :slight_smile:

2 Likes