给定一个由N 个整数组成的数组arr[] ,任务是打印给定数组中相邻元素的最大成对绝对差的子序列。如果存在多个这样的子序列,则打印长度最小的子序列。
例子:
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.
处理方法:对于绝对差最大的子序列,步骤如下:
- 创建新数组(比如ans[] )来存储所需子序列的所有元素。
- 插入数组 arr[] 的第一个元素。
- 查找数组的所有局部最小值和最大值,不包括数组的第一个和最后一个元素,并插入数组ans[] 。
- 插入数组arr[]的最后一个元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the subsequence
// with maximum absolute difference
void getSubsequence(vector ar)
{
int N = ar.size();
// To store the resultant subsequence
vector ans;
// First element should be included
// in the subsequence
ans.push_back(ar[0]);
// Traverse the given array arr[]
for (int i = 1; i < N - 1; i++) {
// If current element is greater
// than the previous element
if (ar[i] > ar[i - 1]) {
// If the current element is
// not the local maxima
// then continue
if (i < N - 1
&& ar[i] <= ar[i + 1]) {
continue;
}
// Else push it in subsequence
else {
ans.push_back(ar[i]);
}
}
// If the current element is less
// then the previous element
else {
// If the current element is
// not the local minima
// then continue
if (i < N - 1
&& ar[i + 1] < ar[i]) {
continue;
}
// Else push it in subsequence
else {
ans.push_back(ar[i]);
}
}
}
// Last element should also be
// included in subsequence
ans.push_back(ar[N - 1]);
// Print the element
for (auto& it : ans)
cout << it << ' ';
}
// Driver Code
int main()
{
// Given array
vector arr = { 1, 2, 4, 3, 5 };
// Function Call
getSubsequence(arr);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the subsequence
// with maximum absolute difference
static void getSubsequence(int []ar)
{
int N = ar.length;
// To store the resultant subsequence
Vector ans = new Vector();
// First element should be included
// in the subsequence
ans.add(ar[0]);
// Traverse the given array arr[]
for(int i = 1; i < N - 1; i++)
{
// If current element is greater
// than the previous element
if (ar[i] > ar[i - 1])
{
// If the current element is
// not the local maxima
// then continue
if (i < N - 1 && ar[i] <= ar[i + 1])
{
continue;
}
// Else push it in subsequence
else
{
ans.add(ar[i]);
}
}
// If the current element is less
// then the previous element
else
{
// If the current element is
// not the local minima
// then continue
if (i < N - 1 && ar[i + 1] < ar[i])
{
continue;
}
// Else push it in subsequence
else
{
ans.add(ar[i]);
}
}
}
// Last element should also be
// included in subsequence
ans.add(ar[N - 1]);
// Print the element
for(int it : ans)
System.out.print(it + " ");
}
// Driver Code
public static void main(String[] args)
{
// Given array
int []arr = { 1, 2, 4, 3, 5 };
// Function Call
getSubsequence(arr);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program for the above approach
# Function to find the subsequence
# with maximum absolute difference
def getSubsequence(ar):
N = len(ar)
# To store the resultant subsequence
ans = []
# First element should be included
# in the subsequence
ans.append(ar[0])
# Traverse the given array arr[]
for i in range(1, N - 1):
# If current element is greater
# than the previous element
if (ar[i] > ar[i - 1]):
# If the current element is
# not the local maxima
# then continue
if(i < N - 1 and
ar[i] <= ar[i + 1]):
continue
# Else push it in subsequence
else:
ans.append(ar[i])
# If the current element is less
# then the previous element
else:
# If the current element is
# not the local minima
# then continue
if (i < N - 1 and
ar[i + 1] < ar[i]):
continue
# Else push it in subsequence
else:
ans.append(ar[i])
# Last element should also be
# included in subsequence
ans.append(ar[N - 1])
# Print the element
for it in ans:
print(it, end = " ")
# Driver Code
if __name__ == '__main__':
# Given array
arr = [ 1, 2, 4, 3, 5 ]
# Function Call
getSubsequence(arr)
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the subsequence
// with maximum absolute difference
static void getSubsequence(int []ar)
{
int N = ar.Length;
// To store the resultant subsequence
List ans = new List();
// First element should be included
// in the subsequence
ans.Add(ar[0]);
// Traverse the given array []arr
for(int i = 1; i < N - 1; i++)
{
// If current element is greater
// than the previous element
if (ar[i] > ar[i - 1])
{
// If the current element is
// not the local maxima
// then continue
if (i < N - 1 && ar[i] <= ar[i + 1])
{
continue;
}
// Else push it in subsequence
else
{
ans.Add(ar[i]);
}
}
// If the current element is less
// then the previous element
else
{
// If the current element is
// not the local minima
// then continue
if (i < N - 1 && ar[i + 1] < ar[i])
{
continue;
}
// Else push it in subsequence
else
{
ans.Add(ar[i]);
}
}
}
// Last element should also be
// included in subsequence
ans.Add(ar[N - 1]);
// Print the element
foreach(int it in ans)
Console.Write(it + " ");
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 1, 2, 4, 3, 5 };
// Function Call
getSubsequence(arr);
}
}
// This code is contributed by Princi Singh
输出:
1 4 3 5
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live