📜  其索引之间的差异等于其值之间的差异的子序列的最大和

📅  最后修改于: 2021-10-27 07:41:18             🧑  作者: Mango

给定一个大小为N的数组A[] ,任务是找到子序列的最大和,使得对于子序列中存在的每一对,它们在原始数组中的索引之间的差异等于它们的值之间的差异。

例子:

方法:对于具有索引ij且值为A[i]A[j] 的两个元素如果i – j等于A[i] – A[j],A[i] – i等于A[j] – j 。因此,有效子序列将具有相同的A[i] – i 值。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0,以存储所需子序列可能的最大和。
  • 初始化一个映射,比如mp,以存储每个A[i] – i 的值
  • 使用变量在[0, N – 1]范围内迭代,例如i 
    • A[i]添加到mp[A[i] – i]。
    • ans更新为ansmp[A[i] – i]的最大值
  • 最后,打印ans

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum sum of
// a subsequence having difference between
// indices equal to difference in their values
void maximumSubsequenceSum(int A[], int N)
{
    // Stores the maximum sum
    int ans = 0;
 
    // Stores the value for each A[i] - i
    map mp;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update the value in map
        mp[A[i] - i] += A[i];
 
        // Update the answer
        ans = max(ans, mp[A[i] - i]);
    }
 
    // Finally, print the answer
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    // Given Input
    int A[] = { 10, 7, 1, 9, 10, 1 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    maximumSubsequenceSum(A, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.HashMap;
public class GFG
{
 
    // Function to find the maximum sum of
    // a subsequence having difference between
    // indices equal to difference in their values
    static void maximumSubsequenceSum(int A[], int N)
    {
       
        // Stores the maximum sum
        int ans = 0;
 
        // Stores the value for each A[i] - i
        HashMap mp = new HashMap<>();
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
 
            // Update the value in map
            mp.put(A[i] - i,
                   mp.getOrDefault(A[i] - i, 0) + A[i]);
 
            // Update the answer
            ans = Math.max(ans, mp.get(A[i] - i));
        }
 
        // Finally, print the answer
        System.out.println(ans);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Given Input
        int A[] = { 10, 7, 1, 9, 10, 1 };
        int N = A.length;
 
        // Function Call
        maximumSubsequenceSum(A, N);
    }
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find the maximum sum of
# a subsequence having difference between
# indices equal to difference in their values
def maximumSubsequenceSum(A, N):
     
    # Stores the maximum sum
    ans = 0
 
    # Stores the value for each A[i] - i
    mp = {}
 
    # Traverse the array
    for i in range(N):
        if (A[i] - i in mp):
             
            # Update the value in map
            mp[A[i] - i] += A[i]
        else:
            mp[A[i] - i] = A[i]
             
        # Update the answer
        ans = max(ans, mp[A[i] - i])
 
    # Finally, print the answer
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    A = [ 10, 7, 1, 9, 10, 1 ]
    N = len(A)
 
    # Function Call
    maximumSubsequenceSum(A, N)
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System.Collections.Generic;
using System;
 
class GFG{
 
// Function to find the maximum sum of
// a subsequence having difference between
// indices equal to difference in their values
static void maximumSubsequenceSum(int []A, int N)
{
     
    // Stores the maximum sum
    int ans = 0;
 
    // Stores the value for each A[i] - i
    Dictionary mp = new Dictionary();
                                         
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Update the value in map
        if (mp.ContainsKey(A[i] - i))
            mp[A[i] - i] += A[i];
        else
            mp[A[i] - i] = A[i]; 
             
        // Update the answer
        ans = Math.Max(ans, mp[A[i] - i]);
    }
 
    // Finally, print the answer
    Console.Write(ans);
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given Input
    int []A = { 10, 7, 1, 9, 10, 1 };
    int N = A.Length;
 
    // Function Call
    maximumSubsequenceSum(A, N);
}
}
 
// This code is contributed by amreshkumar3


Javascript


输出:
26

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程