原文:

给定一个整数数组 arr[] ,任务是打印数组中所有波峰的列表和所有波谷的另一个列表。峰是数组中大于其相邻元素的元素。类似地,槽是比其相邻元素小的元素。

示例:

输入: arr[] = {5,10,5,7,4,3,5} 输出: 峰值:10 7 5 谷值:5 5 3 输入: arr[] = {1,2,3,4,5} 输出: 峰值:5 谷值:1

方法:对于数组的每个元素,检查当前元素是波峰(元素必须大于其相邻元素)还是波谷(元素必须小于其相邻元素)。 注意数组的第一个和最后一个元素将有一个邻居。

下面是上述方法的实现:

c

// c   implementation of the approach
#include 
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

python 3

# 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

java 描述语言


output: 

peaks : 10 7 5 
troughs : 5 5 3