Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIFind maximum height to cut all chocolates horizontally such that at least...

Find maximum height to cut all chocolates horizontally such that at least K amount remains

Given an array arr[] consisting of heights of N chocolate bars, the task is to find the maximum height at which the horizontal cut is made to all the chocolates such that the sum remaining amount of chocolate is at least K.

Examples:

Input: K = 7, arr[] = [15, 20, 8, 17]
Output: 15
Explanation:
Suppose a cut is made at height 8:
-> chocolate taken from 1st chocolate bar = 15 – 8 =7
-> chocolate taken from 2nd chocolate bar = 20 – 8 =12
-> chocolate taken from 3rd chocolate bar = 8 – 8 = 0
-> chocolate taken from 4th chocolate bar = 17 – 8 = 9
=> Total chocolate wasted = (7 + 12 + 0 + 9) – K = 28 – 7 = 21

Suppose a cut is made at height 15:
-> chocolate taken from 1st chocolate bar = 15 – 15 = 0
-> chocolate taken from 2nd chocolate bar = 20 – 15 = 5
-> 3rd chocolate bar wont be chosen as it is less than 15
-> chocolate taken from 4th chocolate bar = 17 – 15 = 2
=> Total chocolate wasted = (0 + 5 + 2) – K = 7 – 7 = 0

Therefore when we take chocolate of height 15 then chocolate wasted is minimum. Therefore 15 is the answer.

Input: K = 12, arr[] = [30, 25, 22, 17, 20]
Output: 21
Explanation:

After a cut at height 18, the chocolate removed is 25 and chocolate wastage is (25 – 12) = 13 units. But if the cut is made at height 21 is made then 14 units of chocolate is removed and the wastage is (14 – 12) = 2 which is the least, hence 21 is the answer

Approach: The given problem can be solved based on Binary Search

The idea is to perform the Binary Search over then range [0, max element of the array] and find that value in the range, say M, such that the sum of remaining chocolate after making the horizontal cut at M gives minimum difference with K

Follow the steps below to solve the given problem:

  • Initialize two variables, say low and high as 0 and the maximum array element respectively.
  • Iterate until low <= high and perform the following steps:
    • Find the value of mid as (low + high)/2.
    • Find the sum of remaining chocolates after making the horizontal cut at height mid as M.
    • If the value of M is less than K, then update the value of high as (mid – 1). Otherwise, update the value of low as (mid + 1).
  • After performing the above steps, print the value as high as the resultant maximum height that must be cut.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the sum of remaining
// chocolate after making the horizontal
// cut at height mid
int cal(vector<int> arr, int mid)
{
 
    // Stores the sum of chocolates
    int chocolate = 0;
 
    // Traverse the array arr[]
    for (auto i : arr) {
 
        // If the height is at least mid
        if (i >= mid)
            chocolate += i - mid;
    }
 
    // Return the possible sum
    return chocolate;
}
 
// Function to find the maximum horizontal
// cut made to all the chocolates such that
// the sum of the remaining element is
// at least K
int maximumCut(vector<int> arr, int K)
{
 
    // Ranges of Binary Search
    int low = 0;
    int high = *max_element(arr.begin(), arr.end());
 
    // Perform the Binary Search
    while (low <= high) {
        int mid = (low + high) / 2;
 
        // Find the sum of removed after
        // making cut at height mid
        int chocolate = cal(arr, mid);
 
        // If the chocolate removed is
        // same as the chocolate needed
        // then return the height
        if (chocolate == K)
            return mid;
 
        // If the chocolate removed is
        // less than chocolate needed
        // then shift to the left range
        else if (chocolate < K)
            high = mid - 1;
 
        // Otherwise, shift to the right
        // range
        else {
            low = mid + 1;
            if (mid > high)
                high = mid;
        }
    }
 
    // Return the possible cut
    return high;
}
 
// Driver Code
int main()
{
    int N = 4;
    int K = 7;
    vector<int> arr{ 15, 20, 8, 17 };
    cout << (maximumCut(arr, K));
}
 
// This code is contributed by ipg2016107.


Java




// Java program for the above approach
import java.util.*;
import java.util.Arrays;
class GFG {
 
    // Function to find the sum of remaining
    // chocolate after making the horizontal
    // cut at height mid
    static int cal(int arr[], int mid)
    {
 
        // Stores the sum of chocolates
        int chocolate = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < arr.length; i++) {
 
            // If the height is at least mid
            if (arr[i] >= mid)
                chocolate += arr[i] - mid;
        }
 
        // Return the possible sum
        return chocolate;
    }
 
    // Function to find the maximum horizontal
    // cut made to all the chocolates such that
    // the sum of the remaining element is
    // at least K
    static int maximumCut(int arr[], int K)
    {
 
        // Ranges of Binary Search
        int low = 0;
        int high = Arrays.stream(arr).max().getAsInt();
 
        // Perform the Binary Search
        while (low <= high) {
            int mid = (low + high) / 2;
 
            // Find the sum of removed after
            // making cut at height mid
            int chocolate = cal(arr, mid);
 
            // If the chocolate removed is
            // same as the chocolate needed
            // then return the height
            if (chocolate == K)
                return mid;
 
            // If the chocolate removed is
            // less than chocolate needed
            // then shift to the left range
            else if (chocolate < K)
                high = mid - 1;
 
            // Otherwise, shift to the right
            // range
            else {
                low = mid + 1;
                if (mid > high)
                    high = mid;
            }
        }
 
        // Return the possible cut
        return high;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int K = 7;
        int arr[] = { 15, 20, 8, 17 };
        System.out.println(maximumCut(arr, K));
    }
}
 
