- 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 minimum sum subsequence by taking at least one element from consecutive 3 elements in python

Suppose we have a 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.

To solve this, we will follow these steps:

- n := size of nums
- if n is same as 0, then
- return 0

- if n is same as 1, then
- return nums[0]

- if n is same as 2, then
- return minimum of nums[0] and nums[1]

- table := a list of size n and fill with 0
- table[0] := nums[0]
- table[1] := nums[1]
- table[2] := nums[2]
- for i in range 3 to n, do
- table[i] := nums[i] + minimum of table[i - 3], table[i - 2] and table[i - 1]

- res := minimum of table[n - 1], table[n - 2] and table[n - 3]
- return res

Let us see the following implementation to get better understanding:

## Example Code

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))

## Input

[2, 3, 4, 5, 6, 7]

## Output

7

- Related Questions & Answers
- Maximum sum subsequence with at-least k distant elements in C++ program
- Maximum sum subsequence with at-least k distant elements in C++
- Count elements that are divisible by at-least one element in another array in C++
- Python program to check if two lists have at least one common element
- Program to find elements from list which have occurred at least k times in Python
- Find minimum sum such that one of every three consecutive elements is taken in C++
- Program to find closest subsequence sum in Python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Print array elements that are divisible by at-least one other in C++
- Program to find size of smallest sublist whose sum at least target in Python
- Program to check every sublist in a list containing at least one unique element in Python
- Program to find k where k elements have value at least k in Python
- Subarray sum with at least two elements in JavaScript
- C# program to check if two lists have at-least one element common
- Find the number of binary strings of length N with at least 3 consecutive 1s in C++

Advertisements