Find the amplitude and number of waves for the given array | 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 ; Prlet the Amplitude ; Given array a [ ] ; Calculate number of waves ; Function Call
function check ( a , n ) { let ma = a [ 1 ] - a [ 0 ] ; 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 ] ) ) ma = Math . max ( ma , Math . abs ( a [ i ] - a [ i + 1 ] ) ) ; else return false ; } document . write ( " " + ma ) ; document . write ( " " ) ; return true ; } let a = [ 1 , 2 , 1 , 5 , 0 , 7 , - 6 ] ; let n = a . length ; let wave = ( n - 1 ) / 2 ; if ( check ( a , n ) ) document . write ( " " + wave ) ; else document . write ( " " ) ;