// This code is contributed by ukasp.


Python3




# Python program for the above approach
 
# Function to find the sum of remaining
# chocolate after making the horizontal
# cut at height mid
 
 
def cal(arr, mid):
 
      # Stores the sum of chocolates
    chocolate = 0
 
    # Traverse the array arr[]
    for i in arr:
 
          # If the height is at least mid
        if i >= mid:
            chocolate += i - mid
 
    # Return the possible sum
    return chocolate
 
# Function to find the maximum horizontal
# cut made to all the chocolates such that
# the sum of the remaining element is
# at least K
 
 
def maximumCut(arr, K):
 
      # Ranges of Binary Search
    low = 0
    high = max(arr)
 
    # Perform the Binary Search
    while low <= high:
        mid = (low + high) // 2
 
        # Find the sum of removed after
        # making cut at height mid
        chocolate = cal(arr, mid)
 
        # If the chocolate removed is
        # same as the chocolate needed
        # then return the height
        if chocolate == K:
            return mid
 
        # If the chocolate removed is
        # less than chocolate needed
        # then shift to the left range
        elif chocolate < K:
            high = mid - 1
 
        # Otherwise, shift to the right
        # range
        else:
            low = mid + 1
            if mid > high:
                high = mid
 
    # Return the possible cut
    return high
 
 
# Driver Code
N, K = 4, 7
arr = [15, 20, 8, 17]
 
print(maximumCut(arr, K))


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG {
 
    // Function to find the sum of remaining
    // chocolate after making the horizontal
    // cut at height mid
    static int cal(List<int> arr, int mid)
    {
 
        // Stores the sum of chocolates
        int chocolate = 0;
 
        // Traverse the array arr[]
        foreach(int i in arr)
        {
 
            // If the height is at least mid
            if (i >= mid)
                chocolate += i - mid;
        }
 
        // Return the possible sum
        return chocolate;
    }
 
    // Function to find the maximum horizontal
    // cut made to all the chocolates such that
    // the sum of the remaining element is
    // at least K
    static int maximumCut(List<int> arr, int K)
    {
 
        // Ranges of Binary Search
        int low = 0;
        int high = arr.Max();
 
        // Perform the Binary Search
        while (low <= high) {
            int mid = (low + high) / 2;
 
            // Find the sum of removed after
            // making cut at height mid
            int chocolate = cal(arr, mid);
 
            // If the chocolate removed is
            // same as the chocolate needed
            // then return the height
            if (chocolate == K)
                return mid;
 
            // If the chocolate removed is
            // less than chocolate needed
            // then shift to the left range
            else if (chocolate < K)
                high = mid - 1;
 
            // Otherwise, shift to the right
            // range
            else {
                low = mid + 1;
                if (mid > high)
                    high = mid;
            }
        }
 
        // Return the possible cut
        return high;
    }
 
    // Driver Code
    public static void Main()
    {
        int K = 7;
        List<int> arr = new List<int>() { 15, 20, 8, 17 };
        Console.Write(maximumCut(arr, K));
    }
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to find the sum of remaining
        // chocolate after making the horizontal
        // cut at height mid
        function cal(arr, mid) {
 
            // Stores the sum of chocolates
            let chocolate = 0
 
            // Traverse the array arr[]
            for (let i = 0; i < arr.length; i++) {
 
                // If the height is at least mid
                if (arr[i] >= mid)
                    chocolate += arr[i] - mid
            }
 
            // Return the possible sum
            return chocolate
        }
 
        // Function to find the maximum horizontal
        // cut made to all the chocolates such that
        // the sum of the remaining element is
        // at least K
        function maximumCut(arr, K) {
 
            // Ranges of Binary Search
            let low = 0
            let high = arr[0];
 
            for (let i = 1; i < arr.length; i++) {
                high = Math.max(high, arr[i]);
            }
 
            // Perform the Binary Search
            while (low <= high) {
                mid = Math.floor((low + high) / 2);
 
                // Find the sum of removed after
                // making cut at height mid
                chocolate = cal(arr, mid)
 
                // If the chocolate removed is
                // same as the chocolate needed
                // then return the height
                if (chocolate == K) {
                    return mid
                }
                // If the chocolate removed is
                // less than chocolate needed
                // then shift to the left range
                else if (chocolate < K) {
                    high = mid - 1
                }
 
                // Otherwise, shift to the right
                // range
                else {
                    low = mid + 1
                    if (mid > high)
                        high = mid
                }
            }
             
            // Return the possible cut
            return high
        }
         
        // Driver Code
        let N = 4;
        let K = 7;
        let arr = [15, 20, 8, 17];
 
        document.write(maximumCut(arr, K))
 
     // This code is contributed by Potta Lokesh
    </script>


Output

15

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments