Python Program for Number of local extrema in an array


In this article, we will learn a python program for a number of local extrema in an array.

An extrema is an element that is either greater than or less than both of its neighbors.

Assume we have taken an array containing n elements. We will now find the count of a number of local extrema in a specified input array.

Note

The first and last elements are not extrema.

Using For loop

NOTE

Both array[0] and array[n-1] have only one neighbor each, hence they are neither minima nor maxima.

len() − The number of items in an object is returned by the len() method.The len() function returns the number of characters in a string when the object is a string.

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Create a function findExtrema() that returns the local extrema in an array by accepting input array, and array length as arguments.

  • Create a variable to store the count of the number of local extrema in an array.

  • Use the for loop to traverse from the first element of the array to the length of the array using the len() function.

  • At any given time, only one of the following conditions will be true: either a[i] will be greater than neighbors or less than neighbors.

  • check if a[i] is greater than both its neighbors using an if conditional statement and add the result to the count.

  • Similarly, check if a[i] is lesser than both its neighbors using an if conditional statement and add the result to the count.

  • Return the count using the return statement.

  • Create a variable to store an input array and print the given input array.

  • Use the len() function(number of items in an object) to get the length of an input array.

  • Call the findExtrema() function by passing the input array and array length as arguments to it to print the count of the number of local extrema in an array.

Example

The following program returns the Number of local extrema in an array using for loop −

# creating a function that returns the local extrema
# in an array by accepting input array,
# array length as arguments
def findExtrema(inputArray, arrayLength):
   # storing the count of no of local extrema in an array
   outputCount = 0
   # traversing from the first index to the length of the given array
   for k in range(1, arrayLength - 1):
      # At any given time, only one of the following conditions will be true:
      # either a[i] will be greater than neighbors or less than neighbors.
      # check if a[i] if greater than both its neighbours
      # Here it increments the output count by 1 if the condition is true
      # Else it increments output count by 0(same value) if condition is False
      outputCount += (inputArray[k] > inputArray[k - 1] and inputArray[k] > inputArray[k + 1])
      # check if a[i] if lesser than both its neighbours
      outputCount += (inputArray[k] < inputArray[k - 1] and inputArray[k] < inputArray[k + 1])
   # returning the number of local extrema of the given array
   return outputCount
# input array
inputArray = [5, 0, 1, 2, 1, 0, 3, 4, 1, 2]
# getting the length of an array
arrayLength = len(inputArray)
# Printing the given array
print("The Given Array is:", inputArray)
# calling the findExtrema() function by passing the
# input array and array length as arguments to it.
print("The Number of local extrema is:", findExtrema(inputArray, arrayLength))

Output

On execution, the above program will generate the following output −

The Given Array is: [5, 0, 1, 2, 1, 0, 3, 4, 1, 2]
The number of local extrema is: 5

Time Complexity: O(n)

Auxiliary Space: O(1)

Because there was no more space used, the space complexity is O. (1).

Because we only used a for loop to iterate through the list, the time complexity is O(N), where N is the number of elements in the given list or array.

Conclusion

After learning about local extrema in this article, we used Python's for loop to implement the same problem.

Updated on: 31-Jan-2023

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements