-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArraySplitting.java
More file actions
41 lines (30 loc) · 1.01 KB
/
Copy pathArraySplitting.java
File metadata and controls
41 lines (30 loc) · 1.01 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
package interviewQuestions;
import java.util.Arrays;
public class ArraySplitting {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 5};
splitAndPrint(arr);
}
// O(n) solution
public static void splitAndPrint(int[] arr) {
int totalSum = Arrays.stream(arr).sum();
int leftSum = 0;
int splitIndex = -1;
for (int i = 0; i < arr.length - 1; i++) { // can't split after last element
leftSum += arr[i];
int rightSum = totalSum - leftSum;
if (leftSum == rightSum) {
splitIndex = i + 1;
break;
}
}
if (splitIndex == -1) {
System.out.println("Split Not Possible");
return;
}
int[] left = Arrays.copyOfRange(arr, 0, splitIndex);
int[] right = Arrays.copyOfRange(arr, splitIndex, arr.length);
System.out.println(Arrays.toString(left));
System.out.println(Arrays.toString(right));
}
}