- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find maximum sum of the subsequence, where difference of two values is same as their position difference in Python

Suppose we have a list of numbers called nums, we select a subsequence of strictly increasing values, where the differences of each two numbers is the same as the differences of their two indices. So we have to find the maximum sum of such a subsequence.

So, if the input is like nums = [6, 7, 9, 9, 8, 5], then the output will be 22, as we select the subsequence [6, 7, 9] whose indices are [0, 1, 3]. The differences between each consecutive numbers is [1, 2] which is same as the differences of their indices.

To solve this, we will follow these steps −

d := an empty map

for each index i and value x in nums, do

d[x − i] := d[x − i] + x

return maximum of all values in d

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, nums): from collections import defaultdict d = defaultdict(int) for i, x in enumerate(nums): d[x − i] += x return max(d.values()) ob1 = Solution() nums = [6, 7, 9, 9, 8, 5] print(ob1.solve(nums))

## Input

[6, 7, 9, 9, 8, 5]

## Output

22

- Related Questions & Answers
- Program to find two pairs of numbers where difference between sum of these pairs are minimized in python
- Program to find minimum number of subsequence whose concatenation is same as target in python
- C program to find sum and difference of two numbers
- Program to find maximum sum of two sets where sums are equal in C++
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Program to find length of longest arithmetic subsequence with constant difference in Python
- Program to Find the longest subsequence where the absolute difference between every adjacent element is at most k in Python.
- Program to find maximum sum of two non-overlapping sublists in Python
- Maximum sum of pairs with specific difference C++ program
- Program to find the maximum difference between the index of any two different numbers in C++
- Python program to Sort a List of Dictionaries by the Sum of their Values
- Program to find closest subsequence sum in Python
- Find the sum of maximum difference possible from all subset of a given array in Python
- C++ program to find range whose sum is same as n
- Python program to find difference between two timestamps

Advertisements