Given an array **arr[]** of length **N**, containing values in the range **[1, N]**, the task is to find a subsequence **s _{1}, s_{2}, …, s_{k}** such that

is

**maximised**. In case of multiple subsequences having maximum possible sum, print the smallest one.

Input:N = 3, P = {3, 2, 1}

Output:K = 2, S = {3, 1}

Explanation:

Maximum sum possible = 2

Subsequences {3, 2, 1} and {3, 1} achieves that sum.

Hence, the subsequence {3, 1} is considered being of smaller length.

Input:N = 4, P = {1, 3, 4, 2}

Output:K = 3, S = {1, 4, 2}

Explanation:

Maximum sum possible = 5

Subsequences {1, 3, 4, 2} and {1, 4, 2} achieves that sum.

Hence, the subsequence {1, 4, 2} is considered being of smaller length.

**Naive Approach:**

Generate all subsequences of length >= 2 and calculate their respective **sums**. Keep track of the maximum sum obtained for any subsequence. In the case of multiple subsequences having the maximum sum, keep updating the minimum length, and maintain that subsequence. Finally print the subsequence.

**Time Complexity:** O(2^{N})

**Efficient Approach:**

There are a few key observations that will help us proceed:

- The maximum sum will be obtained when all the elements in the permutation are considered.

For example:**N**= 4,**P**= [1, 3, 4, 2]

**Max sum**= | 1 – 3 | + | 3 – 4 | + | 4 – 2 | = 2 + 1 + 2 = 5

Here, the length is**N**and the required subsequence is permutation**P**itself.Now that we know the maximum possible sum, the objective is to minimize the subsequence length without affecting this maximum sum.

- For a monotonically increasing or decreasing subsequence, maximum sum can be acheived by only considering the First and Last element, for example:

**S**= [1, 3, 5, 7]

**Max sum**= | 1 – 3 | + | 3 – 5 | + | 5 – 7 | = 2 + 2 + 2 = 6,**K**= 4

Considering only first and last element,

**S**= [1, 7]

**Max sum**= | 1 – 7 | = 6,**K**= 2

In this way, the length of the subsequence can be reduced without affecting the **Max sum**.

Hence, from the given array, keep extracting the end-points of monotonically increasing or decreasing subsequences, and add them to the subsequence in the following way:

**first**and the

**last**element of the permutation are default endpoints

**P[ i ]**is a

**monotonically increasing endpoint**if

**P[ i – 1 ] < P[ i ] > P[ i + 1 ]**

**P[ i ]**is

**monotonically decreasing endpoint**if

**P[ i – 1 ] > P[ i ] < P[ i + 1 ]**

The subsequence thus obtained will have maximum sum and minimum length.

Below is the implementation of the above approach:

## C++

