-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminmal_subarray.cpp
More file actions
41 lines (35 loc) · 1005 Bytes
/
minmal_subarray.cpp
File metadata and controls
41 lines (35 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// Created by Mayank Parasar on 2019-12-18.
//
/*
* Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray
* of which the sum ≥ s. If there isn't one, return 0 instead.
* Input: s = 7, nums = [2,3,1,2,4,3]
* Output: 2
*/
#include <iostream>
#include <vector>
using namespace std;
int minimal_contiguous_arrray(vector<int>& nums, int sum) {
vector<int> len;
int accum= 0;
for(int i = 0; i < nums.size(); i++) {
for(int k = i; k < nums.size(); k++) {
accum+=nums[k];
if(accum >= sum) {
// put the length into the vector and break
int length = (k-i + 1);
len.push_back(length);
break;
}
}
// reset accum
accum = 0;
}
return (*min_element(len.begin(), len.end()));
}
int main() {
vector<int> num = {2, 3, 1, 2, 4, 3};
cout << minimal_contiguous_arrray(num, 7);
return 0;
}