The Wayback Machine - https://web.archive.org/web/20240906165446/https://www.geeksforgeeks.org/python-program-to-find-the-strongest-neighbour/
Open In App

Python program to find the Strongest Neighbour

Last Updated : 28 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
Python Program for Find sum of even factors of a number
Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, â€Ķ pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (
4 min read
Python Program for Find minimum sum of factors of number
Given a number, find minimum sum of its factors.Examples: Input : 12Output : 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3 = 2 + 2 + 3 = 7Therefore minimum sum is 7Input : 105Output : 15 C/C++ Code # Python program to
1 min read
Python Program for Find largest prime factor of a number
Given a positive integer \'n\'( 1 <= n <= 1015). Find the largest prime factor of a number. Input: 6 Output: 3 Explanation Prime factor of 6 are- 2, 3 Largest of them is 3 Input: 15 Output: 5 C/C++ Code # Python3 code to find largest prime # factor of number import math # A function to find largest prime factor def maxPrimeFactors (n): # Init
3 min read
Python Program for Find sum of odd factors of a number
Given a number n, the task is to find the odd factor sum. Examples: Input : n = 30 Output : 24 Odd dividers sum 1 + 3 + 5 + 15 = 24 Input : 18 Output : 13 Odd dividers sum 1 + 3 + 9 = 13 Let p1, p2, ... pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (p1a1)*(p2a2
2 min read
Python program to find common elements in three lists using sets
Prerequisite: Sets in Python Given three arrays, we have to find common elements in three sorted lists using sets. Examples : Input : ar1 = [1, 5, 10, 20, 40, 80] ar2 = [6, 7, 20, 80, 100] ar3 = [3, 4, 15, 20, 30, 70, 80, 120] Output : [80, 20] Input : ar1 = [1, 5, 5] ar2 = [3, 4, 5, 5, 10] ar3 = [5, 5, 10, 20] Output : [5] Method 1: We have given
5 min read
Python Program to Find Sum of Array
Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provided to demonstrate the calculation of the sum for a
4 min read
Python program to find all Strong Numbers in given list
Given a list, write a Python program to find all the Strong numbers in a given list of numbers. A Strong Number is a number that is equal to the sum of factorial of its digits. Examples: Input : [1, 2, 5, 145, 654, 34] Output : [1, 2, 145] Input : [15, 58, 75, 675, 145, 2] Output : [145, 2] Explanation : We defined 2 functions here: First is factor
6 min read
Python Program for Find sum of Series with n-th term as n^2 - (n-1)^2
We are given an integer n and n-th term in a series as expressed below: Tn = n2 - (n-1)2 We need to find Sn mod (109 + 7), where Sn is the sum of all of the terms of the given series and, Sn = T1 + T2 + T3 + T4 + ...... + Tn Examples: Input : 229137999 Output : 218194447 Input : 344936985 Output : 788019571 Let us do some calculations, before writi
2 min read
Python Program to find the cube of each list element
Given a list, the task is to write a python program to cube all the list elements. Input: [1, 2, 3, 4] Output: [1, 8, 27, 64] Explanation: Cubing all the list elementsInput: [2, 4, 6] Output: [8, 64, 216] Method 1: Using loop This is the brute force way. In this, we just multiply the same element two times by itself. Example: C/C++ Code # Initializ
4 min read
Python program to find the smallest word in a sentence
Given a string S of lowercase English alphabets, the task is to print the smallest word in the given string. Examples: Input: S = “sky is blue”Output: "is"Explanation: Length of “sky” is 3.Length of is “is” 2.Length of “blue” is 4.Therefore, the smallest word is “is”. Input: S = “geeks for geeks”Output: "for" Searching-based Approach: Refer to this
5 min read
Python program to find the group sum till each K in a list
Given a List, the task is to write a Python program to perform grouping of sum till K occurs. Examples: Input : test_list = [2, 3, 5, 6, 2, 6, 8, 9, 4, 6, 1], K = 6 Output : [10, 6, 2, 6, 21, 6, 1] Explanation : 2 + 3 + 5 = 10, grouped and cumulated before 6. Input : test_list = [2, 3, 5, 6, 2, 6, 8], K = 6 Output : [10, 6, 2, 6, 8] Explanation : 2
3 min read
Python program to find sum of absolute difference between all pairs in a list
Given a list of distinct elements, write a Python program to find the sum of absolute differences of all pairs in the given list.Examples: Input : [9, 2, 14] Output : 24 Explanation: (abs(9-2) + abs(9-14) + abs(2-14)) Input : [1, 2, 3, 4] Output : 10 Explanation: (abs(1-2) + abs(1-3) + abs(1-4) + abs(2-3) + abs(2-4) + abs(3-4)) The first approach i
5 min read
Python program to find smallest number in a list
We are given a list of numbers and our task is to write a Python program to find the smallest number in given list. For the following program we can use various methods including the built-in min method, sorting the array and returning the last element, etc.Example: Input : list1 = [10, 20, 4] Output : 4 Input : list2 = [20, 10, 20, 1, 100] Output
8 min read
Python program to find second largest number in a list
Given a list of numbers, the task is to write a Python program to find the second largest number in the given list. Examples: Input: list1 = [10, 20, 4]Output: 10 Input: list2 = [70, 11, 20, 4, 100]Output: 70 Method 1: Sorting is an easier but less optimal method. Given below is an O(n) algorithm to do the same. C/C++ Code # Python program to find
7 min read
Python program to find sum of elements in list
Given a list of numbers, write a Python program to find the sum of all the elements in the list. Example: Input: [12, 15, 3, 10]Output: 40 Input: [17, 5, 3, 5]Output: 30 Python program to find sum of elements in listExample #1: C/C++ Code # Python program to find sum of elements in list total = 0 # creating a list list1 = [11, 5, 17, 18, 23] # Iter
5 min read
Python program to find uncommon words from two Strings
Given two sentences as strings A and B. The task is to return a list of all uncommon words. A word is uncommon if it appears exactly once in any one of the sentences, and does not appear in the other sentence. Note: A sentence is a string of space-separated words. Each word consists only of lowercase letters. Examples: Input : A = "Geeks for Geeks"
4 min read
Python Program to find transpose of a matrix
Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[][] is obtained by changing A[i][j] to A[j][i]. For Square Matrix: The below program finds transpose of A[][] and stores the result in B[][], we can change N for different dimension. C/C++ Code # Python3 Program to find # transpose of a
5 min read
Python program to find the sum of all items in a dictionary
Given a dictionary in Python, write a Python program to find the sum of all items in the dictionary. Examples: Input : {'a': 100, 'b':200, 'c':300}Output : 600 Input : {'x': 25, 'y':18, 'z':45}Output : 88 Method #1: Using Inbuilt sum() Function Use the sum function to find the sum of dictionary values. C/C++ Code # Python3 Program to find sum of #
6 min read
Python program to find middle of a linked list using one traversal
Given a singly linked list, find the middle of the linked list. Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. Method 1: Traverse the whole linked list and count the no. of nodes. Now traverse the list again till count/2 and return the n
5 min read
Python program to find the highest 3 values in a dictionary
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Examples: Input : my_dict = {'A': 67, 'B': 23, 'C': 45, 'D': 56, 'E': 12, 'F': 69} Output : {'F': 69, 'A': 67, 'D': 56} Let us see different m
5 min read