Help me in solving DIFFSUM problem

My issue

My code

# cook your dish here
a = int(input())
b = int(input())
if a > b:
    print( a - b )
else:
    print( a + b )
    

Problem Link: DIFFSUM Problem - CodeChef

The problem here is that the input comes in one line e.g. “82 28”. Therefore when you set a = int(input()), you are essentially saying a = int("82 28"), which cannot be type casted. Instead, you should use the following syntax: a, b = map(int, input().split()). Breaking this down:

  • input().split(), for the test-case results in: [“82”, “28”].
  • Then the map() function take each element in the list above, casts those elements as int (specified by the int keyword passed to map()) and then assigns each element to the respective variables in this case a and b.

This syntax allows you to take a single line input and assign it to multiple variables.

Here is the working code using this method:

a, b = map(int, input().split())
if a > b:
    print(a - b)
else:
    print(a + b)

#include <stdio.h>

int main(void) {
// your code goes here
int a,b;
scanf(“%d %d”,&a,&b);
if(a > b)
{
printf(“%d”,a-b);
}
else
{
printf(“%d”,a+b);
}
return 0;
}