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