Write a Python program to find the Strongest Neighbour


This program is a simple python script that calculates the maximum of adjacent elements of an input list "a1" and stores the result in a new list "a2".

Here's what the program does in detail:

  • The variable "n" is defined as 6 which indicates the number of elements in the input list "a1".
  • The input list "a1" is defined as [10, 20, 30, 20, 30, 400].
  • The list "a2" is defined as an empty list and will be used to store the result.
  • A for loop is used to iterate over the elements in the range from 1 to n-1 (which is 1 to 5 in this case).
  • In each iteration, the maximum value between the current element (a1[i]) and the previous element (a1[i-1]) is calculated using the "max()" function and stored in the variable "r".
  • The value of "r" is then appended to the list "a2".
  • After the loop, another for loop is used to print each element of the list "a2". The end parameter is set to " " to print each element on the same line with a space separator.

Source Code

n = 6
a1 = [10,20,30,20,30,400]
a2 = []	
for i in range(1, n):
    r = max(a1[i], a1[i-1])
    a2.append(r)
for i in a2 :
    print(i,end=" ")

Output

20 30 30 30 400

Example Programs