Minimum Number of Coins in a Bag
Last Updated :
29 Dec, 2023
Given an array arr[] where each element represents the number of coins in the bag, also given an integer k, minimize the maximum number of coins present in any bag using at most k operations, In each operation the coins in one bag can be distributed into two bags.
Examples:
Input: arr[] = [9], k = 2
Output: 3
Explanation:
- Divide the bag with 9 coins into two bags of sizes 6 and 3. [9] -> [6,3].
- Divide the bag with 6 coins into two bags of sizes 3 and 3. [6,3] -> [3,3,3].
- The bag with the most number of coins has 3 coins in it.
Input: arr[] = [3, 4, 8], k = 2
Output: 4
Explantion:
- Divide the bag with 8 coins into two bags of sizes 4 and 4. [3, 4, 8] -> [3, 4, 4, 4].
- Divide the bag with 4 coins into two bags of sizes 3 and 1. [3, 4, 4, 4] -> [3, 4, 4, 3, 1].
- The bag with the most number of coins has 4 coins in it.
Approach: To solve the problem follow the below idea:
This problem can be solved using the Binary search , since we need to minimize the maximum bag size we can apply the binary search on the bag size since the maximum bag size can’t be more the maximum element in the array. So applying the binary on the size of the bag and check if we split in that particular size we were able to finish it in less than equal to k operations while minimising it.
Follow these steps to solve the above problem:
- Initialize low as mid and high as max value based on the constraint
- Perform binary search while low<=mid
- In the validBagSplit() function count the operations required for split the each bag into bag size of mid.
- If the bag size is exactly divisible by mid then (arr[i] / mid) – 1 more operations need to split into extra (arr[i] / mid) – 1 bags.
- Else it need arr[i] / mid more operations to split to more (arr[i] / mid) bags.
- Check if dividing each bag in the size of mid is consuming greater than k operations, if its consuming assign the low as mid +1.
- If dividing each bag in the size of mid is consuming less than equal to k operations check if we can minimize the bag size and store it in the result variable.
- Return the result.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool validBagSplit(vector< int >& arr, int k, int mid)
{
int operations = 0;
for ( int i = 0; i < arr.size(); i++) {
if (arr[i] % mid == 0) {
operations += arr[i] / mid - 1;
}
else
operations += arr[i] / mid;
}
if (operations <= k)
return true ;
return false ;
}
int minimizedMaxbagsize(vector< int >& arr, int k)
{
int low = 1;
int high = 99999;
int res = high;
while (low <= high) {
int mid = low + (high - low) / 2;
if (!validBagSplit(arr, k, mid))
low = mid + 1;
else {
high = mid - 1;
res = min(res, mid);
}
}
return res;
}
int main()
{
vector< int > arr = { 9 };
int k = 2;
cout << minimizedMaxbagsize(arr, k);
return 0;
}
|
Java
import java.util.*;
public class Main {
static boolean validBagSplit(ArrayList<Integer> arr,
int k, int mid)
{
int operations = 0 ;
for ( int i = 0 ; i < arr.size(); i++) {
if (arr.get(i) % mid == 0 ) {
operations += arr.get(i) / mid - 1 ;
}
else
operations += arr.get(i) / mid;
}
return operations <= k;
}
static int minimizedMaxBagSize(ArrayList<Integer> arr,
int k)
{
int low = 1 ;
int high = 99999 ;
int res = high;
while (low <= high) {
int mid = low + (high - low) / 2 ;
if (!validBagSplit(arr, k, mid))
low = mid + 1 ;
else {
high = mid - 1 ;
res = Math.min(res, mid);
}
}
return res;
}
public static void main(String[] args)
{
ArrayList<Integer> arr
= new ArrayList<>(Arrays.asList( 9 ));
int k = 2 ;
System.out.println(minimizedMaxBagSize(arr, k));
}
}
|
Python3
def valid_bag_split(arr, k, mid):
operations = 0
for i in range ( len (arr)):
if arr[i] % mid = = 0 :
operations + = arr[i] / / mid - 1
else :
operations + = arr[i] / / mid
return operations < = k
def minimized_max_bag_size(arr, k):
low = 1
high = 99999
res = high
while low < = high:
mid = low + (high - low) / / 2
if not valid_bag_split(arr, k, mid):
low = mid + 1
else :
high = mid - 1
res = min (res, mid)
return res
arr = [ 9 ]
k = 2
print (minimized_max_bag_size(arr, k))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static bool ValidBagSplit(List< int > arr, int k, int mid)
{
int operations = 0;
foreach ( int val in arr)
{
if (val % mid == 0)
{
operations += val / mid - 1;
}
else
{
operations += val / mid;
}
}
return operations <= k;
}
static int MinimizedMaxBagSize(List< int > arr, int k)
{
int low = 1;
int high = 99999;
int res = high;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (!ValidBagSplit(arr, k, mid))
{
low = mid + 1;
}
else
{
high = mid - 1;
res = Math.Min(res, mid);
}
}
return res;
}
public static void Main()
{
List< int > arr = new List< int > { 9 };
int k = 2;
Console.WriteLine(MinimizedMaxBagSize(arr, k));
}
}
|
Javascript
<script>
function validBagSplit(arr, k, mid) {
let operations = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] % mid === 0) {
operations += arr[i] / mid - 1;
} else {
operations += Math.floor(arr[i] / mid);
}
}
return operations <= k;
}
function minimizedMaxBagSize(arr, k) {
let low = 1;
let high = 99999;
let res = high;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
if (!validBagSplit(arr, k, mid)) {
low = mid + 1;
} else {
high = mid - 1;
res = Math.min(res, mid);
}
}
return res;
}
const arr = [9];
const k = 2;
console.log(minimizedMaxBagSize(arr, k));
</script>
|
Time Complexity: O(n*log n) where n is the size of the array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...