Open In App

Python program to find the Strongest Neighbour

Last Updated : 28 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Read the input array i.e,arr1.
  2. 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.
  3. 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

Time complexity: O(n), where n is the size of the input array. This is because the program iterates through the array once to calculate the maximum value between adjacent pairs and then iterates through the array again to print the output.
Auxiliary space: O(n) because the program creates an additional array, arr2, to store the maximum value between adjacent pairs. The size of arr2 is equal to n-1 because the maximum value cannot be calculated for the last element in the array since it does not have an adjacent element.



Similar Reads

Python - Replacing by Greatest Neighbour in list
Given a list, the task is to write a Python program to replace with the greatest neighbor among previous and next elements. Input : test_list = [5, 4, 2, 5, 8, 2, 1, 9], Output : [5, 5, 5, 8, 8, 8, 9, 9] Explanation : 4 is having 5 and 2 as neighbours, replaced by 5 as greater than 2. Input : test_list = [5, 4, 2, 5], Output : [5, 5, 5, 5] Explanat
2 min read
Python Program for Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
You have been given a series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!, find out the sum of the series till nth term. Examples: Input :n = 5 Output : 2.70833 Input :n = 7 Output : 2.71806 C/C++ Code # Python code to find smallest K-digit # number divisible by X def sumOfSeries(num): # Computing MAX res = 0 fact = 1 for i in range(1, num+1): fact *=
1 min read
Python Program for Program to Print Matrix in Z form
Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 C/C++ Code # Python program to print a # square matrix in Z form arr = [[4, 5, 6, 8
2 min read
Python Program for Program to calculate area of a Tetrahedron
A Tetrahedron is simply a pyramid with a triangular base. It is a solid object with four triangular faces, three on the sides or lateral faces, one on the bottom or the base and four vertices or corners. If the faces are all congruent equilateral triangles, then the tetrahedron is called regular. The volume of the tetrahedron can be found by using
2 min read
Python Program for Efficient program to print all prime factors of a given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". Following are the steps to find all prime factors. 1) While n is divisible by 2, print 2 and divide n by 2. 2) After step 1, n must be
6 min read
Python Program for Program to cyclically rotate an array by one
Write a Python program for a given array, the task is to cyclically rotate the array clockwise by one time. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: arr[] = {5, 1, 2, 3, 4} Input: arr[] = {2, 3, 4, 5, 1}Output: {1, 2, 3, 4, 5} Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.Approach: Assign every element wi
4 min read
Python program to find N largest elements from a list
Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N. Examples : Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52] A simple solution traverse the given list N times. In every traversal, find the maximum, add it to result, and re
5 min read
Python program to find the most occurring character and its count
Given a string, write a python program to find the most occurrence character and its number of occurrences. Examples: Input : hello Output : ('l', 2) Input : geeksforgeeks Output : ('e', 4) We can solve this problem quickly in python using Counter() method. Simple Approach is to 1) Create a dictionary using Counter method having strings as keys and
3 min read
Python program to find Cumulative sum of a list
The problem statement asks to produce a new list whose i^{th} element will be equal to the sum of the (i + 1) elements. Examples : Input : list = [10, 20, 30, 40, 50]Output : [10, 30, 60, 100, 150]Input : list = [4, 10, 15, 18, 20]Output : [4, 14, 29, 47, 67] Approach 1 : We will use the concept of list comprehension and list slicing to get the cum
4 min read
Python Program for Find the perimeter of a cylinder
Given diameter and height, find the perimeter of a cylinder. Perimeter is the length of the outline of a two - dimensional shape. A cylinder is a three - dimensional shape. So, technically we cannot find the perimeter of a cylinder but we can find the perimeter of the cross-section of the cylinder. This can be done by creating the projection on its
2 min read