对的绝对差至少为 Subsequence 的最大值的最长子序列
给定一个长度为N的数组arr[] 。任务是找到数组的最长子序列的长度,使得任何一对元素之间的绝对差值大于或等于该子序列中的最大元素。
例子:
Input: N = 6, arr[] = {1, 1, 0, 0, 0, 0}
Output: 4
Explanation: Considering 0 as max element of subsequence, longest array can be made
by choosing elements from arr3 to arr6 => {0, 0, 0, 0}.
Thus, Length of above subsequence = 4.
Input: N = 4, arr[] = {-3, 0, 2, 0}
Output: 3
方法:该问题的解决方案基于以下观察。
- Try to include as many negative and 0 value elements as possible because in an array of all negative and zero value elements the absolute difference of any two pairs would always be greater than or equal to zero and the maximum element in that subsequence would be less than or equal to zero.
- Now, the focus should be to add only one positive element if possible, in the subsequence because on considering a number of positive elements greater than or equal to 2, a scenario might come when both positive elements would be in consideration as a pair then their absolute difference would be less than the maximum element(which would be one of the taken positive element).
上述观察可以通过对数组进行排序并找到满足问题陈述给定条件的最长序列来实现。请按照以下步骤操作:
- 按降序排列数组。
- 使用等于给定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)