Maximize card deck count that can be formed from cards of given type and joker
Last Updated :
18 Oct, 2021
Given an integer N denoting the numbers of cards in a specific deck and an array arr[] of size N where ith element denotes the frequency of the ith type of card. Also given K Jokers. The task is to find the maximum possible valid decks with given data.
A valid deck should follow one of the two mentioned conditions:
1. A deck containing exactly one card of each type, and no jokers.
2. A deck containing exactly one card of each type except one, and exactly one joker.
Examples:
Input: N = 2, arr[] = {10, 15}, K = 3
Output: 13
Explanation: Below are the ways in which 13 decks are made:
10 decks of type {1, 2}
3 decks of type {J, 2}
Therefore maximum possible number of decks are 10 + 3 = 13 decks.
Input: N = 3, arr[] = {1, 2, 3}, K = 4
Output: 3
Explanation: Below are the ways in which 13 decks are made:
1 deck of type {1, 2, 3}
1 deck of type {J, 2, 3}
1 deck of type {J, 2, 3}
Therefore maximum possible number of decks are 1+1+1 = 3 decks.
Approach: The given problem can be solved by using Greedy Approach and Binary Search algorithm. Follow the steps below to solve the given problem.
- Sort the given array arr[] in non-decreasing order.
- binary search can be applied on the answer to get the maximum value.
- Initialize two variables say, L = 0 and R = max_element(arr) + K + 1 that will define initial range of answer to be found out.
- Iterate while L +1 < R
- Initialize a variable say mid = (L + R)/2 to keep track of the middle element in range at each iteration.
- Use a variable say, need = 0 to count the extra cards needed for the current mid element.
- iterate whole array arr[] with variable i:
- if arr[i] < mid set need = need + arr[i] – mid.
- if need <= mid and need <= k, set L = mid.
- otherwise set R = mid.
- At last final answer will be stored in L, Therefore return L as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumCardDecks( int arr[], int n, int k)
{
sort(arr, arr + n);
int L = 0;
int R = arr[n - 1] + k + 1;
while (L + 1 < R) {
int mid = (L + R) / 2;
int need = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] < mid) {
need += mid - arr[i];
}
}
if (need <= mid && need <= k) {
L = mid;
}
else {
R = mid;
}
}
return L;
}
int main()
{
int N = 3, K = 4;
int arr[] = { 1, 2, 3 };
cout << maximumCardDecks(arr, N, K);
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
static int maximumCardDecks( int arr[], int n, int k)
{
Arrays.sort(arr);
int L = 0 ;
int R = arr[n - 1 ] + k + 1 ;
while (L + 1 < R)
{
int mid = (L + R) / 2 ;
int need = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] < mid) {
need += mid - arr[i];
}
}
if (need <= mid && need <= k) {
L = mid;
}
else {
R = mid;
}
}
return L;
}
public static void main (String[] args)
{
int N = 3 , K = 4 ;
int arr[] = { 1 , 2 , 3 };
System.out.print(maximumCardDecks(arr, N, K));
}
}
|
Python3
def maximumCardDecks(arr, n, k):
arr.sort()
L = 0
R = arr[n - 1 ] + k + 1
while (L + 1 < R) :
mid = (L + R) / / 2
need = 0
for i in range (n):
if (arr[i] < mid):
need + = mid - arr[i]
if (need < = mid and need < = k):
L = mid
else :
R = mid
return L
N = 3
K = 4
arr = [ 1 , 2 , 3 ]
print (maximumCardDecks(arr, N, K))
|
C#
using System;
class GFG
{
static int maximumCardDecks( int []arr, int n, int k)
{
Array.Sort(arr);
int L = 0;
int R = arr[n - 1] + k + 1;
while (L + 1 < R)
{
int mid = (L + R) / 2;
int need = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] < mid) {
need += mid - arr[i];
}
}
if (need <= mid && need <= k) {
L = mid;
}
else {
R = mid;
}
}
return L;
}
public static void Main (String[] args)
{
int N = 3, K = 4;
int []arr = { 1, 2, 3 };
Console.Write(maximumCardDecks(arr, N, K));
}
}
|
Javascript
<script>
function maximumCardDecks(arr, n, k)
{
arr.sort( function (a, b) { return a - b })
let L = 0;
let R = arr[n - 1] + k + 1;
while (L + 1 < R) {
let mid = (L + R) / 2;
let need = 0;
for (let i = 0; i < n; i++) {
if (arr[i] < mid) {
need += mid - arr[i];
}
}
if (need <= mid && need <= k) {
L = mid;
}
else {
R = mid;
}
}
return L;
}
let N = 3, K = 4;
let arr = [1, 2, 3];
document.write(maximumCardDecks(arr, N, K));
</script>
|
Time Complexity: O(N(log(N) + log(M + K)), where M is maximum element of the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...