-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay15.java
More file actions
94 lines (75 loc) · 2.41 KB
/
Copy pathDay15.java
File metadata and controls
94 lines (75 loc) · 2.41 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
/*Split array in three equal sum subarrays
Given an array, arr[], determine if arr can be split into three consecutive parts such that the sum of each part is equal. If possible, return any index pair(i, j) in an array such that sum(arr[0..i]) = sum(arr[i+1..j]) = sum(arr[j+1..n-1]), otherwise return an array {-1,-1}.
Note: Since multiple answers are possible, return any of them. The driver code will print true if it is correct otherwise, it will print false.
Examples :
Input: arr[] = [1, 3, 4, 0, 4]
Output: true
Explanation: [1, 2] is valid pair as sum of subarray arr[0..1] is equal to sum of subarray arr[2..3] and also to sum of subarray arr[4..4]. The sum is 4, so driver code prints true.
Input: arr[] = [2, 3, 4]
Output: false
Explanation: No three subarrays exist which have equal sum.
Input: arr[] = [0, 1, 1]
Output: false
*/
/*// User function Template for Java
class Solution {
public List<Integer> findSplit(int[] arr) {
List<Integer> result = new ArrayList<>();
int total = 0;
for (int x : arr) total += x;
// If total sum not divisible by 3
if (total % 3 != 0) {
result.add(-1);
result.add(-1);
return result;
}
int partSum = total / 3;
int currSum = 0;
int i = -1, j = -1;
for (int k = 0; k < arr.length; k++) {
currSum += arr[k];
if (currSum == partSum && i == -1) {
i = k;
}
else if (currSum == 2 * partSum && i != -1) {
j = k;
break;
}
}
if (i != -1 && j != -1) {
result.add(i);
result.add(j);
} else {
result.add(-1);
result.add(-1);
}
return result;
}
}
*/
/*int[] splitArray(int[] arr, int n) {
int total = 0;
for (int x : arr) total += x;
if (total % 3 != 0)
return new int[]{-1, -1};
int partSum = total / 3;
int currSum = 0;
int i = -1, j = -1;
for (int k = 0; k < n; k++) {
currSum += arr[k];
if (currSum == partSum && i == -1) {
i = k;
}
else if (currSum == 2 * partSum && i != -1) {
j = k;
break;
}
}
if (i != -1 && j != -1)
return new int[]{i, j};
return new int[]{-1, -1};
}
*/
/*⏱ Complexity
Time: O(n)
Space: O(1) (excluding output list) */