Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. Local extrema are the peaks and valleys in a sequence of numbers.
In this article we are having two arrays, our task is to write a Javascript program for number of local extrema in an array.
What are Local Extrema?
A local extrema in an array is an element that is either:
- Local Maximum: Greater than both its neighbors (peak)
- Local Minimum: Smaller than both its neighbors (valley)
Elements at the start and end of the array are not considered local extrema since they don't have two neighbors.
Example
Input:
arr1 = [1, 2, 3, 2, 1, 4, 5, 6, 5, 4, 3, 2, 1];
First local extrema: 3, {2 < 3 > 2}
Second local extrema: 1, {2 > 1 < 4}
Third local extrema: 6, {5 < 6 > 5}
Output:
3
Input:
arr2 = [2, 4, 6, 8, 10];
Output:
0
Algorithm Steps
We will be following below mentioned steps for counting local extrema:
- Initialize a counter variable to track extrema count
- Iterate through the array from index 1 to length-2 (excluding first and last elements)
- For each element, check if it's greater than both neighbors OR smaller than both neighbors
- If condition is met, increment the counter
- Return the final count
Implementation
Here is a complete example implementing the algorithm using a for loop:
const arr1 = [1, 2, 3, 2, 1, 4, 5, 6, 5, 4, 3, 2, 1];
const arr2 = [2, 4, 6, 8, 10];
function localExtrema(arr) {
let count = 0;
// Skip first and last elements
for (let i = 1; i < arr.length - 1; i++) {
// Check if current element is local maximum or minimum
if ((arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) ||
(arr[i] < arr[i - 1] && arr[i] < arr[i + 1])) {
count++;
}
}
return count;
}
console.log("Array 1:", arr1);
console.log("Number of local extrema:", localExtrema(arr1));
console.log();
console.log("Array 2:", arr2);
console.log("Number of local extrema:", localExtrema(arr2));
Array 1: [ 1, 2, 3, 2, 1, 4, 5, 6, 5, 4, 3, 2, 1 ] Number of local extrema: 3 Array 2: [ 2, 4, 6, 8, 10 ] Number of local extrema: 0
How It Works
The algorithm works by examining each interior element and comparing it with its immediate neighbors:
- For arr1 = [1, 2, 3, 2, 1, 4, 5, 6, 5, 4, 3, 2, 1]: Elements 3, 1, and 6 are local extrema
- For arr2 = [2, 4, 6, 8, 10]: This is a strictly increasing sequence with no peaks or valleys
Time and Space Complexity
- Time Complexity: O(n) where n is the array length
- Space Complexity: O(1) as we only use a counter variable
Conclusion
Finding local extrema involves checking each interior element against its neighbors. This algorithm efficiently identifies peaks and valleys in O(n) time, making it suitable for analyzing data trends and patterns in arrays.
