Answer the question
In order to leave comments, you need to log in
How to find adjacent array elements whose sum is maximum (minimum)?
Greetings
The first part of the question
is not yet possible to comprehend how to solve the problem correctly:
Given an array. Find two adjacent elements whose sum is maximum.
# первый вариант
lst0 = [random.randrange(1, 10) for x in range(5)]
print(lst0)
max_lst = 0
for x in range(len(lst0) - 1):
x = lst0[x] + lst0[x + 1]
if x > max_lst:
max_lst = x
print(max_lst, end='\n\n\n')
# второй вариант
lst1 = [random.randrange(1, 10) for x in range(5)]
print(lst1)
lst1 = [lst1[x] + lst1[x + 1] for x in range(len(lst1) - 1)]
print(max(lst1), end='\n\n\n')
# третий вариант
lst2 = [random.randrange(1, 10) for x in range(5)]
print(lst2)
lst2 = [lst2[x] + lst2[x + 1] for x in range(len(lst2) - 1)]
lst2.sort()
print(lst2[-1])
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question