Timer is Running

Pick values
Submissions: 1278   Accuracy:


  Difficulty: Easy   Marks: 2

You are given an array A[ ] of positive integers. You have to pick up some values from the array. The only condition is that at least one value among any four consecutive values must be picked. That means at least one among A[i], A[i+1], A[i+2], A[i+3] has to be picked. The aim is to pick values in such a way that the sum of the values is minimized.

The first line of input contains a single integer T denoting the number of test cases.Then T test cases follow. Each test case consists of two lines. The first line contains an integer N denoting the size of the array. The Second line of each test case contains N space separated positive integers denoting the values/elements in the array A[ ].


Corresponding to each test case, in a new line, print the minimum possible sum of the values that can be selected.

1 ≤ T ≤ 10
1 ≤ N ≤ 100000
1 ≤ A[i] ≤ 100000

1 2 3 1 2


At least one among any 4 consecutive numbers has to be picked. 
Given: 1 2 3 1 2

we have to pick at least one from the first 4 that means from 1 2 3 1
we have to pick at least one from the last 4 that means from 2 3 1 2

if we pick the second last 1, we satisfy both the conditions and the sum is minimised.


** For More Input/Output Examples Use 'Expected Output' option **

Author: Hemang Sarkar

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.