C++ code to count local extrema of given array


Extrema are such numbers which are either minima or maxima. In other words, it is a number or element which is either greater than or less than both of its adjacent values.

Suppose we have an array A with n elements. An element of this array A[i] is called a local minimum if and only if it is strictly less than both of its neighbours. Also if it is strictly greater than its neighbours it will be local maximum. For A[0] and A[n-1] as there are only one neighbour they are not maxima or minima. We have to find the number of local extrema in the given array.

For a given set of numbers, the first and last one can never be extrema. In a given array, we can find such local extrema by using various C++ techniques.

Input Output scenarios

Consider a set of N elements in an array. Then, we can calculate the total number of local extrema present in the array.

Input: numbers = 12, 16, 4
Output: 1
Input: numbers = 10, 16, 12, 5, 23, 40
Output: 2

Using for Loop

In this approach, we will use a for loop which will iterate from second element to the second last element of the given array. This is because the first and last element can never be extrema. Within the loop, we will check for each element whether it is greater or lesser than both of its adjacent elements by using if statements. If either of the condition is true, the num variable is incremented by one.

Example

#include <iostream>
using namespace std;
int numberOfExtrema(int x[], int N){
   int num = 0;
   // Iterate the loop from the second
   // element to the second last element
   for(int i = 1; i < N-1; i++){
      if(x[i] > x[i-1] && x[i] > x[i+1]){
         num++;
      }
      if(x[i] < x[i-1] && x[i] < x[i+1]){
         num++;
      }
   }
   return num;
}
int main() {
   int x[] = {10, 16, 12, 5, 23, 40};
   int N = sizeof(x) / sizeof(x[0]);
   std::cout << "Number of local extrema in the array: " << numberOfExtrema(x, N);
   return 0;
}

Output

Number of local extrema in the array: 2

Using the C++ Standard Template Library

We can use the maximum and minimum operations of the STL (Standard Template Library) algorithm. std::max() and std::min() are predefined functions used to return the largest and smallest element of a specified range of an array respectively.

Syntax

std::max_element(first, last)
std::min_element(first, last)

Where,

  • first is the position of the element from where the range begins.

  • last is The position of the element where the range ends.

If we want to find the local extrema, we need to consider three elements at a time (the current element and both its neighbors). So, our range consists of three elements. Hence, we use specify those three elements within curly braces.

std::max({element 1, element 2, element 3});

Here, we iterate the loop from second element to the second last element of the given array. We find the maximum and minimum element among x[i - 1], x[i], x[i + 1]. If the current element is equal to either of the two then, num is incremented by one.

Example

Let us see an example −

#include <iostream>
#include <algorithm>
using namespace std;
int numberOfExtrema(int x[], int N){
   int num = 0;
   // Iterate the loop from the second
   // Element to the second last element
   for (int i = 1; i < N - 1; i++) {
      int max_element = std::max({x[i - 1], x[i], x[i + 1]});
      int min_element = std::min({x[i - 1], x[i], x[i + 1]});
      if (x[i] == max_element || x[i] == min_element) {
         num++;
      }
   }
   return num;
}
int main() {
   int x[] = {10, 16, 12, 5, 23, 40};
   int N = sizeof(x) / sizeof(x[0]);
   std::cout << "Number of local extrema in the array: " << numberOfExtrema(x,N);
   return 0;
}

Output

Number of local extrema in the array: 2

Using Recursion

Here, we will use recursion to solve our problem. We will divide the array into smaller subarrays and find the central value in it. Then, we check whether the central element is greater than or smaller than both of its adjacent elements.

If either of the condition is satisfied, the num variable is incremented by one. As the recursive function calls itself, all the results are combined to give the final output.

Example

#include <iostream>
int recursion(int x[], int left, int right) {
   // If subarray contains 2 or less elements
   if (right - left <= 1) {
      return 0;
   }
   int center = left + (right - left) / 2;
   int num = 0;
   if ((x[center] > x[center - 1] && x[center] > x[center + 1]) ||
   (x[center] < x[center - 1] && x[center] < x[center + 1])) {
      num++;
   }
   num += recursion(x, left, center);
   num += recursion(x, center, right);
   return num;
}
int numberOfExtrema(int x[], int N) {
   return recursion(x, 0, N - 1);
}
int main() {
   int x[] = {10, 16, 12, 5, 23, 40};
   int N = sizeof(x) / sizeof(x[0]);
   std::cout << "Number of local extrema in the array: " << numberOfExtrema(x, N) << std::endl;
   return 0;
}

Output

Number of local extrema in the array: 2

Conclusion

In this article, we have discussed different approaches to find the local extrema in an array. In the first approach, we have learnt about the simple iterative approach which involves use of for loop. In the second approach, we have used the std::max() and std::min() functions. In the third approach, we have discussed about the recursive method which is useful for larger arrays.

Updated on: 05-Jan-2024

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements