Imprima todos los picos y valles en una array de enteros

Dado un arreglo de enteros arr[] , la tarea es imprimir una lista de todos los picos y otra lista de todos los valles presentes en el arreglo. Un pico es un elemento en la array que es mayor que sus elementos vecinos. De manera similar, un canal es un elemento que es más pequeño que sus elementos vecinos.

Ejemplos:  

Entrada: arr[] = {5, 10, 5, 7, 4, 3, 5} 
Salida: 
Picos: 10 7 5 
Valles: 5 5 3
Entrada: arr[] = {1, 2, 3, 4, 5} 
Salida: 
Picos: 5 
Canales: 1  

Enfoque: para cada elemento de la array, verifique si el elemento actual es un pico (el elemento debe ser mayor que sus elementos vecinos) o un valle (el elemento debe ser más pequeño que sus elementos vecinos). 
Tenga en cuenta que el primer y el último elemento de la array tendrán un solo vecino.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function that returns true if num is
// greater than both arr[i] and arr[j]
static bool isPeak(int arr[], int n, int num,
                   int i, int j)
{
 
    // If num is smaller than the element
    // on the left (if exists)
    if (i >= 0 && arr[i] > num)
        return false;
 
    // If num is smaller than the element
    // on the right (if exists)
    if (j < n && arr[j] > num)
        return false;
    return true;
}
 
// Function that returns true if num is
// smaller than both arr[i] and arr[j]
static bool isTrough(int arr[], int n, int num,
                     int i, int j)
{
 
    // If num is greater than the element
    // on the left (if exists)
    if (i >= 0 && arr[i] < num)
        return false;
 
    // If num is greater than the element
    // on the right (if exists)
    if (j < n && arr[j] < num)
        return false;
    return true;
}
 
