Timer is Running

Taking 1 out of 3 consecutives
Submissions: 3211   Accuracy:

30.18%

  Difficulty: Medium   Marks: 4

Given an array a consisting of N non-negative numbers your task is to find the minimum sum of the array such that out of 3 consecutive  elements you need to add at-least one.

Input:
First line consists of T Test cases. Each test case contains 2 lines . The first line consists of a number N specifying the number of elements in an array and the next line contains N space separated elements.

Output:
Output in a single line minimum sum that can be obtained.

Constraints:
1<=T<=30
1<=N<=105
1<=a[i]<=105

Example:
Input:
2
6
1 2 3 6 7 1
2
3 2
Output:
4
0

Explanation:
Testcase 1:
Moving from left to right 3+1. When 3 is added next 3 consecutive elements be 6 7 and 1, from which we take 1. Which covers all subarray of lenght 3 (3+1=4).
Testcase 2:
We won't take any element as the array length is less than 3.

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

Author: Ayush Saluja


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.