Given an array arr[] of N positive integers. The task is to find the maximum for every adjacent pair in the array.
Examples:
Input: 1 2 2 3 4 5 Output: 2 2 3 4 5 Input: 5 5 Output: 5
Approach:
- Read the input array i.e,arr1.
- for i=1 to sizeofarray-1
- find the max value between arr1[i] and arr1[i-1].
- store the above value in another array i.e, arr2.
- print the values of arr2.
Below is the implementation.
Python3
# define a function for finding # the maximum for adjacent # pairs in the array def maximumAdjacent(arr1, n): # array to store the max # value between adjacent pairs arr2 = [] # iterate from 1 to n - 1 for i in range(1, n): # find max value between # adjacent pairs gets # stored in r r = max(arr1[i], arr1[i-1]) # add element arr2.append(r) # printing the elements for ele in arr2 : print(ele,end=" ") if __name__ == "__main__" : # size of the input array n = 6 # input array arr1 = [1,2,2,3,4,5] # function calling maximumAdjacent(arr1, n) |
Output:
2 2 3 4 5
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python Program for Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Python Program for Program to find area of a circle
- Python Program for Program to Print Matrix in Z form
- Python Program for Program to calculate area of a Tetrahedron
- Python Program for Efficient program to print all prime factors of a given number
- Python program to find IP Address
- Python program to find N largest elements from a list
- Python program to find the most occurring character and its count
- Python program to find Cumulative sum of a list
- Python Program for Find remainder of array multiplication divided by n
- Python Program for Find the perimeter of a cylinder
- Python Program for Find sum of even factors of a number
- Python Program for Find minimum sum of factors of number
- Python Program for Find largest prime factor of a number
- Python Program for Find sum of odd factors of a number
- Python Program to find largest element in an array
- Python program to find common elements in three lists using sets
- Python program to find all Strong Numbers in given list
- Python Program for Find sum of Series with n-th term as n^2 - (n-1)^2
- Python program to find sum of absolute difference between all pairs in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

