The lead game...Why do i get wrong answer

#include <stdio.h>

int main(int argc, const char * argv[])
{
int numberOfRounds = 0;
int playerOne[10000], playerTwo[10000];

scanf("%d", &numberOfRounds);

if(numberOfRounds <= 10000) {
for(int i = 0; i<numberOfRounds; i++){
    int pOne = 0, pTwo = 0;
    scanf("%d %d", &pOne, &pTwo);
    if((pOne > 0) && (pOne <= 10000)) {
        playerOne[i] = pOne;
    }
    else {
        break;
    }
    if((pTwo > 0) && (pTwo <= 10000)) {
        playerTwo[i] = pTwo;
    }
    else {
        break;
    }
}
}

int scoreOne = 0 , scoreTwo = 0;
for(int j = 0; j<numberOfRounds; j++){
    if(playerOne[j] > playerTwo[j]) {
        int score = playerOne[j] - playerTwo[j];
        if(score > scoreOne) {
            scoreOne = score;
        }
    }
    else {
        int score = playerTwo[j] - playerOne[j];
        if(score > scoreTwo) {
            scoreTwo = score;
        }
    }
}

if(scoreOne > scoreTwo) {
    printf("%d %d \n", 1, scoreOne);
}
else {
    printf("%d %d \n", 2, scoreTwo);
}

return 0;

}

One solution may be that you are only checking for the highest lead in a single round, but what is required is the net lead at the end of that round, that is cumulative lead.

for examlpe

2

100 2

100 2

100 2

Your code gives output

1 98

But the actual output should be

1 294

That is the net lead for player 1 at the end of round 3 is 294 which is the highest lead in this case.
So go for the cumulative lead and your program should work.

1 Like