Smallest subsequence with sum of absolute difference of consecutive elements maximized | Java program to find 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
import java . util . * ; class GFG { static void getSubsequence ( int [ ] arr , int n ) { Vector < Integer > req = new Vector < Integer > ( ) ; req . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . add ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . add ( arr [ i ] ) ; } req . add ( arr [ n - 1 ] ) ; System . out . print ( req . size ( ) + "NEW_LINE"); for ( int x : req ) System . out . print ( x + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . length ; getSubsequence ( arr , n ) ; } }