-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathsplitarraylargestsum.java
76 lines (70 loc) · 2.29 KB
/
splitarraylargestsum.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
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
/*
-- Split Array Largest Sum --
Given an array nums which consists of non-negative integers and an integer m,
you can split the array into m non-empty continuous subarrays.
Here is an algorithm to minimize the largest sum among these m subarrays in which
Set left to be the largest number in the array. Set right to be the sum of the array.
Then the result should be in the range of [left, right].
Finding the target by using binary search.
*/
package arrayls;
import java.util.Scanner;
public class splitarr {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter Size of Arraay");
int n = scan.nextInt();
int m = scan.nextInt();
int[] arrr = new int[n];
System.out.println("Enter "+n+" Elements of array");
for (int i = 0;i<n;i++){
arrr[i] = scan.nextInt();
}
System.out.println(splitArray(arrr, m));
}
public static int splitArray(int[] nums, int m) {
int n = nums.length;
int left = 0;
int right = 0;
// Here Lest will be incresed Forwards
//Set left to be the largest number in the array
//Set right to be the sum of the array.
for (int i = 0; i < n; ++i) {
left = Math.max(left, nums[i]);
right += nums[i];
}
// Then the result should be in the range of [left, right].
while (left <= right) {
int mid = (right - left) / 2 + left;
if (canSplit(nums, mid, m)) right = mid - 1;
else left = mid + 1;
}
return left;
}
private static boolean canSplit(int[] nums, int target, int m) {
int cnt = 1;
int sum = 0;
for (int i = 0; i < nums.length; ++i) {
sum += nums[i];
if (sum > target) {
++cnt;
sum = nums[i];
if (cnt > m) return false;
}
}
return true;
}
}
/*
Test Cases:
Input : 5 // Length of Array
2 // value of m
7 2 5 10 8
Output: 18
Input : 3 // Length of Array
3 // value of m
1 4 4
Output: 9
Time Complexity: O(n) where n is length of array
Space Complexity: O(1)
*/