- 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

# Find minimum sum such that one of every three consecutive elements is taken in C++

Suppose we have an array of n elements. The task is to find the minimum sum of elements from the array. Such that at least one element one element is picked out of every three consecutive elements in that array. So if the array is like [1, 2, 3, 6, 7, 1]. The output is 4. So if we pick 3 and 1, then (3 + 1 = 4). So there are some subarrays of consecutive elements like [1, 2, 3], [2, 3, 6], [3, 6, 7], [6, 7, 1], we have picked one element from every subarray.

Consider sum(i) will return minimum possible sum, where arr[i] is part of the solution and is last picked element. Then our result is min of sum(i – 1), sum(i – 2), sum(i – 3). As we can see that this has overlapping subproblem, then we can use the dynamic programming approach to solve this.

## Example

#include <iostream> using namespace std; int minOfThree(int a, int b, int c) { return min(min(a, b), c); } int getMinSum(int arr[], int n) { int sum[n]; sum[0] = arr[0]; sum[1] = arr[1]; sum[2] = arr[2]; for (int i=3; i<n; i++) sum[i] = arr[i] + minOfThree(sum[i-3], sum[i-2], sum[i-1]); return minOfThree(sum[n-1], sum[n-2], sum[n-3]); } int main() { int arr[] = {1, 2, 3, 20, 2, 10, 1}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Minimum sum is: " << getMinSum(arr, n); }

## Output

Minimum sum is: 4

- Related Questions & Answers
- Maximum subsequence sum such that no three are consecutive
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Find three element from different three arrays such that that a + b + c = sum in Python
- Place k elements such that minimum distance is maximized in C++
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed in C++
- Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++
- Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python
- Find pairs with given sum such that elements of pair are in different rows in Python
- Find a point such that sum of the Manhattan distances is minimize in C++
- Find pairs with given sum such that pair elements lie in different BSTs in Python
- Maximum sum such that no two elements are adjacent in C++
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B in Python
- Consecutive elements sum array in JavaScript
- Check if three consecutive elements in an array is identical in JavaScript
- Find an element in array such that sum of left array is equal to sum of right array using c++