Número de extremos locales en una array

Se le da una array de n elementos. Un extremo es un elemento que es mayor que sus dos vecinos o menor que sus dos vecinos. Tienes que calcular el número de extremos locales en una array dada. 
Nota: los elementos primero y último no son extremos.

Ejemplos: 

Input : a[] = {1, 5, 2, 5}
Output : 2

Input : a[] = {1, 2, 3}
Output : 0

Enfoque: para calcular el número de extremos, debemos verificar si un elemento es máximo o mínimo, es decir, si es mayor que sus dos vecinos o menor que ambos vecinos. Para esto, simplemente itere sobre la array y para cada elemento verifique su posibilidad de ser un extremo.

Nota: a[0] y a[n-1] tienen exactamente un vecino cada uno, no son mínimos ni máximos.

Implementación:

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>
Producción

2

Complejidad temporal: O(n)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *