Divide Chocolates into K sets of consecutuve Chocolates
Last Updated :
16 Jan, 2024
We are given N chocolates whose sweetness value is given by arr[]. We need to divide chocolates into K sets such that each set will contain consecutive chocolates and each chocolate should be present in exactly 1 set. The task is to maximize the minimum sweetness among all the K sets.
Note: A set may be empty, meaning someone might get 0 sweetness.
Examples:
Input: N = 3, K = 2, arr = {1, 2, 4}
Output: 3
Explanation: If we put the first two chocolates in the 1st set and the third chocolate in the 2nd set. Then the total sweetness value that you will get will be 3. Hence, the answer is 3 in this case.
Input: N = 3, K = 1, arr = {1, 3, 5}
Output: 9
Explanation: As you don’t have any other friends other than yourself. You will get all the chocolates. Hence, the answer is 9 in this case.
Approach: To solve the problem follow the below idea:
Using binary search, we can easily find out what would be maximum sweetness value satisfying the conditions given.
Below are the steps involved:
- Initilaize low = 0, high = sum(arr).
- Perform binary search where
- Get the mid = (high + low) >> 1.
- For each mid value check whether it can be maximum sweetness value or not.
- If it can be maximum value increase low = mid.
- Otherwise, make high = mid -1. As we need to achieve maximum value.
- Return low, which will be the maximum sweetness value.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int countPartitions( int N, int * arr, int val)
{
int currSum = 0;
int count = 0;
for ( int i = 0; i < N; i++) {
currSum += arr[i];
if (currSum >= val) {
currSum = 0;
count++;
}
}
return count;
}
int maxSweetness( int N, int K, int arr[])
{
int low = 0;
int high = 0;
for ( int i = 0; i < N; i++) {
high += arr[i];
}
while ((high - low) > 1) {
int mid = (high + low) >> 1;
if (countPartitions(N, arr, mid) >= K) {
low = mid;
}
else {
high = mid - 1;
}
}
if (countPartitions(N, arr, high) >= K) {
return high;
}
return low;
}
int main()
{
int N = 3;
int K = 2;
int arr[] = { 1, 2, 4 };
cout << maxSweetness(N, K, arr);
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
static int countPartitions( int N, int [] arr, int val) {
int currSum = 0 ;
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
currSum += arr[i];
if (currSum >= val) {
currSum = 0 ;
count++;
}
}
return count;
}
static int maxSweetness( int N, int K, int [] arr) {
int low = 0 ;
int high = 0 ;
for ( int i = 0 ; i < N; i++) {
high += arr[i];
}
while ((high - low) > 1 ) {
int mid = (high + low) >>> 1 ;
if (countPartitions(N, arr, mid) >= K) {
low = mid;
} else {
high = mid - 1 ;
}
}
if (countPartitions(N, arr, high) >= K) {
return high;
}
return low;
}
public static void main(String[] args) {
int N = 3 ;
int K = 2 ;
int [] arr = { 1 , 2 , 4 };
System.out.println(maxSweetness(N, K, arr));
}
}
|
Python3
def count_partitions(N, arr, val):
curr_sum = 0
count = 0
for i in range (N):
curr_sum + = arr[i]
if curr_sum > = val:
curr_sum = 0
count + = 1
return count
def max_sweetness(N, K, arr):
low = 0
high = sum (arr)
while high - low > 1 :
mid = (high + low) >> 1
if count_partitions(N, arr, mid) > = K:
low = mid
else :
high = mid - 1
if count_partitions(N, arr, high) > = K:
return high
return low
N = 3
K = 2
arr = [ 1 , 2 , 4 ]
print (max_sweetness(N, K, arr))
|
C#
using System;
public class Program
{
static int CountPartitions( int N, int [] arr, int val)
{
int currSum = 0;
int count = 0;
for ( int i = 0; i < N; i++)
{
currSum += arr[i];
if (currSum >= val)
{
currSum = 0;
count++;
}
}
return count;
}
static int MaxSweetness( int N, int K, int [] arr)
{
int low = 0;
int high = 0;
for ( int i = 0; i < N; i++)
{
high += arr[i];
}
while ((high - low) > 1)
{
int mid = (high + low) >> 1;
if (CountPartitions(N, arr, mid) >= K)
{
low = mid;
}
else
{
high = mid - 1;
}
}
if (CountPartitions(N, arr, high) >= K)
{
return high;
}
return low;
}
public static void Main( string [] args)
{
int N = 3;
int K = 2;
int [] arr = { 1, 2, 4 };
Console.WriteLine(MaxSweetness(N, K, arr));
}
}
|
Javascript
function countPartitions(N, arr, val) {
let currSum = 0;
let count = 0;
for (let i = 0; i < N; i++) {
currSum += arr[i];
if (currSum >= val) {
currSum = 0;
count++;
}
}
return count;
}
function maxSweetness(N, K, arr) {
let low = 0;
let high = 0;
for (let i = 0; i < N; i++) {
high += arr[i];
}
while ((high - low) > 1) {
let mid = Math.floor((high + low) / 2);
if (countPartitions(N, arr, mid) >= K) {
low = mid;
} else {
high = mid - 1;
}
}
if (countPartitions(N, arr, high) >= K) {
return high;
}
return low;
}
let inputN = 3;
let inputK = 2;
let inputArr = [1, 2, 4];
console.log(maxSweetness(inputN, inputK, inputArr));
|
Complexity Analysis:
Time Complexity: O(N log (sum (arr))), Where N is the size of the array arr.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...