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
using System ; class GFG { static 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 = Math . Max ( ma , Math . Abs ( a [ i ] - a [ i + 1 ] ) ) ; else return false ; } Console . Write ( " Amplitude ▁ = ▁ " + ma ) ; Console . WriteLine ( ) ; return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 1 , 5 , 0 , 7 , - 6 } ; int n = a . Length ; int wave = ( n - 1 ) / 2 ; if ( check ( a , n ) ) Console . Write ( " Waves ▁ = ▁ " + wave ) ; else Console . Write ( " - 1" ) ; } }