📌  相关文章
📜  最长子序列,使得相邻元素之间的差异为 K

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

最长子序列,使得相邻元素之间的差异为 K

给定一个大小为N的数组arr[]和一个整数K ,任务是找到最长的子序列,使得相邻元素之间的差为K

例子:

方法:给定的问题可以使用动态规划来解决。 这个想法是存储在包含当前元素之后结束的相邻元素之间具有差异K的最长子序列的长度。创建一个无序映射mp其中mp[i]表示包含整数i的子序列的最大长度。
因此,得到最大长度子序列的关系可以写成:

映射mp中存储的最大整数是必需的答案。下面是上述方法的实现:

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to find the longest
// subsequence such that difference
// between adjacent elements is K
int longestSubsequence(vector& arr, int K)
{
    // Stores length of longest
    // subsequence in a map
    unordered_map mp;
 
    // Variable to store the maximum
    // length of subsequence
    int mx = 1;
 
    // Loop to iterate through the
    // given array
    for (auto x : arr) {
        mp[x] = 1;
 
        // If (x - K) exists
        if (mp.count(x - K)) {
            mp[x] = 1 + mp[x - K];
        }
 
        // if (x + K) exists
        if (mp.count(x + K)) {
            mp[x] = max(mp[x], 1 + mp[x + K]);
        }
 
        mx = max(mx, mp[x]);
    }
 
    // Return Answer
    return mx;
}
 
// Driver Code
int main()
{
    vector arr = { 1, 2, 3, 4, 5, 3, 2 };
    int K = 1;
 
    cout << longestSubsequence(arr, K);
}


Java
// Java code for the above approach
import java.util.HashMap;
 
class GFG {
 
    // Function to find the longest
    // subsequence such that difference
    // between adjacent elements is K
    public static int longestSubsequence(int[] arr, int K) {
 
        // Stores length of longest
        // subsequence in a map
        HashMap mp = new HashMap();
 
        // Variable to store the maximum
        // length of subsequence
        int mx = 1;
 
        // Loop to iterate through the
        // given array
        for (int x : arr) {
            mp.put(x, 1);
 
            // If (x - K) exists
            if (mp.containsKey(x - K)) {
                mp.put(x, 1 + mp.get(x - K));
            }
 
            // If (x + K) exists
            if (mp.containsKey(x + K)) {
                mp.put(x, Math.max(mp.get(x), 1 + mp.get(x + K)));
            }
            mx = Math.max(mx, mp.get(x));
        }
 
        // Return Answer
        return mx;
    }
 
    // Driver Code
    public static void main(String args[]) {
        int[] arr = { 1, 2, 3, 4, 5, 3, 2 };
        int K = 1;
 
        System.out.print(longestSubsequence(arr, K));
    }
}
 
// This code is contributed by gfgking


Python3
# python code for the above approach
 
# Function to find the longest
# subsequence such that difference
# between adjacent elements is K
def longestSubsequence(arr, K):
 
    # Stores length of longest
    # subsequence in a map
    mp = {}
 
    # Variable to store the maximum
    # length of subsequence
    mx = 1
 
    # Loop to iterate through the
    # given array
    for x in arr:
        mp[x] = 1
 
        # If (x - K) exists
        if ((x - K) in mp):
            mp[x] = 1 + mp[x - K]
 
        # if (x + K) exists
        if ((x+K) in mp):
            mp[x] = max(mp[x], 1 + mp[x + K])
 
        mx = max(mx, mp[x])
 
    # Return Answer
    return mx
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 2, 3, 4, 5, 3, 2]
    K = 1
 
    print(longestSubsequence(arr, K))
 
# This code is contributed by rakeshsahni


C#
// C# code for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the longest
// subsequence such that difference
// between adjacent elements is K
static int longestSubsequence(List arr, int K)
{
     
    // Stores length of longest
    // subsequence in a map
    Dictionary mp = new Dictionary();
 
    // Variable to store the maximum
    // length of subsequence
    int mx = 1;
 
    // Loop to iterate through the
    // given array
    foreach(int x in arr)
    {
        mp[x] = 1;
 
        // If (x - K) exists
        if (mp.ContainsKey(x - K))
        {
            mp[x] = 1 + mp[x - K];
        }
 
        // If (x + K) exists
        if (mp.ContainsKey(x + K))
        {
            mp[x] = Math.Max(mp[x], 1 + mp[x + K]);
        }
        mx = Math.Max(mx, mp[x]);
    }
 
    // Return Answer
    return mx;
}
 
// Driver Code
public static void Main()
{
    List arr = new List(){ 1, 2, 3, 4, 5, 3, 2 };
    int K = 1;
 
    Console.Write(longestSubsequence(arr, K));
}
}
 
// This code is contributed by ukasp


Javascript


输出
6

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