- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# 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 Articles
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Maximum subsequence sum such that no three are consecutive
- Place k elements such that minimum distance is maximized in C++
- Find three element from different three arrays such that that a + b + c = sum in Python
- Find three consecutive natural numbers such that the sum of the first and second is 15 more than the third.
- Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python
- 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++
- The sum of three consecutive integers is (-87). Find the three integers.
- Find three consecutive even numbers such that sum of 1st and last numbers exceed the second number by 10.
- The sum of three consecutive multiples of 7 is 63. Find multiples.
- The sum of three consecutive even number is 96 . Find the numbers
- The sum of three consecutive odd numbers is 39. Find the number
- Three consecutive positive integers are such that the sum of the square of the first and the product of other two is 46, find the integers.
- Maximum sum such that no two elements are adjacent in C++