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

Given a list of numbers, we need to find a subsequence where the difference between consecutive values equals the difference between their original indices. Our goal is to find the maximum sum of such a subsequence.

For example, if we have nums = [6, 7, 9, 9, 8, 5], we can select subsequence [6, 7, 9] at indices [0, 1, 3]. The value differences are [1, 2] and index differences are also [1, 2], giving us a sum of 22.

Algorithm

The key insight is that for any valid subsequence, if we have values a and b at indices i and j, then b - a = j - i. Rearranging gives us a - i = b - j.

This means all elements in a valid subsequence will have the same value of element - index. We can group elements by this difference and sum them up ?

Implementation

from collections import defaultdict

def max_subsequence_sum(nums):
    # Dictionary to store sum for each (value - index) difference
    diff_sums = defaultdict(int)
    
    # For each element, calculate (value - index) and add to sum
    for i, value in enumerate(nums):
        diff_key = value - i
        diff_sums[diff_key] += value
    
    # Return maximum sum among all groups
    return max(diff_sums.values())

# Test with example
nums = [6, 7, 9, 9, 8, 5]
result = max_subsequence_sum(nums)
print(f"Maximum subsequence sum: {result}")
Maximum subsequence sum: 22

How It Works

Let's trace through the example step by step ?

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

# Calculate (value - index) for each element
for i, value in enumerate(nums):
    diff = value - i
    print(f"Index {i}, Value {value}, Diff: {diff}")
Index 0, Value 6, Diff: 6
Index 1, Value 7, Diff: 6
Index 2, Value 9, Diff: 7
Index 3, Value 9, Diff: 6
Index 4, Value 8, Diff: 4
Index 5, Value 5, Diff: 0

Elements with difference 6 are: [6, 7, 9] with sum = 22. This forms our optimal subsequence.

Alternative Implementation

def max_subsequence_sum_v2(nums):
    diff_groups = {}
    
    for i, value in enumerate(nums):
        key = value - i
        if key in diff_groups:
            diff_groups[key] += value
        else:
            diff_groups[key] = value
    
    return max(diff_groups.values()) if diff_groups else 0

# Test with multiple examples
test_cases = [
    [6, 7, 9, 9, 8, 5],
    [1, 2, 3, 4],
    [5, 5, 5, 5]
]

for nums in test_cases:
    result = max_subsequence_sum_v2(nums)
    print(f"Input: {nums}")
    print(f"Maximum sum: {result}\n")
Input: [6, 7, 9, 9, 8, 5]
Maximum sum: 22

Input: [1, 2, 3, 4]
Maximum sum: 10

Input: [5, 5, 5, 5]
Maximum sum: 20

Conclusion

The solution uses the mathematical insight that valid subsequence elements share the same value - index difference. By grouping elements by this key and summing within groups, we can efficiently find the maximum subsequence sum in O(n) time complexity.

Updated on: 2026-03-25T11:39:33+05:30

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements