Maximize the sum of differences of consecutive elements after removing exactly K elements | C ++ implementation of the approach ; Function to return the maximized sum ; Remove any k internal elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int * arr , int n , int k ) { if ( k <= n - 2 ) return ( arr [ n - 1 ] - arr [ 0 ] ) ; return 0 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 1 ; cout << findSum ( arr , n , k ) ; return 0 ; }