Open In App

Divide Chocolates into K sets of consecutuve Chocolates

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ Implementation
#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;
}
 
// Function to find maximum sweetness by dividing them in
// K groups
int maxSweetness(int N, int K, int arr[])
{
 
    int low = 0;
    int high = 0;
 
    // Set the high value
    for (int i = 0; i < N; i++) {
 
        high += arr[i];
    }
 
    // Iterate between low and high
    while ((high - low) > 1) {
 
        int mid = (high + low) >> 1;
 
        // Check for the value mid
        // If this can be maximum value of sweetness
        if (countPartitions(N, arr, mid) >= K) {
            low = mid;
        }
 
        // If cannot be the value
        else {
            high = mid - 1;
        }
    }
 
    if (countPartitions(N, arr, high) >= K) {
        return high;
    }
 
    return low;
}
 
// Driver code
int main()
{
    int N = 3;
    int K = 2;
    int arr[] = { 1, 2, 4 };
 
    // Function call
    cout << maxSweetness(N, K, arr);
 
    return 0;
}


Java




import java.util.Scanner;
 
public class Main {
    // Function to count the number of partitions with sweetness value greater than or equal to val
    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;
    }
 
    // Function to find maximum sweetness by dividing them into K groups
    static int maxSweetness(int N, int K, int[] arr) {
        int low = 0;
        int high = 0;
 
        // Set the high value
        for (int i = 0; i < N; i++) {
            high += arr[i];
        }
 
        // Iterate between low and high
        while ((high - low) > 1) {
            int mid = (high + low) >>> 1;
 
            // Check for the value mid
            // If this can be the maximum value of sweetness
            if (countPartitions(N, arr, mid) >= K) {
                low = mid;
            } else {
                // If it cannot be the value
                high = mid - 1;
            }
        }
 
        if (countPartitions(N, arr, high) >= K) {
            return high;
        }
 
        return low;
    }
 
    // Driver code
    public static void main(String[] args) {
        int N = 3;
        int K = 2;
        int[] arr = {1, 2, 4};
 
        // Function call
        System.out.println(maxSweetness(N, K, arr));
    }
}
 
 
// This code is contributed by rambabuguphka


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
 
# Driver code
N = 3
K = 2
arr = [1, 2, 4]
 
# Function call
print(max_sweetness(N, K, arr))


C#




using System;
 
public class Program
{
    // Function to count the number of partitions with sweetness value greater than or equal to val
    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;
    }
 
    // Function to find maximum sweetness by dividing them into K groups
    static int MaxSweetness(int N, int K, int[] arr)
    {
        int low = 0;
        int high = 0;
 
        // Set the high value
        for (int i = 0; i < N; i++)
        {
            high += arr[i];
        }
 
        // Iterate between low and high
        while ((high - low) > 1)
        {
            int mid = (high + low) >> 1;
 
            // Check for the value mid
            // If this can be the maximum value of sweetness
            if (CountPartitions(N, arr, mid) >= K)
            {
                low = mid;
            }
            else
            {
                // If it cannot be the value
                high = mid - 1;
            }
        }
 
        if (CountPartitions(N, arr, high) >= K)
        {
            return high;
        }
 
        return low;
    }
 
    // Entry point
    public static void Main(string[] args)
    {
        int N = 3;
        int K = 2;
        int[] arr = { 1, 2, 4 };
 
        // Function call
        Console.WriteLine(MaxSweetness(N, K, arr));
    }
}


Javascript




// Function to count the number of partitions with sweetness value greater than or equal to val
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 to find maximum sweetness by dividing them into K groups
function maxSweetness(N, K, arr) {
    let low = 0;
    let high = 0;
 
    // Set the high value
    for (let i = 0; i < N; i++) {
        high += arr[i];
    }
 
    // Iterate between low and high
    while ((high - low) > 1) {
        let mid = Math.floor((high + low) / 2);
 
        // Check for the value mid
        // If this can be the maximum value of sweetness
        if (countPartitions(N, arr, mid) >= K) {
            low = mid;
        } else {
            // If it cannot be the value
            high = mid - 1;
        }
    }
 
    if (countPartitions(N, arr, high) >= K) {
        return high;
    }
 
    return low;
}
 
// Driver code
let inputN = 3;
let inputK = 2;
let inputArr = [1, 2, 4];
 
// Function call
console.log(maxSweetness(inputN, inputK, inputArr));


Output

3

Complexity Analysis:

Time Complexity: O(N log (sum (arr))), Where N is the size of the array arr.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads