Subsequence with maximum pairwise absolute difference and minimum size
Last Updated :
14 Jun, 2021
Given an array arr[] of N integers, the task is to print the subsequence from the given array with the maximum pairwise absolute difference of adjacent elements. If multiple such subsequences exist, then print the subsequence with the smallest length.
Examples:
Input: arr[] = {1, 2, 4, 3, 5}
Output: 1 4 3 5
Explanation:
For the subsequence {1, 4, 3, 5},
The absolute difference between adjacent pair is |1 – 4| + |4 – 3| + |3 – 5| = 6, which is the maximum possible absolute difference.
Input: arr[] = {1, 2, 5, 6, 3, 4}
Output: {1, 6, 4}
Explanation:
For the subsequence {1, 6, 4},
The absolute difference between adjacent pair is |1 – 6| + |6 – 4| = 7, which is the maximum possible absolute difference.
Approach: For the subsequence with the maximum absolute difference, below are the steps:
- Create the new array(say ans[]) to store all the elements of the required subsequence.
- Insert the first element of the array arr[].
- Find all the local minima and maxima of the array excluding the first and last element of the array and insert, in the array ans[].
- Insert the last element of the array arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getSubsequence(vector< int > ar)
{
int N = ar.size();
vector< int > ans;
ans.push_back(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.push_back(ar[i]);
}
}
else {
if (i < N - 1
&& ar[i + 1] < ar[i]) {
continue ;
}
else {
ans.push_back(ar[i]);
}
}
}
ans.push_back(ar[N - 1]);
for ( auto & it : ans)
cout << it << ' ' ;
}
int main()
{
vector< int > arr = { 1, 2, 4, 3, 5 };
getSubsequence(arr);
}
|
Java
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);
}
}
|
Python3
def getSubsequence(ar):
N = len (ar)
ans = []
ans.append(ar[ 0 ])
for i in range ( 1 , N - 1 ):
if (ar[i] > ar[i - 1 ]):
if (i < N - 1 and
ar[i] < = ar[i + 1 ]):
continue
else :
ans.append(ar[i])
else :
if (i < N - 1 and
ar[i + 1 ] < ar[i]):
continue
else :
ans.append(ar[i])
ans.append(ar[N - 1 ])
for it in ans:
print (it, end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 3 , 5 ]
getSubsequence(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void getSubsequence( int []ar)
{
int N = ar.Length;
List< int > ans = new List< int >();
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]);
foreach ( int it in ans)
Console.Write(it + " " );
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 4, 3, 5 };
getSubsequence(arr);
}
}
|
Javascript
<script>
function getSubsequence(ar)
{
let N = ar.length;
let ans = [];
ans.push(ar[0]);
for (let i = 1; i < N - 1; i++)
{
if (ar[i] > ar[i - 1])
{
if (i < N - 1 && ar[i] <= ar[i + 1])
{
continue ;
}
else
{
ans.push(ar[i]);
}
}
else
{
if (i < N - 1 && ar[i + 1] < ar[i])
{
continue ;
}
else
{
ans.push(ar[i]);
}
}
}
ans.push(ar[N - 1]);
for (let it = 0; it < ans.length; it++)
document.write(ans[it] + " " );
}
let arr = [ 1, 2, 4, 3, 5 ];
getSubsequence(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...