-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
39 lines (35 loc) · 863 Bytes
/
code_1.cpp
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
//
// main.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 31/07/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <bits/stdc++.h>
using namespace std;
int smallestSumSubarr(int arr[], int n) {
int min_ending_here = INT_MAX;
int min_so_far = INT_MAX;
for (int i = 0; i < n; i++) {
if (min_ending_here > 0) {
min_ending_here = arr[i];
}
else {
min_ending_here += arr[i];
}
min_so_far = min(min_so_far, min_ending_here);
}
return min_so_far;
}
int main() {
int n;
cout << "\nEnter Size of Set\t:\t";
cin >> n;
int a[n];
cout << "\nEnter Set Elements\n";
for(int i = 0; i < n ; i++ ) {
cin >> a[i];
}
cout << "\nSmallest sum\t:\t" << smallestSumSubarr(a, n);
return 0;
}