Minimum sum subsequence such that at least one of every four consecutive elements is picked
Last Updated :
19 Sep, 2023
Given an array arr[] of positive integers. The task is to find minimum sum subsequence from the array such that at least one value among all groups of four consecutive elements is picked.
Examples :
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 6
6 is sum of output subsequence {1, 5}
Note that we have following subarrays of four
consecutive elements
{(1, 2, 3, 4),
(2, 3, 4, 5),
(3, 4, 5, 6)
(4, 5, 6, 7)
(5, 6, 7, 8)}
Our subsequence {1, 5} has an element from
all above groups of four consecutive elements.
And this subsequence is minimum sum such
subsequence.
Input : arr[] = {1, 2, 3, 3, 4, 5, 6, 1}
Output : 4
The subsequence is {3, 1}. Here we consider
second three.
Input: arr[] = {1, 2, 3, 2, 1}
Output: 2
The subsequence can be {1, 1} or {2}
Input: arr[] = {6, 7, 8}
Output: 6
Input: arr[] = {6, 7}
Output: 6
The idea is similar to LIS problem. We store minimum sum subsequence ending with every element of arr[]. We finally return minimum of last four values.
dp[i] stores minimum sum subsequence (with at least
one of every four consecutive elements) of arr[0..i]
such that arr[i] is part of the solution. Note that
this may not be the best solution for subarray
arr[0..i].
We can recursively compute dp[i] using below formula
dp[i] = arr[i] + min(dp[i-1], dp[i-2], dp[i-3], dp[i-4])
Finally we return minimum of dp[n-1], dp[n-2],
dp[n-4] and dp[n-3]
OR
///Idea is simple store values of first 4 numbers in four variables
let arr[] = {5,6,7,8,9}
a=5;
b=6;
c=7;
d=8;
now Imagine if size is only five then what are the possible sets we can have?
{5,6,7,8} and {6,7,8,9}
if you can simply analyse these both the first three numbers in set two are common and
the first number in set 1 is different and last number in set 2 is also different
now what is the minimum sum we can have
(min(6,7,8) ,(5+9) ) why minimum of (6,7,8) because they are common
why (5+9) because we have to choose one element each from both sets ;
This is code implementation
class Solution{
public:
int minSum(int arr[], int n){
//Write your code here
if(n<=4)
return *min_element(arr, arr+n);
int a, b, c, d, e, ans;
a = arr[0];
b = arr[1];
c = arr[2];
d = arr[3];
for(int i=4; i<n; i++){
e = arr[i] + min({a, b, c, d});
a = b;
b = c;
c = d;
d = e;
}
return min({a, b, c, d, e});
}
};
copied from :chessnoobdj;
///
Below is the implementation of above idea.
C++
// C++ program to find minimum sum subsequence
// of an array such that one of every four
// consecutive elements is picked.
#include <iostream>
using namespace std;
// Returns sum of minimum sum subsequence
// such that one of every four consecutive
// elements is picked from arr[].
int minSum(int arr[], int n)
{
// dp[i] is going to store minimum sum
// subsequence of arr[0..i] such that arr[i]
// is part of the solution. Note that this
// may not be the best solution for subarray
// arr[0..i]
int dp[n];
// If there is single value, we get the
// minimum sum equal to arr[0]
if (n == 1)
return arr[0];
// If there are two values, we get the
// minimum sum equal to the minimum of
// two values
if (n == 2)
return min(arr[0], arr[1]);
// If there are three values, return
// minimum of the three elements of
// array
if (n == 3)
return min(arr[0], min(arr[1], arr[2]));
// If there are four values, return minimum
// of the four elements of array
if (n == 4)
return min(min(arr[0], arr[1]),
min(arr[2], arr[3]));
dp[0] = arr[0];
dp[1] = arr[1];
dp[2] = arr[2];
dp[3] = arr[3];
for (int i = 4; i < n; i++)
dp[i] = arr[i] + min(min(dp[i - 1], dp[i - 2]),
min(dp[i - 3], dp[i - 4]));
// Return the minimum of last 4 index
return min(min(dp[n - 1], dp[n - 2]),
min(dp[n - 4], dp[n - 3]));
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 3, 4, 5, 6, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minSum(arr, n);
return 0;
}
Java
// Java program to find minimum sum subsequence
// of an array such that one of every four
// consecutive elements is picked.
import java.io.*;
class GFG {
// Returns sum of minimum sum subsequence
// such that one of every four consecutive
// elements is picked from arr[].
static int minSum(int[] arr, int n)
{
// dp[i] is going to store minimum sum
// subsequence of arr[0..i] such that arr[i]
// is part of the solution. Note that this
// may not be the best solution for subarray
// arr[0..i]
int[] dp = new int[n];
// If there is single value, we get the
// minimum sum equal to arr[0]
if (n == 1)
return arr[0];
// If there are two values, we get the
// minimum sum equal to the minimum of
// two values
if (n == 2)
return Math.min(arr[0], arr[1]);
// If there are three values, return
// minimum of the three elements of
// array
if (n == 3)
return Math.min(arr[0], Math.min(arr[1], arr[2]));
// If there are four values, return minimum
// of the four elements of array
if (n == 4)
return Math.min(Math.min(arr[0], arr[1]),
Math.min(arr[2], arr[3]));
dp[0] = arr[0];
dp[1] = arr[1];
dp[2] = arr[2];
dp[3] = arr[3];
for (int i = 4; i < n; i++)
dp[i] = arr[i] + Math.min(Math.min(dp[i - 1], dp[i - 2]),
Math.min(dp[i - 3], dp[i - 4]));
// Return the minimum of last 4 index
return Math.min(Math.min(dp[n - 1], dp[n - 2]),
Math.min(dp[n - 4], dp[n - 3]));
}
// Driver code
static public void main(String[] args)
{
int[] arr = { 1, 2, 3, 3, 4, 5, 6, 1 };
int n = arr.length;
System.out.println(minSum(arr, n));
}
}
// This Code is contributed by vt_m.
Python3
# Python 3 program to find minimum sum
# subsequence of an array such that one
# of every four consecutive elements is picked.
# Returns sum of minimum sum subsequence
# such that one of every four consecutive
# elements is picked from arr[].
def minSum(arr, n):
# dp[i] is going to store minimum sum
# subsequence of arr[0..i] such that
# arr[i] is part of the solution. Note
# that this may not be the best solution
# for subarray arr[0..i]
dp = [0] * n
# If there is single value, we get
# the minimum sum equal to arr[0]
if (n == 1):
return arr[0]
# If there are two values, we get the
# minimum sum equal to the minimum of
# two values
if (n == 2):
return min(arr[0], arr[1])
# If there are three values, return
# minimum of the three elements of
# array
if (n == 3):
return min(arr[0],
min(arr[1], arr[2]))
# If there are four values,
# return minimum of the four
# elements of array
if (n == 4):
return min(min(arr[0], arr[1]),
min(arr[2], arr[3]))
dp[0] = arr[0]
dp[1] = arr[1]
dp[2] = arr[2]
dp[3] = arr[3]
for i in range( 4, n):
dp[i] = arr[i] + min(min(dp[i - 1], dp[i - 2]),
min(dp[i - 3], dp[i - 4]))
# Return the minimum of last 4 index
return min(min(dp[n - 1], dp[n - 2]),
min(dp[n - 4], dp[n - 3]))
# Driver code
if __name__ == "__main__":
arr = [ 1, 2, 3, 3, 4, 5, 6, 1 ]
n = len(arr)
print(minSum(arr, n))
# This code is contributed by ita_c
C#
// C# program to find minimum sum subsequence
// of an array such that one of every four
// consecutive elements is picked.
using System;
class GFG {
// Returns sum of minimum sum subsequence
// such that one of every four consecutive
// elements is picked from arr[].
static int minSum(int[] arr, int n)
{
// dp[i] is going to store minimum sum
// subsequence of arr[0..i] such that arr[i]
// is part of the solution. Note that this
// may not be the best solution for subarray
// arr[0..i]
int[] dp = new int[n];
// If there is single value, we get the
// minimum sum equal to arr[0]
if (n == 1)
return arr[0];
// If there are two values, we get the
// minimum sum equal to the minimum of
// two values
if (n == 2)
return Math.Min(arr[0], arr[1]);
// If there are three values, return
// minimum of the three elements of
// array
if (n == 3)
return Math.Min(arr[0], Math.Min(arr[1], arr[2]));
// If there are four values, return minimum
// of the four elements of array
if (n == 4)
return Math.Min(Math.Min(arr[0], arr[1]),
Math.Min(arr[2], arr[3]));
dp[0] = arr[0];
dp[1] = arr[1];
dp[2] = arr[2];
dp[3] = arr[3];
for (int i = 4; i < n; i++)
dp[i] = arr[i] + Math.Min(Math.Min(dp[i - 1], dp[i - 2]),
Math.Min(dp[i - 3], dp[i - 4]));
// Return the minimum of last 4 index
return Math.Min(Math.Min(dp[n - 1], dp[n - 2]),
Math.Min(dp[n - 4], dp[n - 3]));
}
// Driver code
static public void Main()
{
int[] arr = { 1, 2, 3, 3, 4, 5, 6, 1 };
int n = arr.Length;
Console.WriteLine(minSum(arr, n));
}
}
// This code is contributed by vt_m.
JavaScript
<script>
// JavaScript program to find minimum sum subsequence
// of an array such that one of every four
// consecutive elements is picked.
// Returns sum of minimum sum subsequence
// such that one of every four consecutive
// elements is picked from arr[].
function minSum(arr, n)
{
// dp[i] is going to store minimum sum
// subsequence of arr[0..i] such that arr[i]
// is part of the solution. Note that this
// may not be the best solution for subarray
// arr[0..i]
let dp = [];
// If there is single value, we get the
// minimum sum equal to arr[0]
if (n == 1)
return arr[0];
// If there are two values, we get the
// minimum sum equal to the minimum of
// two values
if (n == 2)
return Math.min(arr[0], arr[1]);
// If there are three values, return
// minimum of the three elements of
// array
if (n == 3)
return Math.min(arr[0],
Math.min(arr[1], arr[2]));
// If there are four values, return minimum
// of the four elements of array
if (n == 4)
return Math.min(Math.min(arr[0], arr[1]),
Math.min(arr[2], arr[3]));
dp[0] = arr[0];
dp[1] = arr[1];
dp[2] = arr[2];
dp[3] = arr[3];
for(let i = 4; i < n; i++)
dp[i] = arr[i] +
Math.min(Math.min(dp[i - 1], dp[i - 2]),
Math.min(dp[i - 3], dp[i - 4]));
// Return the minimum of last 4 index
return Math.min(Math.min(dp[n - 1], dp[n - 2]),
Math.min(dp[n - 4], dp[n - 3]));
}
// Driver Code
let arr = [ 1, 2, 3, 3, 4, 5, 6, 1 ];
let n = arr.length;
document.write(minSum(arr, n));
// This code is contributed by code_hunt
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Alternate Solution :
First of all, think that we have only four elements then our result is at least four given elements. Now, in the case, if we have more than four elements then we must maintain an array sum[] where sum[i] includes the possible minimal sum up to i-th element and also i-th element must be a part of the solution.
While computing sum[i], our base condition is that arr[i] must be a part of sum[i] and then we must have an element from last four elements. So, we can recursively compute sum[i] as sum of arr[i] and minimum (sum[i-1], sum[i-2], sum[i-3], sum[i-4]). Since there are overlapping subproblems in the recursive structure of our problem, we can use Dynamic Programming to solve this problem. And for the final result we must compute minimum of last four values of sum[] array as result must contain an element from last four elements.
C++
// CPP program to calculate
// minimum possible sum for given constraint
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
// function to calculate min sum using dp
int minSum(int ar[], int n)
{
// if elements are less than or equal to 4
if (n <= 4)
return *min_element(ar, ar + n);
// save start four element as it is
int sum[n];
sum[0] = ar[0];
sum[1] = ar[1];
sum[2] = ar[2];
sum[3] = ar[3];
// compute sum[] for all rest elements
for (int i = 4; i < n; i++)
sum[i] = ar[i] + (*min_element(sum + i - 4, sum + i));
// Since one of the last 4 elements must be
// present
return *min_element(sum + n - 4, sum + n);
}
// driver program
int main()
{
int ar[] = { 2, 4, 1, 5, 2, 3, 6, 1, 2, 4 };
int n = sizeof(ar) / sizeof(ar[0]);
cout << "Minimum sum = " << minSum(ar, n);
return 0;
}
Java
// Java program to calculate
// minimum possible sum for given constraint
import java.util.Arrays;
class GFG
{
// function to calculate min sum using dp
static int minSum(int ar[], int n)
{
// if elements are less than or equal to 4
if (n <= 4)
return Arrays.stream(ar).min().getAsInt();
// save start four element as it is
int []sum = new int[n];
sum[0] = ar[0];
sum[1] = ar[1];
sum[2] = ar[2];
sum[3] = ar[3];
// compute sum[] for all rest elements
for (int i = 4; i < n; i++)
//sum[i] = ar[i] + (*min_element(sum + i - 4, sum + i));
sum[i] = ar[i] + Arrays.stream(Arrays.copyOfRange(
sum, i - 4, i)).min().getAsInt();
// Since one of the last 4 elements must be
// present
return Arrays.stream(Arrays.copyOfRange(
sum, n - 4, n)).min().getAsInt();
}
// Driver Code
public static void main(String[] args)
{
int ar[] = { 2, 4, 1, 5, 2, 3, 6, 1, 2, 4 };
int n = ar.length;
System.out.println("Minimum sum = " + minSum(ar, n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to calculate
# minimum possible sum for given constraint
# function to calculate min sum using dp
def minSum(ar, n):
# if elements are less than or equal to 4
if (n <= 4):
return min(ar)
# save start four element as it is
sum = [0 for i in range(n)]
sum[0] = ar[0]
sum[1] = ar[1]
sum[2] = ar[2]
sum[3] = ar[3]
# compute sum[] for all rest elements
for i in range(4, n):
sum[i] = ar[i] + min(sum[i - 4:i])
# Since one of the last 4 elements must be
# present
return min(sum[n - 4:n])
# Driver Code
ar = [2, 4, 1, 5, 2, 3, 6, 1, 2, 4]
n = len(ar)
print("Minimum sum = ", minSum(ar, n))
# This code is contributed by Mohit Kumar
C#
// C# program to calculate
// minimum possible sum for given constraint
using System;
using System.Linq;
class GFG
{
// function to calculate min sum using dp
static int minSum(int []ar, int n)
{
// if elements are less than or equal to 4
if (n <= 4)
return ar.Min();
// save start four element as it is
int []sum = new int[n];
sum[0] = ar[0];
sum[1] = ar[1];
sum[2] = ar[2];
sum[3] = ar[3];
int []tempArr;
// compute sum[] for all rest elements
for (int i = 4; i < n; i++)
{
//sum[i] = ar[i] + (*min_element(sum + i - 4, sum + i));
tempArr = new int[4];
Array.Copy(sum, i - 4, tempArr, 0, 4);
sum[i] = ar[i] + tempArr.Min();
}
// Since one of the last 4 elements must be
// present
tempArr = new int[4];
Array.Copy(sum,n-4,tempArr,0,4);
return tempArr.Min();
}
// Driver Code
public static void Main(String[] args)
{
int []ar = { 2, 4, 1, 5, 2, 3, 6, 1, 2, 4 };
int n = ar.Length;
Console.WriteLine("Minimum sum = " +
minSum(ar, n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program to calculate
// minimum possible sum for given constraint
// function to calculate min sum using dp
function minSum(ar, n)
{
// if elements are less than or equal to 4
if (n <= 4)
return Math.min.apply(Math,ar);
var i;
// save start four element as it is
var sum = Array(n).fill(n);
sum[0] = ar[0];
sum[1] = ar[1];
sum[2] = ar[2];
sum[3] = ar[3];
// compute sum[] for all rest elements
for (i = 4; i < n; i++){
var temp = [];
var it;
for(it= i-4;it<i;it++)
temp.push(sum[it]);
//(*min_element(sum + i - 4, sum + i));
sum[i] = ar[i] + Math.min.apply(Math,temp);
}
// Since one of the last 4 elements must be
// present
var temp1 = [];
for(i=n-4;i<n;i++)
temp1.push(sum[i]);
return Math.min.apply(Math,temp1);
}
// driver program
var ar = [2, 4, 1, 5, 2, 3, 6, 1, 2, 4];
var n = ar.length;
document.write("Minimum sum = "+minSum(ar, n));
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Efficient approach : Space Optimization O(1)
To optimize the space complexity of the above approach we will using variables only, we can use four variables to keep track of the minimum sum subsequence for the previous four elements.
Implementation Steps:
- Implement the minSum function that takes an integer array arr and its size n as parameters.
- Handle the base cases for n equals 1, 2, 3, and 4 by returning the minimum values from the array.
- Initialize four variables (prev1, prev2, prev3, and prev4) with the first four elements of the array.
- Iterate from index 4 to n-1 and calculate the minimum sum subsequence for each element. Update the prev1, prev2, prev3, and prev4 variables in each iteration.
- After the loop, return the minimum value among the last four elements: min(min(prev1, prev2), min(prev3, prev4)).
Implementation:
C++
#include <iostream>
using namespace std;
// Returns sum of minimum sum subsequence
// such that one of every four consecutive
// elements is picked from arr[].
int minSum(int arr[], int n)
{
// If there is a single value, we get the
// minimum sum equal to arr[0]
if (n == 1)
return arr[0];
// If there are two values, we get the
// minimum sum equal to the minimum of
// two values
if (n == 2)
return min(arr[0], arr[1]);
// If there are three values, return
// the minimum of the three elements of
// the array
if (n == 3)
return min(arr[0], min(arr[1], arr[2]));
// If there are four values, return the minimum
// of the four elements of the array
if (n == 4)
return min(min(arr[0], arr[1]),
min(arr[2], arr[3]));
// Initialize variables to store the minimum sum
// subsequences for the previous four elements
int prev1 = arr[0];
int prev2 = arr[1];
int prev3 = arr[2];
int prev4 = arr[3];
for (int i = 4; i < n; i++)
{
// Calculate the minimum sum subsequence for
// the current element by taking the minimum of
// the previous four elements and adding the
// current element
int current = arr[i] + min(min(prev1, prev2),
min(prev3, prev4));
// Update the variables for the next iteration
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = current;
}
// Return the minimum of the last four elements
return min(min(prev1, prev2),
min(prev3, prev4));
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 3, 4, 5, 6, 1};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum sum = " << minSum(arr, n);
return 0;
}
Java
import java.util.Arrays;
public class GFG {
// Returns sum of minimum sum subsequence
// such that one of every four consecutive
// elements is picked from arr[].
public static int minSum(int[] arr, int n) {
// If there is a single value, we get the
// minimum sum equal to arr[0]
if (n == 1)
return arr[0];
// If there are two values, we get the
// minimum sum equal to the minimum of
// two values
if (n == 2)
return Math.min(arr[0], arr[1]);
// If there are three values, return
// the minimum of the three elements of
// the array
if (n == 3)
return Math.min(arr[0], Math.min(arr[1], arr[2]));
// If there are four values, return the minimum
// of the four elements of the array
if (n == 4)
return Math.min(Math.min(arr[0], arr[1]), Math.min(arr[2], arr[3]));
// Initialize variables to store the minimum sum
// subsequences for the previous four elements
int prev1 = arr[0];
int prev2 = arr[1];
int prev3 = arr[2];
int prev4 = arr[3];
for (int i = 4; i < n; i++) {
// Calculate the minimum sum subsequence for
// the current element by taking the minimum of
// the previous four elements and adding the
// current element
int current = arr[i] + Math.min(Math.min(prev1, prev2), Math.min(prev3, prev4));
// Update the variables for the next iteration
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = current;
}
// Return the minimum of the last four elements
return Math.min(Math.min(prev1, prev2), Math.min(prev3, prev4));
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 2, 3, 3, 4, 5, 6, 1};
int n = arr.length;
System.out.println("Minimum sum = " + minSum(arr, n));
}
}
Python3
# Returns sum of minimum sum subsequence
# such that one of every four consecutive
# elements is picked from arr[].
def min_sum(arr):
n = len(arr)
# If there is a single value, we get the
# minimum sum equal to arr[0]
if n == 1:
return arr[0]
# If there are two values, we get the
# minimum sum equal to the minimum of
# two values
if n == 2:
return min(arr[0], arr[1])
# If there are three values, return
# the minimum of the three elements of
# the array
if n == 3:
return min(arr[0], min(arr[1], arr[2]))
# If there are four values, return the minimum
# of the four elements of the array
if n == 4:
return min(min(arr[0], arr[1]), min(arr[2], arr[3]))
# Initialize variables to store the minimum sum
# subsequences for the previous four elements
prev1 = arr[0]
prev2 = arr[1]
prev3 = arr[2]
prev4 = arr[3]
for i in range(4, n):
# Calculate the minimum sum subsequence for
# the current element by taking the minimum of
# the previous four elements and adding the
# current element
current = arr[i] + min(prev1, prev2, prev3, prev4)
# Update the variables for the next iteration
prev1 = prev2
prev2 = prev3
prev3 = prev4
prev4 = current
# Return the minimum of the last four elements
return min(prev1, prev2, prev3, prev4)
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 3, 4, 5, 6, 1]
print("Minimum sum =", min_sum(arr))
C#
using System;
class GFG
{
// Returns sum of minimum sum subsequence
// such that one of every four consecutive
// elements is picked from arr[].
static int MinSum(int[] arr, int n)
{
// If there is a single value, we get the
// minimum sum equal to arr[0]
if (n == 1)
return arr[0];
// If there are two values, we get the
// minimum sum equal to the minimum of
// two values
if (n == 2)
return Math.Min(arr[0], arr[1]);
// If there are three values, return
// the minimum of the three elements of
// the array
if (n == 3)
return Math.Min(arr[0], Math.Min(arr[1], arr[2]));
// If there are four values, return the minimum
// of the four elements of the array
if (n == 4)
return Math.Min(Math.Min(arr[0], arr[1]),
Math.Min(arr[2], arr[3]));
// Initialize variables to store the minimum sum
// subsequences for the previous four elements
int prev1 = arr[0];
int prev2 = arr[1];
int prev3 = arr[2];
int prev4 = arr[3];
for (int i = 4; i < n; i++)
{
// Calculate the minimum sum subsequence for
// the current element by taking the minimum of
// the previous four elements and adding the
// current element
int current = arr[i] + Math.Min(Math.Min(prev1, prev2),Math.Min(prev3, prev4));
// Update the variables for the next iteration
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = current;
}
// Return the minimum of the last four elements
return Math.Min(Math.Min(prev1, prev2),
Math.Min(prev3, prev4));
}
// Driver code
static void Main()
{
int[] arr = { 1, 2, 3, 3, 4, 5, 6, 1 };
int n = arr.Length;
Console.WriteLine("Minimum sum = " + MinSum(arr, n));
}
}
JavaScript
// Returns sum of minimum sum subsequence
// such that one of every four consecutive
// elements is picked from arr[].
function minSum(arr) {
const n = arr.length;
// If there is a single value, we get the
// minimum sum equal to arr[0]
if (n === 1) {
return arr[0];
}
// If there are two values, we get the
// minimum sum equal to the minimum of
// two values
if (n === 2) {
return Math.min(arr[0], arr[1]);
}
// If there are three values, return
// the minimum of the three elements of
// the array
if (n === 3) {
return Math.min(arr[0], Math.min(arr[1], arr[2]));
}
// If there are four values, return the minimum
// of the four elements of the array
if (n === 4) {
return Math.min(Math.min(arr[0], arr[1]), Math.min(arr[2], arr[3]));
}
// Initialize variables to store the minimum sum
// subsequences for the previous four elements
let prev1 = arr[0];
let prev2 = arr[1];
let prev3 = arr[2];
let prev4 = arr[3];
for (let i = 4; i < n; i++) {
// Calculate the minimum sum subsequence for
// the current element by taking the minimum of
// the previous four elements and adding the
// current element
const current = arr[i] + Math.min(Math.min(prev1, prev2), Math.min(prev3, prev4));
// Update the variables for the next iteration
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = current;
}
// Return the minimum of the last four elements
return Math.min(Math.min(prev1, prev2), Math.min(prev3, prev4));
}
// Driver code
const arr = [1, 2, 3, 3, 4, 5, 6, 1];
console.log("Minimum sum =", minSum(arr));
// This code is contributed by shivamgupta0987654321
Output
Minimum sum = 4
Time Complexity: O(n)
Auxiliary Space: O(1)
Thanks to Shivam Pradhan for providing this alternate solution.
Similar Reads
Maximum subsequence sum such that no K elements are consecutive
Given an array arr[] of N positive integers, the task is to find the maximum sum of a subsequence consisting of no K consecutive array elements. Examples: Input: arr[] = {10, 5, 8, 16, 21}, K = 4Output: 55Explanation:Maximum sum is obtained by picking 10, 8, 16, 21. Input: arr[] = {4, 12, 22, 18, 34
9 min read
Find minimum sum such that one of every three consecutive elements is taken
Given an array of n non-negative numbers, the task is to find the minimum sum of elements (picked from the array) such that at least one element is picked out of every 3 consecutive elements in the array. Examples : Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {1, 2, 3, 6, 7, 1} Output : 4 W
9 min read
Maximum subsequence sum such that no three are consecutive in O(1) space
Given an array A[] of N positive numbers, the task is to find the maximum sum that can be formed which has no three consecutive elements present. Examples: Input: A[] = {1, 2, 3}, N=3Output: 5Explanation: Three of them can't be taken together so answer is 2 + 3 = 5 Input: A[] = {3000, 2000, 1000, 3,
8 min read
Maximum Sum Subsequence made up of consecutive elements of different parity
Given an array arr[] consisting of N integers, the task is to find the maximum sum of a non-empty subsequence such that each pair of consecutive terms is of different parity (even or odd). Examples: Input: arr[] = {1, 2, 6, 8, -5, 10}Output: 14Explanation: Considering the subsequence {1, 8, -5, 10}
9 min read
Maximum subsequence sum such that no three are consecutive
Given a sequence of positive numbers, find the maximum sum that can be formed which has no three consecutive elements present.Examples : Input: arr[] = {1, 2, 3}Output: 5We can't take three of them, so answer is2 + 3 = 5Input: arr[] = {3000, 2000, 1000, 3, 10}Output: 5013 3000 + 2000 + 3 + 10 = 5013
14 min read
Minimize insertions to make sum of every pair of consecutive array elements at most K
Given an array arr[] of N positive integers and an integer K, the task is to find the minimum number of insertions of any positive integers is required such that the sum of every adjacent element is at most K. If it is not possible, then print "-1". Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 6Out
8 min read
Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
Given an array arr[] of N elements. The task is to find the number of sub-sequences which have at least two consecutive elements such that absolute difference between them is ? 1. Examples: Input: arr[] = {1, 6, 2, 1} Output: 6 {1, 2}, {1, 2, 1}, {2, 1}, {6, 2, 1}, {1, 1} and {1, 6, 2, 1} are the su
7 min read
Rearrange an array such that product of every two consecutive elements is a multiple of 4
Given an array arr[] of size N, the task is to rearrange the array elements such that for every index i(1 <= i <= N - 1), the product of arr[i] and arr[i - 1] is a multiple of 4.Example: Input: arr[] = {1, 10, 100} Output: 1, 100, 10 Explanation: 1 * 100 is divisible by 4 100 * 10 is divisible
11 min read
Longest subsequence such that adjacent elements have at least one common digit
Given an array arr[], the task is to find the length of the longest sub-sequence such that adjacent elements of the subsequence have at least one digit in common.Examples: Input: arr[] = [1, 12, 44, 29, 33, 96, 89] Output: 5 Explanation: The longest sub-sequence is [1 12 29 96 89]Input: arr[] = [12,
15+ min read
Maximum sum subsequence with at-least k distant elements
Given an array and a number k, find a subsequence such that Sum of elements in subsequence is maximumIndices of elements of subsequence differ atleast by k Examples Input : arr[] = {4, 5, 8, 7, 5, 4, 3, 4, 6, 5} k = 2 Output: 19 Explanation: The highest value is obtained if you pick indices 1, 4, 7,
6 min read