📌  相关文章
📜  具有最大成对绝对差和最小大小的子序列

📅  最后修改于: 2021-05-18 01:24:24             🧑  作者: Mango

给定N个整数的数组arr [] ,任务是打印给定数组中的子序列,并带有相邻元素的最大成对绝对差。如果存在多个此类子序列,则以最小长度打印该子序列。
例子:

方法:对于具有最大绝对差的子序列,请执行以下步骤:

  1. 创建新数组(例如ans [] )以存储所需子序列的所有元素。
  2. 插入数组arr []的第一个元素。
  3. 找到除数组的第一个和最后一个元素之外的所有数组的局部最小值和最大值,然后将其插入数组ans []中
  4. 插入数组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)