Find the amplitude and number of waves for the given array | C ++ program for the above approach ; Function to find the amplitude and number of waves for the given array ; Check for both sides adjacent elements that both must be less or both must be greater than current element ; Update amplitude with max value ; Print the Amplitude ; Driver Code ; Given array a [ ] ; Calculate number of waves ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int n ) { int ma = a [ 1 ] - a [ 0 ] ; 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 ] ) ) ma = max ( ma , abs ( a [ i ] - a [ i + 1 ] ) ) ; else return false ; } cout << " Amplitude ▁ = ▁ " << ma ; cout << endl ; return true ; } int main ( ) { int a [ ] = { 1 , 2 , 1 , 5 , 0 , 7 , -6 } ; int n = sizeof a / sizeof a [ 0 ] ; int wave = ( n - 1 ) / 2 ; if ( check ( a , n ) ) cout << " Waves ▁ = ▁ " << wave ; else cout << " - 1" ; return 0 ; }