`// C++ program to find ` `// smallest subsequence ` `// with sum of absolute ` `// difference of consecutive ` `// elements maximized ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the smallest ` `// subsequence and its sum ` `void` `getSubsequence(vector<` `int` `>& arr, ` ` ` `int` `n) ` `{ ` ` ` `// Final subsequence ` ` ` `vector<` `int` `> req; ` ` ` ` ` `// First element is ` ` ` `// a default endpoint ` ` ` `req.push_back(arr[0]); ` ` ` ` ` `// Iterating through the array ` ` ` `for` `(` `int` `i = 1; i < n - 1; i++) { ` ` ` ` ` `// Check for monotonically ` ` ` `// increasing endpoint ` ` ` `if` `(arr[i] > arr[i + 1] ` ` ` `&& arr[i] > arr[i - 1]) ` ` ` `req.push_back(arr[i]); ` ` ` ` ` `// Check for monotonically ` ` ` `// decreasing endpoint ` ` ` `else` `if` `(arr[i] < arr[i + 1] ` ` ` `&& arr[i] < arr[i - 1]) ` ` ` `req.push_back(arr[i]); ` ` ` `} ` ` ` ` ` `// Last element is ` ` ` `// a default endpoint ` ` ` `req.push_back(arr[n - 1]); ` ` ` ` ` `// Length of final subsequence ` ` ` `cout << req.size() << endl; ` ` ` ` ` `// Print the subsequence ` ` ` `for` `(` `auto` `x : req) ` ` ` `cout << x << ` `" "` `; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr = { 1, 2, 5, 3, ` ` ` `6, 7, 4 }; ` ` ` `int` `n = arr.size(); ` ` ` `getSubsequence(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find smallest ` `// subsequence with sum of absolute ` `// difference of consecutive ` `// elements maximized ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to print the smallest ` `// subsequence and its sum ` `static` `void` `getSubsequence(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` ` ` `// Final subsequence ` ` ` `Vector<Integer> req = ` `new` `Vector<Integer>(); ` ` ` ` ` `// First element is ` ` ` `// a default endpoint ` ` ` `req.add(arr[` `0` `]); ` ` ` ` ` `// Iterating through the array ` ` ` `for` `(` `int` `i = ` `1` `; i < n - ` `1` `; i++) ` ` ` `{ ` ` ` ` ` `// Check for monotonically ` ` ` `// increasing endpoint ` ` ` `if` `(arr[i] > arr[i + ` `1` `] && ` ` ` `arr[i] > arr[i - ` `1` `]) ` ` ` `req.add(arr[i]); ` ` ` ` ` `// Check for monotonically ` ` ` `// decreasing endpoint ` ` ` `else` `if` `(arr[i] < arr[i + ` `1` `] && ` ` ` `arr[i] < arr[i - ` `1` `]) ` ` ` `req.add(arr[i]); ` ` ` `} ` ` ` ` ` `// Last element is ` ` ` `// a default endpoint ` ` ` `req.add(arr[n - ` `1` `]); ` ` ` ` ` `// Length of final subsequence ` ` ` `System.out.print(req.size() + ` `"\n"` `); ` ` ` ` ` `// Print the subsequence ` ` ` `for` `(` `int` `x : req) ` ` ` `System.out.print(x + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `[]arr = { ` `1` `, ` `2` `, ` `5` `, ` `3` `, ` ` ` `6` `, ` `7` `, ` `4` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `getSubsequence(arr, n); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find smallest ` `// subsequence with sum of absolute ` `// difference of consecutive ` `// elements maximized ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Function to print the smallest ` `// subsequence and its sum ` `static` `void` `getSubsequence(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` ` ` `// Final subsequence ` ` ` `List<` `int` `> req = ` `new` `List<` `int` `>(); ` ` ` ` ` `// First element is ` ` ` `// a default endpoint ` ` ` `req.Add(arr[0]); ` ` ` ` ` `// Iterating through the array ` ` ` `for` `(` `int` `i = 1; i < n - 1; i++) ` ` ` `{ ` ` ` ` ` `// Check for monotonically ` ` ` `// increasing endpoint ` ` ` `if` `(arr[i] > arr[i + 1] && ` ` ` `arr[i] > arr[i - 1]) ` ` ` `req.Add(arr[i]); ` ` ` ` ` `// Check for monotonically ` ` ` `// decreasing endpoint ` ` ` `else` `if` `(arr[i] < arr[i + 1] && ` ` ` `arr[i] < arr[i - 1]) ` ` ` `req.Add(arr[i]); ` ` ` `} ` ` ` ` ` `// Last element is ` ` ` `// a default endpoint ` ` ` `req.Add(arr[n - 1]); ` ` ` ` ` `// Length of readonly subsequence ` ` ` `Console.Write(req.Count + ` `"\n"` `); ` ` ` ` ` `// Print the subsequence ` ` ` `foreach` `(` `int` `x ` `in` `req) ` ` ` `Console.Write(x + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 1, 2, 5, 3, ` ` ` `6, 7, 4 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `getSubsequence(arr, n); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

5 1 5 3 7 4

**Time complexity:** O(N)

## Recommended Posts:

- Absolute Difference of all pairwise consecutive elements in an array
- k-th smallest absolute difference of two elements in an array
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
- Replace elements with absolute difference of smallest element on left and largest element on right
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Longest subsequence such that absolute difference between every pair is atmost 1
- Subsequence with maximum pairwise absolute difference and minimum size
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number
- Count of elements which are second smallest among three consecutive elements
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Subsequence X of length K such that gcd(X[0], X[1]) + (X[2], X[3]) + ... is maximized
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Find zeroes to be flipped so that number of consecutive 1's is maximized
- Absolute Difference of even and odd indexed elements in an Array
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Maximum absolute difference between distinct elements in an Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.