Smallest subsequence with sum of absolute difference of consecutive elements maximized | C ++ 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 Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getSubsequence ( vector < int > & arr , int n ) { vector < int > req ; req . push_back ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . push_back ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . push_back ( arr [ i ] ) ; } req . push_back ( arr [ n - 1 ] ) ; cout << req . size ( ) << endl ; for ( auto x : req ) cout << x << " ▁ " ; } int main ( ) { vector < int > arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . size ( ) ; getSubsequence ( arr , n ) ; return 0 ; }