void printPeaksTroughs(int arr[], int n)
{
    cout << "Peaks : ";
 
    // For every element
    for (int i = 0; i < n; i++) {
 
        // If the current element is a peak
        if (isPeak(arr, n, arr[i], i - 1, i + 1))
            cout << arr[i] << " ";
    }
    cout << endl;
 
    cout << "Troughs : ";
 
    // For every element
    for (int i = 0; i < n; i++) {
 
        // If the current element is a trough
        if (isTrough(arr, n, arr[i], i - 1, i + 1))
            cout << arr[i] << " ";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 5, 10, 5, 7, 4, 3, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    printPeaksTroughs(arr, n);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
    // Function that returns true if num is
    // greater than both arr[i] and arr[j]
    static boolean isPeak(int arr[], int n, int num,
                                    int i, int j)
    {
 
        // If num is smaller than the element
        // on the left (if exists)
        if (i >= 0 && arr[i] > num)
        {
            return false;
        }
 
        // If num is smaller than the element
        // on the right (if exists)
        if (j < n && arr[j] > num)
        {
            return false;
        }
        return true;
    }
 
    // Function that returns true if num is
    // smaller than both arr[i] and arr[j]
    static boolean isTrough(int arr[], int n, int num,
                                        int i, int j)
    {
 
        // If num is greater than the element
        // on the left (if exists)
        if (i >= 0 && arr[i] < num)
        {
            return false;
        }
 
        // If num is greater than the element
        // on the right (if exists)
        if (j < n && arr[j] < num)
        {
            return false;
        }
        return true;
    }
 
    static void printPeaksTroughs(int arr[], int n)
    {
        System.out.print("Peaks : ");
 
        // For every element
        for (int i = 0; i < n; i++)
        {
 
            // If the current element is a peak
            if (isPeak(arr, n, arr[i], i - 1, i + 1))
            {
                System.out.print(arr[i] + " ");
            }
        }
        System.out.println("");
 
        System.out.print("Troughs : ");
 
        // For every element
        for (int i = 0; i < n; i++)
        {
 
            // If the current element is a trough
            if (isTrough(arr, n, arr[i], i - 1, i + 1))
            {
                System.out.print(arr[i] + " ");
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {5, 10, 5, 7, 4, 3, 5};
        int n = arr.length;
 
        printPeaksTroughs(arr, n);
    }
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 implementation of the approach
 
# Function that returns true if num is
# greater than both arr[i] and arr[j]
def isPeak(arr, n, num, i, j):
 
    # If num is smaller than the element
    # on the left (if exists)
    if (i >= 0 and arr[i] > num):
        return False
 
    # If num is smaller than the element
    # on the right (if exists)
    if (j < n and arr[j] > num):
        return False
    return True
 
# Function that returns true if num is
# smaller than both arr[i] and arr[j]
def isTrough(arr, n, num, i, j):
 
    # If num is greater than the element
    # on the left (if exists)
    if (i >= 0 and arr[i] < num):
        return False
 
    # If num is greater than the element
    # on the right (if exists)
    if (j < n and arr[j] < num):
        return False
    return True
 
def printPeaksTroughs(arr, n):
 
    print("Peaks : ", end = "")
 
    # For every element
    for i in range(n):
 
        # If the current element is a peak
        if (isPeak(arr, n, arr[i], i - 1, i + 1)):
            print(arr[i], end = " ")
    print()
 
    print("Troughs : ", end = "")
 
    # For every element
    for i in range(n):
 
        # If the current element is a trough
        if (isTrough(arr, n, arr[i], i - 1, i + 1)):
            print(arr[i], end = " ")
 
# Driver code
arr = [5, 10, 5, 7, 4, 3, 5]
n = len(arr)
 
printPeaksTroughs(arr, n)
 
# This code is contributed by Mohit Kumar

C#

// C# implementation of the approach
using System;
     
class GFG
{
 
    // Function that returns true if num is
    // greater than both arr[i] and arr[j]
    static Boolean isPeak(int []arr, int n, int num,
                                    int i, int j)
    {
 
        // If num is smaller than the element
        // on the left (if exists)
        if (i >= 0 && arr[i] > num)
        {
            return false;
        }
 
        // If num is smaller than the element
        // on the right (if exists)
        if (j < n && arr[j] > num)
        {
            return false;
        }
        return true;
    }
 
    // Function that returns true if num is
    // smaller than both arr[i] and arr[j]
    static Boolean isTrough(int []arr, int n, int num,
                                        int i, int j)
    {
 
        // If num is greater than the element
        // on the left (if exists)
        if (i >= 0 && arr[i] < num)
        {
            return false;
        }
 
        // If num is greater than the element
        // on the right (if exists)
        if (j < n && arr[j] < num)
        {
            return false;
        }
        return true;
    }
 
    static void printPeaksTroughs(int []arr, int n)
    {
        Console.Write("Peaks : ");
 
        // For every element
        for (int i = 0; i < n; i++)
        {
 
            // If the current element is a peak
            if (isPeak(arr, n, arr[i], i - 1, i + 1))
            {
                Console.Write(arr[i] + " ");
            }
        }
        Console.WriteLine("");
 
        Console.Write("Troughs : ");
 
        // For every element
        for (int i = 0; i < n; i++)
        {
 
            // If the current element is a trough
            if (isTrough(arr, n, arr[i], i - 1, i + 1))
            {
                Console.Write(arr[i] + " ");
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = {5, 10, 5, 7, 4, 3, 5};
        int n = arr.Length;
 
        printPeaksTroughs(arr, n);
    }
}
 
// This code is contributed by Princi Singh

Javascript

<script>
 
// Function that returns true if num is
// greater than both arr[i] and arr[j]
function isPeak(arr, n, num, i,  j)
{
 
    // If num is smaller than the element
    // on the left (if exists)
    if (i >= 0 && arr[i] > num)
        return false;
 
    // If num is smaller than the element
    // on the right (if exists)
    if (j < n && arr[j] > num)
        return false;
    return true;
}
 
// Function that returns true if num is
// smaller than both arr[i] and arr[j]
function isTrough( arr, n,  num, i,  j)
{
 
    // If num is greater than the element
    // on the left (if exists)
    if (i >= 0 && arr[i] < num)
        return false;
 
    // If num is greater than the element
    // on the right (if exists)
    if (j < n && arr[j] < num)
        return false;
    return true;
}
 
function printPeaksTroughs( arr, n)
{
    document.write(  "Peaks : ");
 
    // For every element
    for (var i = 0; i < n; i++) {
 
        // If the current element is a peak
        if (isPeak(arr, n, arr[i], i - 1, i + 1))
            document.write( arr[i] + " ");
    }
    document.write( "<br>");
 
    document.write(  "Troughs : ");
 
    // For every element
    for (var i = 0; i < n; i++) {
 
        // If the current element is a trough
        if (isTrough(arr, n, arr[i], i - 1, i + 1))
            document.write( arr[i] + " ");
    }
}
var arr=[ 5, 10, 5, 7, 4, 3, 5 ];
 
    printPeaksTroughs(arr, 7);
 
 
 
 
 
</script>
Producción: 

Peaks : 10 7 5 
Troughs : 5 5 3

 

Publicación traducida automáticamente

Artículo escrito por divyamohan123 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 *