0

When i run the code in pycharm or some other compiler its is working fine but when i run code in hacker rank and hacker earth i am getting value error.

Please help me guys. The question is:

Max Pair Sum
Given a list of distinct integers A, find the largest sum of any pair of elements. Input

6 
1 2 3 89 4 10 
  • First line contains the size of list N.

  • Second line contains list elements.

Output

99 

Here it's my solution :

N=int(input("enter N :"))
Data=[]
for i in range(N):
    Data.append(int(input()))
#print(Data)
Data.sort()
#print(Data)
max_val=max(Data)
print(max_val)
Data=Data[0:len(Data)-1]
#print(Data)
x=list(map(lambda x:x+max_val,Data))
max_val=max(x)
print(max_val)
Ralubrusto
  • 1,394
  • 2
  • 11
  • 24
  • 2
    *Second line contains list elements.* - you're trying to read N many lines though... seems you're probably supposed to just read the second line, then split it up...? – Jon Clements Nov 29 '20 at 17:44
  • Does this answer your question? [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – Tomerikoo Dec 01 '20 at 09:12

2 Answers2

1

input() in python3 reads the whole line as a string. In your case, it is reading all numbers at once as string. Now, string contains numbers with spaces that's why python is not able to convert that string to int.

Use, Data = list(map(int, input().split())) to read the whole line as an array or a list of integers.

GreatWhite
  • 106
  • 5
0

the second line contains multiple values at the same which are separated by " ". so we can take the input from the user which is a string eg "1 2 3 4". we can split the input based on " ", which returns ["1", "2", "3", "4"]. now we are converting this iterable into int by using map which takes int function and iterable from split and converts it to an array [1,2,3,4]

N=int(input("enter N :"))
Data=map(int, input().split(" "))  #map returns a list    
#print(Data)
Data.sort()
#print(Data)
max_val=max(Data)
print(max_val)
Data=Data[0:len(Data)-1]
#print(Data)
x=list(map(lambda x:x+max_val,Data))
max_val=max(x)
print(max_val)