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 ; Driver Code ; Given array ; Function Call
def getSubsequence ( ar ) : NEW_LINE INDENT N = len ( ar ) NEW_LINE ans = [ ] NEW_LINE ans . append ( ar [ 0 ] ) NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT if ( ar [ i ] > ar [ i - 1 ] ) : NEW_LINE INDENT if ( i < N - 1 and ar [ i ] <= ar [ i + 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( ar [ i ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( i < N - 1 and ar [ i + 1 ] < ar [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( ar [ i ] ) NEW_LINE DEDENT DEDENT DEDENT ans . append ( ar [ N - 1 ] ) NEW_LINE for it in ans : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 3 , 5 ] NEW_LINE getSubsequence ( arr ) NEW_LINE DEDENT