Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python

Suppose we have a list of numbers called nums, we have to find a minimum sum subsequence from the given list such that at least one number for all groups of three consecutive numbers is selected. If the length of given list is less than 3, a number should still be selected.

So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 7, as we can select 2 and 5.

Algorithm

To solve this problem, we will use dynamic programming with the following approach ?

  • If n is 0, return 0
  • If n is 1, return nums[0]
  • If n is 2, return minimum of nums[0] and nums[1]
  • For larger arrays, use a table to store minimum sums
  • For each position i, add current element to minimum of previous 3 positions
  • Return minimum of last 3 positions

Example

Let's implement the solution using dynamic programming ?

class Solution:
    def solve(self, nums):
        n = len(nums)
        if n == 0:
            return 0
        if n == 1:
            return nums[0]
        if n == 2:
            return min(nums[0], nums[1])
        
        table = [0] * n
        
        table[0] = nums[0]
        table[1] = nums[1]
        table[2] = nums[2]
        
        for i in range(3, n):
            table[i] = nums[i] + min(table[i - 3], table[i - 2], table[i - 1])
        
        res = min(table[n - 1], table[n - 2], table[n - 3])
        return res

ob = Solution()
nums = [2, 3, 4, 5, 6, 7]
print(ob.solve(nums))
7

How It Works

The dynamic programming table stores the minimum sum ending at each position. For position i, we add nums[i] to the minimum of the previous three positions, ensuring we select at least one element from every group of three consecutive elements.

For the example [2, 3, 4, 5, 6, 7] ?

nums = [2, 3, 4, 5, 6, 7]
solution = Solution()

# Trace through the algorithm
n = len(nums)
table = [0] * n

table[0] = 2  # First element
table[1] = 3  # Second element  
table[2] = 4  # Third element

# For i = 3: table[3] = 5 + min(2, 3, 4) = 5 + 2 = 7
# For i = 4: table[4] = 6 + min(3, 4, 7) = 6 + 3 = 9  
# For i = 5: table[5] = 7 + min(4, 7, 9) = 7 + 4 = 11

print("Table values:", [2, 3, 4, 7, 9, 11])
print("Minimum of last 3:", min(11, 9, 7))
Table values: [2, 3, 4, 7, 9, 11]
Minimum of last 3: 7

Edge Cases

Let's test with different array sizes ?

solution = Solution()

# Test edge cases
print("Empty array:", solution.solve([]))
print("Single element:", solution.solve([5]))
print("Two elements:", solution.solve([3, 7]))
print("Three elements:", solution.solve([1, 2, 3]))
Empty array: 0
Single element: 5
Two elements: 3
Three elements: 1

Conclusion

This dynamic programming solution efficiently finds the minimum sum subsequence by ensuring at least one element is selected from every group of three consecutive numbers. The algorithm has O(n) time complexity and handles all edge cases properly.

Updated on: 2026-03-25T12:41:52+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements