-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhalfTheArray.cpp
More file actions
54 lines (44 loc) · 873 Bytes
/
halfTheArray.cpp
File metadata and controls
54 lines (44 loc) · 873 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
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int splitArr(vector<int> arr,int size);
int splitArr(vector<int> arr,int size)
{
int lSum{};
sort(arr.begin(),arr.end());
for (const int itr:arr)
{
lSum += itr;
}
int rSum{};
sort(arr.rbegin(),arr.rend());
for (const int it:arr )
{
rSum += it;
lSum -= it;
if (lSum == rSum)
{
return lSum;
}
}
return -1;
}
int main()
{
vector<int> arr;
int size{};
cout << "Enter the size\n";
cin >> size;
cout << "Enter "<<size << "elements";
for ( int i{0}; i<size ; ++i)
{
int temp{};
cin >>temp;
arr.push_back(temp);
}
int index = splitArr(arr,size);
// display(arr);
cout<<'\n'<<index;
return 0;
}