Find the amplitude and number of waves for the given array

Given an array arr[] of N integers, the task is to find the amplitude and number of waves for the given array. If the array is not a wave array then print -1.
Wave Array: An array is a wave array if it is continuously strictly increasing and decreasing or vice-versa.Â
Amplitude is defined as the maximum difference of consecutive numbers.Â
Â
Examples:Â
Input: arr[] = {1, 2, 1, 5, 0, 7, -6}Â
Output: Amplitude = 13, Waves = 3Â
Explanation:Â
For the array observe the pattern 1->2 (increase), 2->1 (decrease), 1->5 (increase), 5->0 (decrease), 0->7 (increase), 7->-6 (decrease). Amplitude = 13 (between 7 and -6) and total waves = 3
Input: arr[] = {1, 2, 1, 5, 0, 7, 7}Â
Output: -1Â
Explanation:Â
The array is not waved array as the last two elements of the array are equal, hence the answer is -1. Â
Approach:Â
The idea is to check for both sides adjacent elements where both must be either less or greater than the current element. If this condition is satisfied then count the number of waves otherwise print -1, where the number of waves is (n – 1) / 2. While traversing the array keep updating the maximum difference between the consecutive element to get the amplitude of the given wave array.Â
Â
Below is the implementation of the above approach:Â
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the amplitude and// number of waves for the given arraybool check(int a[], int n){Â Â Â Â int ma = a[1] - a[0];Â
    // Check for both sides adjacent    // elements that both must be less    // or both must be greater    // than current element    for (int i = 1; i < n - 1; i++) {Â
        if ((a[i] > a[i - 1]             && a[i + 1] < a[i])            || (a[i] < a[i - 1]                && a[i + 1] > a[i]))Â
            // Update amplitude with max value            ma = max(ma, abs(a[i] - a[i + 1]));Â
        else            return false;    }Â
    // Print the Amplitude    cout << "Amplitude = " << ma;    cout << endl;    return true;}Â
// Driver Codeint main(){Â Â Â Â // Given array a[]Â Â Â Â int a[] = { 1, 2, 1, 5, 0, 7, -6 };Â Â Â Â int n = sizeof a / sizeof a[0];Â
    // Calculate number of waves    int wave = (n - 1) / 2;Â
    // Function Call    if (check(a, n))        cout << "Waves = " << wave;    else        cout << "-1";Â
    return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{Â
// Function to find the amplitude and// number of waves for the given arraystatic boolean check(int a[], int n){Â Â Â Â int ma = a[1] - a[0];Â
    // Check for both sides adjacent    // elements that both must be less    // or both must be greater    // than current element    for (int i = 1; i < n - 1; i++)     {        if ((a[i] > a[i - 1] &&              a[i + 1] < a[i]) ||             (a[i] < a[i - 1] &&              a[i + 1] > a[i]))Â
            // Update amplitude with max value            ma = Math.max(ma, Math.abs(a[i] - a[i + 1]));Â
        else            return false;    }Â
    // Print the Amplitude    System.out.print("Amplitude = " + ma);    System.out.println();    return true;}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â // Given array a[]Â Â Â Â int a[] = { 1, 2, 1, 5, 0, 7, -6 };Â Â Â Â int n = a.length;Â
    // Calculate number of waves    int wave = (n - 1) / 2;Â
    // Function Call    if (check(a, n))        System.out.print("Waves = " + wave);    else        System.out.print("-1");}}Â
// This code is contributed by sapnasingh4991 |
Python3
# Python3 program for the above approachÂ
# Function to find the amplitude and# number of waves for the given arraydef check(a, n):Â Â Â Â ma = a[1] - a[0]Â
    # Check for both sides adjacent    # elements that both must be less    # or both must be greater    # than current element    for i in range(1, n - 1):Â
        if ((a[i] > a[i - 1] and             a[i + 1] < a[i]) or            (a[i] < a[i - 1] and             a[i + 1] > a[i])):Â
            # Update amplitude with max value            ma = max(ma, abs(a[i] - a[i + 1]))Â
        else:            return FalseÂ
    # Print the Amplitude    print("Amplitude = ", ma)    return True   # Driver Codeif __name__ == '__main__':       # Given array a[]    a = [1, 2, 1, 5, 0, 7, -6]    n = len(a)Â
    # Calculate number of waves    wave = (n - 1) // 2Â
    # Function Call    if (check(a, n)):        print("Waves = ",wave)    else:        print("-1")Â
# This code is contributed by Mohit Kumar |
C#
// C# program for the above approachusing System;class GFG{Â
// Function to find the amplitude and// number of waves for the given arraystatic bool check(int []a, int n){Â Â Â Â int ma = a[1] - a[0];Â
    // Check for both sides adjacent    // elements that both must be less    // or both must be greater    // than current element    for (int i = 1; i < n - 1; i++)     {        if ((a[i] > a[i - 1] &&              a[i + 1] < a[i]) ||             (a[i] < a[i - 1] &&              a[i + 1] > a[i]))Â
            // Update amplitude with max value            ma = Math.Max(ma, Math.Abs(a[i] - a[i + 1]));        else            return false;    }Â
    // Print the Amplitude    Console.Write("Amplitude = " + ma);    Console.WriteLine();    return true;}Â
// Driver Codepublic static void Main(String[] args){    // Given array []a    int []a = { 1, 2, 1, 5, 0, 7, -6 };    int n = a.Length;Â
    // Calculate number of waves    int wave = (n - 1) / 2;Â
    // Function Call    if (check(a, n))        Console.Write("Waves = " + wave);    else        Console.Write("-1");}}Â
// This code is contributed by sapnasingh4991 |
Javascript
<script>// JavaScript program for the above approach    // Function to find the amplitude and// number of waves for the given arrayfunction check(a, n){    let ma = a[1] - a[0];       // Check for both sides adjacent    // elements that both must be less    // or both must be greater    // than current element    for (let i = 1; i < n - 1; i++)     {        if ((a[i] > a[i - 1] &&              a[i + 1] < a[i]) ||             (a[i] < a[i - 1] &&              a[i + 1] > a[i]))               // Update amplitude with max value            ma = Math.max(ma, Math.abs(a[i] - a[i + 1]));           else            return false;    }       // Print the Amplitude    document.write("Amplitude = " + ma);    document.write("<br/>");    return true;}Â
// Driver Code                // Given array a[]    let a = [ 1, 2, 1, 5, 0, 7, -6 ];    let n = a.length;       // Calculate number of waves    let wave = (n - 1) / 2;       // Function Call    if (check(a, n))        document.write("Waves = " + wave);    else        document.write("-1");              </script> |
Amplitude = 13 Waves = 3
Â
Time Complexity: O(N)Â
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



