Python Program for Number of local extrema in an array Last Updated : 08 May, 2023 Comments Improve Suggest changes Like Article Like Report You are given an array on n-elements. An extrema is an elements which is either greater than its both of neighbors or less than its both neighbors. You have to calculate the number of local extrema in given array. Note : 1st and last elements are not extrema.Examples : Input : a[] = {1, 5, 2, 5} Output : 2 Input : a[] = {1, 2, 3} Output : 0 Approach :For calculating number of extrema we have to check whether an element is maxima or minima i.e. whether it is greater than both of its neighbors or less than both neighbors. For this simply iterate over the array and for each elements check its possibility of being an extrema.Note: a[0] and a[n-1] has exactly one neighbour each, they are neither minima nor maxima. Python3 # Python 3 to find # number of extrema # function to find # local extremum def extrema(a, n): count = 0 # start loop from # position 1 till n-1 for i in range(1, n - 1) : # only one condition # will be true # at a time either # a[i] will be greater # than neighbours or # less than neighbours # check if a[i] if # greater than both its # neighbours, then add # 1 to x count += (a[i] > a[i - 1] and a[i] > a[i + 1]); # check if a[i] if # less than both its # neighbours, then # add 1 to x count += (a[i] < a[i - 1] and a[i] < a[i + 1]); return count # driver program a = [1, 0, 2, 1 ] n = len(a) print(extrema(a, n)) # This code is contributed by Smitha Dinesh Semwal Output : 2 Time Complexity: O(n) where n is size of input array. This is because a for loop is executing from 1 to n. Space Complexity: O(1) as no extra space has been used. Please refer complete article on Number of local extrema in an array for more details! Comment More infoAdvertise with us Next Article Python Program for Number of local extrema in an array kartik Follow Improve Article Tags : Searching Python Python Programs DSA Arrays +1 More Practice Tags : ArrayspythonSearching Similar Reads Python Program for Least frequent element in an array Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them.Examples : Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1} Output : 3 3 appears minimum number of times in given array. Input : arr[] = {10, 20, 30} Output : 10 or 3 min read Python3 Program for Ceiling in a sorted array Given a sorted array and a value x, the ceiling of x is the smallest element in array greater than or equal to x, and the floor is the greatest element smaller than or equal to x. Assume that the array is sorted in non-decreasing order. Write efficient functions to find floor and ceiling of x. Examp 4 min read Python Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app 3 min read Python3 Program for Check for Majority Element in a sorted array Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers. Basically, we need to write a function say isMajority() that takes an array (arr[] ), arrayâs size (n) and a number to be searched (x) as parameters and returns true if x is a majorit 7 min read Python Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples:Â Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Method 1: The idea is to run the loop for no_of_rows. Check each element inside the row and fi 5 min read Python program to test if all elements in list are maximum of K apart Given a list of numbers, the task is to write a Python program to test if all elements are maximum of K apart. Examples: Input : test_list = [475, 503, 425, 520, 470, 500], K = 100Output : True Explanation : Maximum element is 520 and minimum is 425, 520-425 = 95, which is less than 100, hence eleme 5 min read Python3 Program for Range Queries for Frequencies of array elements Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; lef 4 min read Find index of element in array in python We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we a 2 min read Find Index of Element in Array - Python In Python, arrays are used to store multiple values in a single variable, similar to lists but they offer a more compact and efficient way to store data when we need to handle homogeneous data types . While lists are flexible, arrays are ideal when we want better memory efficiency or need to perform 2 min read Python Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix 3 min read Like