JavaScript Program for Number of local extrema in an array


JavaScript Program for the Number of local extrema in an array is a common problem in the field of computer science and data analysis. In this problem, we are required to find the number of local extrema in an array using JavaScript. Local extrema are the peaks and valleys in a sequence of numbers. They are the points where the values change from increasing to decreasing or from decreasing to increasing. Finding the number of local extrema in an array can be a challenging task, but it is an essential step in many data analysis applications.

In this tutorial, we will present a step-by-step guide to solving this problem using JavaScript. We will walk you through the logic behind the code and provide a clear understanding of how the program works. By the end of this tutorial, you will be able to write your own JavaScript program for finding the number of local extrema in an array. So let’s get started!

So, first thing first. Let’s understand the Problem statement with some examples.

Problem Statement

The problem statement requires us to find the number of local extrema in any given array of numbers.

Sample Examples −

Example 1

Input: [1, 2, 3, 2, 1, 4, 5, 6, 5, 4, 3, 2, 1]
Output: 3

Explanation − In the given input array, there are three local extrema. The first local extrema are at index 2 where the value changes from 3 to 2. The second local extrema are at index 6 where the value changes from 1 to 4. The third local extrema are at index 8 where the value changes from 6 to 5. Therefore, the output is 3.

Example 2

Input: [2, 4, 6, 8, 10]
Output: 0

Explanation − In the given input array, there are no local extrema. As the values are continuously increasing, there is no point where the value changes from increasing to decreasing. Therefore, the output is 0.

In both examples, we can see that the input array is checked for local extrema, and the program outputs the number of local extrema found in the array. The number of local extrema is determined by finding the points in the array where the values change from increasing to decreasing or from decreasing to increasing.

Now after understanding the problem statement it’s time to understand the algorithm for this problem statement.

Algorithm

Here's a possible algorithm for finding the number of local extrema in an array −

  • Step 1 − Initialize a variable 'count' to 0 to keep track of the number of local extrema.

  • Step 2 − Loop through the array starting from index 1 and ending at index n-2, where n is the length of the array.

  • Step 3 − For each element in the array, check if it is greater than its previous and next elements or lesser than its previous and next elements. If yes, then increment the 'count' variable.

  • Step 4 − After completing the loop, return the 'count' variable as the number of local extrema in the array.

  • Step 5 − Let’s understand the implementation of this algorithm using Javascript with the help of an example.

Example

Implementation of the above algorithm using Javascript −

In this program, we have defined a function named 'findLocalExtremaCount' that takes an array as an argument and returns the number of local extrema in that array. We have then defined two arrays 'arr1' and 'arr2', and called the 'findLocalExtremaCount' function on each of them. We have used template literals to display the input array and the output count for each array.

Input 1: arr[] = [1,2,3,2,1,4,5,6,5,4,3,2,1]

Expected Output: Number of local extrema in [1,2,3,2,1,4,5,6,5,4,3,2,1] is 3

Input 2: arr[] = [2,4,6,8,10]

Expected Output: Number of local extrema in [2,4,6,8,10] is 0

function findLocalExtremaCount(arr) {
   let count = 0;
   for (let i = 1; i < arr.length - 1; i++) {
      if ((arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) || (arr[i] < arr[i - 1] && arr[i] < arr[i + 1])) {
         count++;
      }
   }
   return count;
}
const arr1 = [1, 2, 3, 2, 1, 4, 5, 6, 5, 4, 3, 2, 1];
console.log(`Number of local extrema in [${arr1}] is ${findLocalExtremaCount(arr1)}`);
const arr2 = [2, 4, 6, 8, 10];
console.log(`Number of local extrema in [${arr2}] is ${findLocalExtremaCount(arr2)}`);

Conclusion

In this tutorial, we have developed a JavaScript program to find the number of local extrema in an array. The program implements a simple algorithm that iterates through the array and checks each element to see if it is greater or smaller than its neighbouring elements. If it is, then it is local extrema and the program increments the count. The program uses a for loop to iterate over the array and has a time complexity of O(n), where n is the number of elements in the array.

The logic behind the code is straightforward and can be easily understood. We have used template literals to display the input and output of the program in an easy-to-understand format. The program takes an array as an input and returns the number of local extrema in that array. In the end, we tested the program with two sample arrays and it provides the correct output for each of them.

Updated on: 02-May-2023

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements