-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtapeEqulibrium.cpp
63 lines (44 loc) · 1.38 KB
/
tapeEqulibrium.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
#include <math.h>
using namespace std;
int addSubArray(vector<int> &A, int low, int high){
int sum = 0;
for(int i = low; i <= high; i++) {
sum += A[i];
}
return sum;
}
int solution(vector<int> &A)
{
int minEqulibrium = INT_MAX;
int sumRight = 0;
int sumLeft = 0;
int size = A.size();
for(int i = 1; i < size; i++) {
sumRight += A[i];
}
sumLeft = A[0];
minEqulibrium = std::min(minEqulibrium, abs(sumLeft - sumRight));
for(int i = 1; i < size - 1; i++) {
sumRight -= A[i];
sumLeft += A[i];
minEqulibrium = std::min(minEqulibrium, abs(sumLeft - sumRight));
}
return minEqulibrium;
}
int main()
{
vector<int> vect{3, 1, 2, 4, 3 };
int min = solution(vect);
std::cout << "Min is : " << min << std::endl;
}