
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- 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
- Program to find maximum difference of adjacent values after deleting k numbers in python
- The sum of two numbers is 8. If their sum is four times their difference, find the numbers.
- Program to Find the longest subsequence where the absolute difference between every adjacent element is at most k in Python.
- Program to find length of longest arithmetic subsequence with constant difference in Python
- Two A.P.s have the same common difference. The first term of one A.P. is 2 and that of the other is 7. The difference between their 10th terms is the same as the difference between their 21st terms, which is the same as the difference between any two corresponding terms. Why?
- Program to find maximum sum of two sets where sums are equal in C++
- Program to find closest subsequence sum in Python
- Program to find maximum sum of two non-overlapping sublists in Python
- C program to find sum and difference of two numbers
- Two APs have the same common difference. The first term of one AP is 2 and that of the other is 7 . The difference between their \( 10^{\text {th }} \) terms is the same as the difference between their \( 21^{\text {st }} \) terms, which is the same as the difference between any two corresponding terms. Why?
- Maximum sum alternating subsequence in C++ program
- Program to find minimum absolute sum difference in Python
- The sum of two numbers is 1000 and the difference between their square is 256000. Find the numbers.

Advertisements