5 Best Ways to Program to Find Maximum Sum of Subsequence with Equal Value and Position Difference in Python

Rate this post

πŸ’‘ Problem Formulation: The challenge is to write a Python program that finds the maximum sum of a subsequence in an array such that the difference between two elements is equal to the difference of their respective indices. For example, given an array [3, 4, 8, 1, 2], an eligible subsequence would be [3, 4, 8] because each subsequent element maintains a difference equal to the index difference, i.e., 4 - 3 = 1 - 0 and 8 - 4 = 2 - 1. The desired output is the maximum sum of such a subsequence, which in this case is 15.

Method 1: Dynamic Programming

The dynamic programming approach constructs a solution step by step. It stores the results of past computations to avoid redundant work. For this problem, it involves iterating through the array and maintaining a record of the maximum sum subsequence for each possible difference between values and indices.

Here’s an example:

def max_sum_subsequence(arr):
    hashmap = dict()
    for i, num in enumerate(arr):
        key = num - i
        hashmap[key] = hashmap.get(key, 0) + num
    return max(hashmap.values())

# Example array
arr = [3, 4, 8, 1, 2]

The output of the code snippet:


This method iterates through the array only once, making it time efficient. The use of a hashmap stores the sum of all subsequences with the same difference between values and indices. Finally, the maximum sum is obtained by looking for the maximum value among the stored sums, which delivers the required result efficiently.

Method 2: Brute Force

The brute force method exhaustively searches through all possible subsequences in the array and selects the one that meets the criteria with the maximum sum. This approach is straightforward but not time-efficient, especially for large arrays.

Here’s an example:

from itertools import combinations

def max_sum_subsequence_brute(arr):
    max_sum = 0
    for size in range(1, len(arr)+1):
        for sub in combinations(arr, size):
            if all(sub[i+1] - sub[i] == i+1 for i in range(len(sub)-1)):
                max_sum = max(max_sum, sum(sub))
    return max_sum

# Example array
arr = [3, 4, 8, 1, 2]

The output of this code snippet:


This method checks each possible subsequence to find those with the required equal difference and calculates their sum. The maximum sum is then updated if a greater sum is found. This method is simple but inefficient for large datasets due to its combinatorial nature.

Method 3: Recursive Solution

The recursive solution breaks down the problem into smaller instances and solves each recursively to build up the final solution. This method can be augmented with memoization to improve performance by avoiding redundant calculations.

Here’s an example:

def max_sum_recursive(arr, n, prev, sum_dict):
    if n == 0:
        return 0

    # Including the last element
    incl = 0
    if prev is None or arr[n - 1] - (n - 1) == prev:
        incl = arr[n - 1] + max_sum_recursive(arr, n - 1, arr[n - 1] - (n - 1), sum_dict)
    # Excluding the last element
    excl = max_sum_recursive(arr, n - 1, prev, sum_dict)
    # Return max of including and excluding
    return max(incl, excl)

# Example array
arr = [3, 4, 8, 1, 2]
print(max_sum_recursive(arr, len(arr), None, {}))

The output of the code snippet:


This recursive method explores both including and excluding the last element recursively and returns the maximum of both choices. However, without memoization, it can lead to an exponential time complexity due to redundant calculations.

Method 4: Greedy Approach

A greedy approach attempts to build up a solution piece by piece, making the most advantageous choice at each step. For this problem, it typically would not work as the global optimum isn’t guaranteed by local optimizations. Therefore, this section would have been methodologically incorrect and has been omitted for this problem.

Bonus One-Liner Method 5: List Comprehension and Max Function

This method employs list comprehension to succinctly iterate through the array and calculate the sums in one line using the max function. It is an elegant but not necessarily the most efficient solution for this problem.

Here’s an example:

arr = [3, 4, 8, 1, 2]
print(max([sum(num for i, num in enumerate(arr) if num - i == diff) for diff in set(num - i for i, num in enumerate(arr))]))

The output of this code snippet:


This method uses list comprehension and set operations to first identify all different differences and then calculate their sums, finally selecting the maximum value. It’s a concise way to achieve the result but can be less readable and not optimal for large arrays.


  • Method 1: Dynamic Programming. Utilizes efficient iteration and a hashmap for storing subsequence sums. Strengths: Efficient for large arrays. Weaknesses: More complex to understand and implement.
  • Method 2: Brute Force. Checks all possible subsequences for the condition. Strengths: Straightforward to code and understand. Weaknesses: Inefficient time complexity, not suitable for large arrays.
  • Method 3: Recursive Solution. Recursively includes or excludes elements to build up the solution. Strengths: Elegant recursive breakdown. Weaknesses: Potentially very inefficient without memoization.
  • Bonus One-Liner Method 5: List Comprehension and Max Function. Elegant and compact one-liner method. Strengths: Short and clear for small cases. Weaknesses: Potentially hard to read, and performance issues with larger datasets.