Maximum sum of a subsequence having difference between their indices equal to the difference between their values | 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 ; Given Input ; Function Call
function maximumSubsequenceSum ( A , N ) { var ans = 0 ; var mp = new Map ( ) ; var i ; for ( i = 0 ; i < N ; i ++ ) { if ( mp . has ( A [ i ] - i ) ) mp . set ( A [ i ] - i , mp . get ( A [ i ] - i ) + A [ i ] ) ; else mp . set ( A [ i ] - i , A [ i ] ) ; ans = Math . max ( ans , mp . get ( A [ i ] - i ) ) ; } document . write ( ans ) ; } var A = [ 10 , 7 , 1 , 9 , 10 , 1 ] ; var N = A . length ; maximumSubsequenceSum ( A , N ) ;