-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPartitions.java
30 lines (28 loc) · 919 Bytes
/
Partitions.java
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
/*https://www.interviewbit.com/problems/partitions/*/
public class Solution {
int[][] dp;
public int solve(int n, int[] B) {
int ways = 0; int sum = 0, i, j, firstSum = 0, leftSum = 0, rightSum = 0;
for (int num : B) sum += num;
if (sum%3 != 0) return 0;
sum /= 3;
for (i = 0; i < n-2; ++i)
{
firstSum += B[i];
if (firstSum == sum) //first bucket found
{
leftSum = B[i+1];
rightSum = 0;
for (j = i+2; j < n; ++j) rightSum += B[j];
if (leftSum == sum && rightSum == sum) ++ways;
for (j = i+2; j < n-1; ++j)
{
leftSum += B[j];
rightSum -= B[j];
if (leftSum == sum && rightSum == sum) ++ways;
}
}
}
return ways;
}
}