Smallest subsequence with sum of absolute difference of consecutive elements maximized | Function to print the smallest subsequence and its sum ; Final subsequence ; First element is a default endpoint ; Iterating through the array ; Check for monotonically increasing endpoint ; Check for monotonically decreasing endpoint ; Last element is a default endpoint ; Length of final subsequence ; Print the subsequence ; Driver code
def getSubsequence ( arr , n ) : NEW_LINE INDENT req = [ ] NEW_LINE req . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] and arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT req . append ( arr [ i ] ) NEW_LINE DEDENT elif ( arr [ i ] < arr [ i + 1 ] and arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT req . append ( arr [ i ] ) ; NEW_LINE DEDENT DEDENT req . append ( arr [ n - 1 ] ) ; NEW_LINE print ( len ( req ) ) NEW_LINE for x in req : NEW_LINE INDENT print ( x , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 3 , 6 , 7 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE getSubsequence ( arr , n ) NEW_LINE DEDENT