Number of local extrema in an array
Last Updated :
29 Jul, 2022
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.
Implementation:
C++
// CPP to find number
// of extrema
#include <bits/stdc++.h>
using namespace std;
// function to find
// local extremum
int extrema(int a[], int n)
{
int count = 0;
// start loop from position 1
// till n-1
for (int i = 1; i < n - 1; i++)
{
// 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] is greater
// than both its neighbours
// then add 1 to x
count += (a[i] > a[i - 1] && a[i] > a[i + 1]);
// check if a[i] is
// less than both its
// neighbours, then
// add 1 to x
count += (a[i] < a[i - 1] && a[i] < a[i + 1]);
}
return count;
}
// driver program
int main()
{
int a[] = { 1, 0, 2, 1 };
int n = sizeof(a) / sizeof(a[0]);
cout << extrema(a, n);
return 0;
}
Java
// Java to find
// number of extrema
import java.io.*;
class GFG {
// function to find
// local extremum
static int extrema(int a[], int n)
{
int count = 0;
// start loop from
// position 1 till n-1
for (int i = 1; i < n - 1; i++)
{
// 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] is greater
// than both its neighbours
// then add 1 to x
if(a[i] > a[i - 1] && a[i] > a[i + 1])
count += 1;
// check if a[i] is
// less than both its
// neighbours, then
// add 1 to x
if(a[i] < a[i - 1] && a[i] < a[i + 1])
count += 1;
}
return count;
}
// driver program
public static void main(String args[])
throws IOException
{
int a[] = { 1, 0, 2, 1 };
int n = a.length;
System.out.println(extrema(a, n));
}
}
/* This code is contributed by Nikita Tiwari.*/
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
C#
// C# to find
// number of extrema
using System;
class GFG {
// function to find
// local extremum
static int extrema(int []a, int n)
{
int count = 0;
// start loop from
// position 1 till n-1
for (int i = 1; i < n - 1; i++)
{
// 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] is greater
// than both its neighbours
// then add 1 to x
if(a[i] > a[i - 1] && a[i] > a[i + 1])
count += 1;
// check if a[i] is
// less than both its
// neighbours, then
// add 1 to x
if(a[i] < a[i - 1] && a[i] < a[i + 1])
count += 1;
}
return count;
}
// Driver program
public static void Main()
{
int []a = { 1, 0, 2, 1 };
int n = a.Length;
Console.WriteLine(extrema(a, n));
}
}
/* This code is contributed by vt_m.*/
PHP
<?php
// PHP to find number
// of extrema
// function to find
// local extremum
function extrema($a, $n)
{
$count = 0;
// start loop from position 1
// till n-1
for ($i = 1; $i < $n - 1; $i++)
{
// 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] is 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] is
// 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 Code
$a = array( 1, 0, 2, 1 );
$n = count($a);
echo extrema($a, $n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// JavaScript program to find
// number of extrema
// function to find
// local extremum
function extrema(a, n)
{
let count = 0;
// start loop from
// position 1 till n-1
for (let i = 1; i < n - 1; i++)
{
// 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] is greater
// than both its neighbours
// then add 1 to x
if(a[i] > a[i - 1] && a[i] > a[i + 1])
count += 1;
// check if a[i] is
// less than both its
// neighbours, then
// add 1 to x
if(a[i] < a[i - 1] && a[i] < a[i + 1])
count += 1;
}
return count;
}
// Driver code
let a = [ 1, 0, 2, 1 ];
let n = a.length;
document.write(extrema(a, n));
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Javascript Program for Number of local extrema in an array 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}Outp
2 min read
Find a local minima in an array Given an array arr[0 .. n-1] of n distinct integers. The task is to find a local minimum in the array. An element arr[x] is considered a local minimum if it is smaller than both of its adjacent elements, meaning arr[x] < arr[x - 1] and arr[x] < arr[x + 1] for indices where 1 <= x <= n -
7 min read
Maximize array elements upto given number Given an array of integers, a number and a maximum value, task is to compute the maximum value that can be obtained from the array elements. Every value on the array traversing from the beginning can be either added to or subtracted from the result obtained from previous index such that at any point
15+ min read
How to get largest and smallest number in an Array? Given an array arr[] of length N, The task is to find the maximum and the minimum number in the array. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: Maximum is: 5Minimum is: 1Explanation: The maximum of the array is 5 and the minimum of the array is 1. Input: arr[] = {5, 3, 7, 4, 2}Output: Maximum
15 min read
Maximum XOR Queries With an Element From Array Given an array arr[] of size n containing non-negative integers and also given a list of q queries in a 2D array queries[][], where each query is of the form [xi, mi]. For each query, you need to find the maximum value of (xi XOR arr[j]) such that arr[j] is less than or equal to mi.In simple terms,
15+ min read