📌  相关文章
📜  对的绝对差至少为 Subsequence 的最大值的最长子序列

📅  最后修改于: 2022-05-13 01:56:10.055000             🧑  作者: Mango

对的绝对差至少为 Subsequence 的最大值的最长子序列

给定一个长度为N的数组arr[] 。任务是找到数组的最长子序列的长度,使得任何一对元素之间的绝对差值大于或等于该子序列中的最大元素。

例子:

方法:该问题的解决方案基于以下观察。

上述观察可以通过对数组进行排序并找到满足问题陈述给定条件的最长序列来实现。请按照以下步骤操作:

  • 按降序排列数组。
  • 使用等于给定N的值初始化answer变量。
  • 开始迭代数组,并通过检查任何对之间的差异是否大于或等于该子序列的最大元素来计算子序列有多少元素不能考虑。
  • 返回(答案 - 元素计数)

下面是上述方法的实现

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to get longest subsequence
int solve(int arr[], int n)
{
 
    // Sort the array in descending order
    sort(arr, arr + n, greater());
 
    // Initialize answer variable equal
    // to value of N
    int answer = n;
 
    // Count of elements
    // that wont be included in
    // the longest subsequence
    int j = 0;
 
    // Traversing the array
    for (int i = 0; i < n; i++) {
 
        if (i + 1 < n) {
 
            // Checking using the given condition
            // and taking count of elements
            // not to be included in the answer
            if (abs(arr[i] - arr[i + 1])
                < arr[j]) {
                j++;
            }
        }
    }
 
    // Printing the final answer
    return answer - j;
}
 
// Driver Code
int main()
{
    int N = 4;
    int arr[] = { -3, 0, 2, 0 };
 
    // Function call
    int ans = solve(arr, N);
    cout << ans;
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to reverse the array
  static void reverse(int a[], int n){
    int i, k, t;
    for (i = 0; i < n / 2; i++) {
      t = a[i];
      a[i] = a[n - i - 1];
      a[n - i - 1] = t;
    }
  }
 
  // Function to get longest subsequence
  static int solve(int arr[], int n)
  {
 
    // Sort the array in descending order
    Arrays.sort(arr);
    //Now reverse the array
    reverse(arr, n);
 
    // Initialize answer variable equal
    // to value of N
    int answer = n;
 
    // Count of elements
    // that wont be included in
    // the longest subsequence
    int j = 0;
 
    // Traversing the array
    for (int i = 0; i < n; i++) {
 
      if (i + 1 < n) {
 
        // Checking using the given condition
        // and taking count of elements
        // not to be included in the answer
        if (Math.abs(arr[i] - arr[i + 1])
            < arr[j]) {
          j++;
        }
      }
    }
 
    // Printing the final answer
    return answer - j;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 4;
    int arr[] = { -3, 0, 2, 0 };
 
    // Function call
    int ans = solve(arr, N);
    System.out.println(ans);
 
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python 3 code for the above approach
 
# Function to get longest subsequence
def solve(arr, n):
 
    # Sort the array in descending order
    arr.sort()
    arr.reverse()
 
    # Initialize answer variable equal
    # to value of N
    answer = n
 
    # Count of elements
    # that wont be included in
    # the longest subsequence
    j = 0
 
    # Traversing the array
    for i in range(n):
 
        if (i + 1 < n):
 
            # Checking using the given condition
            # and taking count of elements
            # not to be included in the answer
            if (abs(arr[i] - arr[i + 1])
                    < arr[j]):
                j += 1
 
    # Printing the final answer
    return answer - j
 
# Driver Code
if __name__ == "__main__":
 
    N = 4
    arr = [-3, 0, 2, 0]
 
    # Function call
    ans = solve(arr, N)
    print(ans)
 
    # This code is contributed by ukasp.


C#
// C# code for the above approach
using System;
class GFG {
 
  // Function to reverse the array
  static void reverse(int[] a, int n)
  {
    int i, k, t;
    for (i = 0; i < n / 2; i++) {
      t = a[i];
      a[i] = a[n - i - 1];
      a[n - i - 1] = t;
    }
  }
 
  // Function to get longest subsequence
  static int solve(int[] arr, int n)
  {
 
    // Sort the array in descending order
    Array.Sort(arr);
 
    // Now reverse the array
    reverse(arr, n);
 
    // Initialize answer variable equal
    // to value of N
    int answer = n;
 
    // Count of elements
    // that wont be included in
    // the longest subsequence
    int j = 0;
 
    // Traversing the array
    for (int i = 0; i < n; i++) {
 
      if (i + 1 < n) {
 
        // Checking using the given condition
        // and taking count of elements
        // not to be included in the answer
        if (Math.Abs(arr[i] - arr[i + 1])
            < arr[j]) {
          j++;
        }
      }
    }
 
    // Printing the final answer
    return answer - j;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 4;
    int[] arr = { -3, 0, 2, 0 };
 
    // Function call
    int ans = solve(arr, N);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
3

时间复杂度: O(N*logN)
辅助空间: O(1)