-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.cpp
73 lines (58 loc) · 1.59 KB
/
array.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
64
65
66
67
68
69
70
71
72
// CPP program to determine if array arr[]
// can be split into three equal sum sets.
#include <iostream>
using namespace std;
// Returns split point. If not possible, then
// return -1.
int findSplitPoint(int arr[], int n)
{
int leftSum = 0 ;
// traverse array element
for (int i = 0; i < n; i++)
{
// add current element to left Sum
leftSum += arr[i] ;
// find sum of rest array elements (rightSum)
int rightSum = 0 ;
for (int j = i+1 ; j < n ; j++ )
rightSum += arr[j] ;
// split point index
if (leftSum == rightSum)
return i+1 ;
}
// if it is not possible to split array into
// two parts
return -1;
}
// Prints two parts after finding split point using
// findSplitPoint()
void printTwoParts(int arr[], int n)
{
int splitPoint = findSplitPoint(arr, n);
if (splitPoint == -1 || splitPoint == n )
{
cout << "Not Possible" <<endl;
return;
}
for (int i = 0; i < n; i++)
{
if(splitPoint == i)
cout << endl;
cout << arr[i] << " " ;
}
}
// Driver code
int main()
{
const size_t max_size = 100;
int numbers[max_size] {};
std::cout << "Enter integers('q' to end the arrays)\n";
size_t size = 0;
while(std::cin >> numbers[size++] && size < max_size)
; //Empty loop body
std::cin.clear(); //Clear failed state
std::cin.ignore(); //Discard first symbol in stream (our X)
int n = sizeof(numbers)/sizeof(numbers[0]);
printTwoParts(numbers, n);
return 0;
}