Subsequence with maximum pairwise absolute difference and minimum size | Java program for the above approach ; 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
import java . util . * ; class GFG { static void getSubsequence ( int [ ] ar ) { int N = ar . length ; Vector < Integer > ans = new Vector < Integer > ( ) ; ans . add ( ar [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( ar [ i ] > ar [ i - 1 ] ) { if ( i < N - 1 && ar [ i ] <= ar [ i + 1 ] ) { continue ; } else { ans . add ( ar [ i ] ) ; } } else { if ( i < N - 1 && ar [ i + 1 ] < ar [ i ] ) { continue ; } else { ans . add ( ar [ i ] ) ; } } } ans . add ( ar [ N - 1 ] ) ; for ( int it : ans ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 3 , 5 } ; getSubsequence ( arr ) ; } }