You are given integers K, M and a non-empty array A consisting of N integers. Every element of the array is not greater than M.
You should divide this array into K blocks of consecutive elements. The size of the block is any integer between 0 and N. Every element of the array should belong to some block.
The sum of the block from X to Y equals A[X] + A[X + 1] + ... + A[Y]. The sum of empty block equals 0.
The large sum is the maximal sum of any block.
For example, you are given integers K = 3, M = 5 and array A such that:
A[0] = 2 A[1] = 1 A[2] = 5 A[3] = 1 A[4] = 2 A[5] = 2 A[6] = 2The array can be divided, for example, into the following blocks:
- [2, 1, 5, 1, 2, 2, 2], [], [] with a large sum of 15;
- [2], [1, 5, 1, 2], [2, 2] with a large sum of 9;
- [2, 1, 5], [], [1, 2, 2, 2] with a large sum of 8;
- [2, 1], [5, 1], [2, 2, 2] with a large sum of 6.
The goal is to minimize the large sum. In the above example, 6 is the minimal large sum.
Write a function:
class Solution { public int solution(int K, int M, int[] A); }
that, given integers K, M and a non-empty array A consisting of N integers, returns the minimal large sum.
For example, given K = 3, M = 5 and array A such that:
A[0] = 2 A[1] = 1 A[2] = 5 A[3] = 1 A[4] = 2 A[5] = 2 A[6] = 2the function should return 6, as explained above.
Write an efficient algorithm for the following assumptions:
- N and K are integers within the range [1..100,000];
- M is an integer within the range [0..10,000];
- each element of array A is an integer within the range [0..M].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = su
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if () {
}
}
}
private boolean isDivisable(int mid, int K, int[] A) {
int size = K;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if () {
}
}
}
private boolean isDivisable(int mid, int K, int[] A) {
int size = K;
int sum = 0;
for (int idx = 0; idx < A.length; idx++) {
sum = sum + A[idx];
if (sum > mid) {
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if () {
}
}
}
private boolean isDivisable(int mid, int K, int[] A) {
int size = K;
int sum = 0;
for (int idx = 0; idx < A.length; idx++) {
sum = sum + A[idx];
if (sum > mid) {
size--;
sum = A[idx];
}
if (size == 0) {
return false;
}
}
return true;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if (isDivisable(mid, K, A)) {
result = mid;
} else {
max = mid + 1;
}
}
return result;
}
private boolean isDivisable(int mid, int K, int[] A) {
int size = K;
int sum = 0;
for (int idx = 0; idx < A.length; idx++) {
sum = sum + A[idx];
if (sum > mid) {
size--;
sum = A[idx];
}
if (size == 0) {
return false;
}
}
return true;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if (isDivisable(mid, K, A)) {
result = mid;
sum
} else {
max = mid + 1;
}
}
return result;
}
private boolean isDivisable(int mid, int K, int[] A) {
int size = K;
int sum = 0;
for (int idx = 0; idx < A.length; idx++) {
sum = sum + A[idx];
if (sum > mid) {
size--;
sum = A[idx];
}
if (size == 0) {
return false;
}
}
return true;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if (isDivisable(mid, K, A)) {
result = mid;
sum = mid - 1;
} else {
max = mid + 1;
}
}
return result;
}
private boolean isDivisable(int mid, int K, int[] A) {
int size = K;
int sum = 0;
for (int idx = 0; idx < A.length; idx++) {
sum = sum + A[idx];
if (sum > mid) {
size--;
sum = A[idx];
}
if (size == 0) {
return false;
}
}
return true;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if (isDivisable(mid, K, A)) {
result = mid;
sum = mid - 1;
} else {
max = mid + 1;
}
}
return result;
}
private boolean isDivisable(int mid, int K, int[] A) {
int size = K;
int sum = 0;
for (int idx = 0; idx < A.length; idx++) {
sum = sum + A[idx];
if (sum > mid) {
size--;
sum = A[idx];
}
if (size == 0) {
return false;
}
}
return true;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int K, int M, int[] A) {
int sum = 0;
int max = 0;
for (int idx = 0; idx < A.length; idx++) {
sum += A[idx];
if (A[idx] > max) {
max = A[idx];
}
}
int result = sum;
while (max <= sum) {
int mid = (max + sum) / 2;
if (isDivisable(mid, K, A)) {
result = mid;
sum = mid - 1;
} else {
max = mid + 1;
}
}
return result;
}
private boolean isDivisable(int mid, int K, int[] A) {
int size = K;
int sum = 0;
for (int idx = 0; idx < A.length; idx++) {
sum = sum + A[idx];
if (sum > mid) {
size--;
sum = A[idx];
}
if (size == 0) {
return false;
}
}
return true;
}
}
The solution obtained perfect score.