Subsequence with maximum pairwise absolute difference and minimum size | Function to find the subsequence with maximum absolute difference ; To store the resultant subsequence ; First element should be included in the subsequence ; Traverse the given array arr [ ] ; If current element is greater than the previous element ; If the current element is not the local maxima then continue ; Else push it in subsequence ; If the current element is less then the previous element ; If the current element is not the local minima then continue ; Else push it in subsequence ; Last element should also be included in subsequence ; Print the element ; Given array ; Function Call
function getSubsequence ( ar ) { let N = ar . length ; let ans = [ ] ; ans . push ( ar [ 0 ] ) ; for ( let i = 1 ; i < N - 1 ; i ++ ) { if ( ar [ i ] > ar [ i - 1 ] ) { if ( i < N - 1 && ar [ i ] <= ar [ i + 1 ] ) { continue ; } else { ans . push ( ar [ i ] ) ; } } else { if ( i < N - 1 && ar [ i + 1 ] < ar [ i ] ) { continue ; } else { ans . push ( ar [ i ] ) ; } } } ans . push ( ar [ N - 1 ] ) ; for ( let it = 0 ; it < ans . length ; it ++ ) document . write ( ans [ it ] + " " ) ; } let arr = [ 1 , 2 , 4 , 3 , 5 ] ; getSubsequence ( arr ) ;