-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-sum.c
More file actions
127 lines (111 loc) · 3.85 KB
/
split-sum.c
File metadata and controls
127 lines (111 loc) · 3.85 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
int splitSum(const int *nums, const int numsSize, int *result[2], int returnColumnSizes[2]) {
int totalLeft = 0;
int totalRight = 0;
int left = 0;
int right = numsSize - 1;
if (right < 1) { /* Empty array or single element array. */
returnColumnSizes[0] = 0;
returnColumnSizes[1] = 0;
return 1;
}
while (right != left) { /* Sum until the indexes meet in the middle. */
if (totalLeft <= totalRight) {
totalLeft += nums[left++];
} else {
totalRight += nums[right--];
}
}
if (totalLeft + nums[left] == totalRight) { /* Check middle in the left group. */
left++;
right++;
returnColumnSizes[0] = left;
memcpy(result[0], nums, left * sizeof(int));
returnColumnSizes[1] = numsSize - right;
memcpy(result[1], nums + right, (numsSize - right) * sizeof(int));
return 0;
}
if (totalLeft == totalRight + nums[right]) { /* Check middle in the right group. */
returnColumnSizes[0] = left;
memcpy(result[0], nums, left * sizeof(int));
returnColumnSizes[1] = numsSize - right;
memcpy(result[1], nums + right, (numsSize - right) * sizeof(int));
return 0;
}
returnColumnSizes[0] = 0;
returnColumnSizes[1] = 0;
return 1;
}
/*
* Globals so they aren't reallcoated on the stack each invocation.
*/
int *result[2];
int returnColumnSizes[2];
/*
* list must be longer than the longest case
* recommend list be a multiple of 64 bits (8 bytes) for alignment
*/
struct caseHolder {
int list[16];
int elements;
} cases[] = { { .list = { 0 }, .elements = 0 },
{ .list = { 100 }, .elements = 1 },
{ .list = { 99, 99 }, .elements = 2 },
{ .list = { 99, 1, 98 }, .elements = 3 },
{ .list = { 98, 1, 99 }, .elements = 3 },
{ .list = { 1, 2, 3, 0 }, .elements = 4 },
{ .list = { 1, 2, 2, 1, 0 }, .elements = 5 },
{ .list = { 10, 11, 12, 16, 17 }, .elements = 5 },
{ .list = { 1, 1, 1, 1, 1, 1, 6 }, .elements = 7 },
{ .list = { 6, 1, 1, 1, 1, 1, 1 }, .elements = 7 },
};
/*
* Test cases
*/
void testCases(int toScreen) {
for (int i = 0; i < (sizeof(cases) / sizeof(cases[0])); ++i) {
if (toScreen) {
printf("c: [");
for (int j = 0; j < cases[i].elements; ++j) {
printf("%s%d", j ? "," : "", cases[i].list[j]);
}
printf("] -> [[");
splitSum(cases[i].list, cases[i].elements, result, returnColumnSizes);
for (int j = 0; j < returnColumnSizes[0]; ++j) {
printf("%s%d", j ? "," : "", result[0][j]);
}
printf("],[");
for (int j = 0; j < returnColumnSizes[1]; ++j) {
printf("%s%d", j ? "," : "", result[1][j]);
}
printf("]]\n");
} else {
splitSum(cases[i].list, cases[i].elements, result, returnColumnSizes);
}
}
}
int main() {
int max = 0;
for (int i = 0; i < (sizeof(cases) / sizeof(cases[0])); ++i) {
if (cases[i].elements > max) {
max = cases[i].elements;
}
}
/*
* Pre-allocate return space.
*/
result[0] = (int *)malloc(max * sizeof(int));
result[1] = (int *)malloc(max * sizeof(int));
testCases(1);
clock_t start_time = clock();
for (int i = 0; i < 1000000; i++) {
testCases(0);
}
clock_t end_time = clock();
double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("c: %.3f seconds\n", elapsed_time);
}