给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是找到给定数组的最长子序列,使得子序列中最大和最小元素之间的差正好是K 。
例子:
Input: arr[] = {1, 3, 2, 2, 5, 2, 3, 7}, K = 1
Output: 5
Explanation:
The longest subsequence whose difference between the maximum and minimum element is K(= 1) is {3, 2, 2, 2, 3}.
Therefore, the length is 5.
Input: arr [] = {4, 3, 3, 4}, K = 4
Output: 0
朴素方法:最简单的方法是生成给定数组的所有可能子序列,并为每个子序列找出子序列中最大值和最小值之间的差值。如果它等于K ,则更新结果最长子序列长度。检查所有子序列后,打印获得的最大长度。
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是基于这样的观察,即在所需的子序列中,只能存在两个唯一元素,它们的差应为K 。这个问题可以通过Hashing来解决,存储每个数组元素的频率。请按照以下步骤解决问题:
- 初始化一个变量,比如ans ,以存储最长子序列的长度。
- 初始化一个哈希图,比如M ,它存储数组元素的频率。
- 使用变量i遍历数组arr[] ,对于每个数组元素arr[i] ,将arr[]在M 中的频率增加1 。
- 现在以M遍历散列映射M和用于每个键(说X),如果(X + K)也存在于所述M,则ANS的值更新为最大ANS的和两个键的值的总和.
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find longest subsequence
// having absolute difference between
// maximum and minimum element K
void longestSubsequenceLength(int arr[],
int N, int K)
{
// Stores the frequency of each
// array element
unordered_map um;
// Traverse the array arr[]
for (int i = 0; i < N; i++)
// Increment um[arr[i]] by 1
um[arr[i]]++;
// Store the required answer
int ans = 0;
// Traverse the hashmap
for (auto it : um) {
// Check if it.first + K
// exists in the hashmap
if (um.find(it.first + K)
!= um.end()) {
// Update the answer
ans = max(ans,
it.second
+ um[it.first + K]);
}
}
// Print the result
cout << ans;
}
// Driver Code
int main()
{
int arr[] = { 1, 3, 2, 2, 5, 2, 3, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 1;
longestSubsequenceLength(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find longest subsequence
// having absolute difference between
// maximum and minimum element K
static void longestSubsequenceLength(int []arr,
int N, int K)
{
// Stores the frequency of each
// array element
Map um = new HashMap();
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
if (um.containsKey(arr[i]))
um.put(arr[i], um.get(arr[i]) + 1);
else
um.put(arr[i], 1);
}
// Store the required answer
int ans = 0;
// Traverse the hashmap
for(Map.Entry e : um.entrySet())
{
// Check if it.first + K
// exists in the hashmap
if (um.containsKey(e.getKey() + K))
{
// Update the answer
ans = Math.max(ans, e.getValue() +
um.get(e.getKey() + K));
}
}
// Print the result
System.out.println(ans);
}
// Driver Code
public static void main(String args[])
{
int []arr = { 1, 3, 2, 2, 5, 2, 3, 7 };
int N = arr.length;
int K = 1;
longestSubsequenceLength(arr, N, K);
}
}
// This code is contributed by bgangwar59
Python3
# Python3 program for the above approach
from collections import defaultdict
# Function to find longest subsequence
# having absolute difference between
# maximum and minimum element K
def longestSubsequenceLength(arr, N, K):
# Stores the frequency of each
# array element
um = defaultdict(int)
# Traverse the array arr[]
for i in range(N):
# Increment um[arr[i]] by 1
um[arr[i]] += 1
# Store the required answer
ans = 0
# Traverse the hashmap
for it in um.keys():
# Check if it.first + K
# exists in the hashmap
if (it + K) in um:
# Update the answer
ans = max(ans,
um[it]
+ um[it + K])
# Print the result
print(ans)
# Driver Code
if __name__ == "__main__":
arr = [1, 3, 2, 2, 5, 2, 3, 7]
N = len(arr)
K = 1
longestSubsequenceLength(arr, N, K)
# This code is contributed by chitranayal.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find longest subsequence
// having absolute difference between
// maximum and minimum element K
static void longestSubsequenceLength(int[] arr,
int N, int K)
{
// Stores the frequency of each
// array element
Dictionary um = new Dictionary();
// Traverse the array
for(int i = 0; i < N; i++)
{
// Increase the counter of
// the array element by 1
int count = um.ContainsKey(arr[i]) ? um[arr[i]] : 0;
if (count == 0)
{
um.Add(arr[i], 1);
}
else
{
um[arr[i]] = count + 1;
}
}
// Store the required answer
int ans = 0;
// Traverse the hashmap
foreach(KeyValuePair it in um)
{
// Check if it.first + K
// exists in the hashmap
if (um.ContainsKey(it.Key + K))
{
// Update the answer
ans = Math.Max(ans, (it.Value + um[it.Key + K]));
}
}
// Print the result
Console.Write(ans);
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 3, 2, 2, 5, 2, 3, 7 };
int N = arr.Length;
int K = 1;
longestSubsequenceLength(arr, N, K);
}
}
// This code is contributed by splevel62.
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。