Check if all objects of type A and B can be placed on N shelves
Last Updated :
11 Feb, 2022
Given two integers A and B, representing the count of objects of two different types, and another integer N which represents the number of shelves, the task is to place all objects in the given N shelves abiding by the following rules:
- Any shelf cannot contain both Type-A and Type-B objects at the same time.
- No shelf can contain more than K objects of Type-A or L objects of type B.
If it is possible to place all the items in N shelves, print “YES”. Otherwise, print “NO”.
Examples:
Input: A = 3, B = 3, N = 3, K = 4, M = 2
Output: YES
Explanation:
3 Type-A items can be placed on 1 shelf, as maximum limit is 4.
3 Type-B items can be placed on 2 shelves, as maximum limit is 2.
Since the required number of shelves does not exceed N, so allocation is possible.
Input: A = 6, B = 7, N = 3, K = 4, L = 5
Output: NO
Explanation:
6 Type-A items require 2 shelves, as maximum limit is 4.
7 Type-B items require 2 shelves, as maximum limit is 5.
Since the required number of shelves exceeds N, so allocation is not possible.
Approach:
To solve the problem, we need to count the minimum number of shelves required to place all objects and check if it exceeds N or not. Follow the steps below:
- Count the minimum number of items required to place Type-A items, say needa. Since, K Type-A items can be placed at most in a single shelf, following two conditions arise:
- If A is divisible by K, all Type-A items can be placed in A / K shelves.
- Otherwise, A % K items needs to be placed in 1 shelf and the rest in A / K shelves.Hence A/ K + 1 shelves are required for this case.
- Similarly, calculate the minimum number of shelves required to place Type-B items, say needb.
- If needa + needb exceeds N, allocation is not possible. Otherwise, it is possible.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int A, int B, int N,
int K, int L)
{
int needa, needb;
if (A % K == 0)
needa = A / K;
else
needa = A / K + 1;
if (B % L == 0)
needb = B / L;
else
needb = B / L + 1;
int total = needa + needb;
if (total > N)
return false ;
else
return true ;
}
int main()
{
int A = 3, B = 3, N = 3;
int K = 4, M = 2;
if (isPossible(A, B, N, K, M))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
Java
class GFG{
static boolean isPossible( int A, int B,
int N, int K,
int L)
{
int needa, needb;
if (A % K == 0 )
needa = A / K;
else
needa = A / K + 1 ;
if (B % L == 0 )
needb = B / L;
else
needb = B / L + 1 ;
int total = needa + needb;
if (total > N)
return false ;
else
return true ;
}
public static void main(String[] args)
{
int A = 3 , B = 3 , N = 3 ;
int K = 4 , M = 2 ;
if (isPossible(A, B, N, K, M))
System.out.print( "YES" + "\n" );
else
System.out.print( "NO" + "\n" );
}
}
|
Python3
def isPossible(A, B, N, K, L):
needa = 0
needb = 0
if (A % K = = 0 ):
needa = A / / K;
else :
needa = A / / K + 1
if (B % L = = 0 ):
needb = B / / L
else :
needb = B / / L + 1
total = needa + needb
if (total > N):
return False
else :
return True
if __name__ = = '__main__' :
A, B, N = 3 , 3 , 3
K, M = 4 , 2
if (isPossible(A, B, N, K, M)):
print ( 'YES' )
else :
print ( 'NO' )
|
C#
using System;
class GFG{
static bool isPossible( int A, int B,
int N, int K,
int L)
{
int needa, needb;
if (A % K == 0)
needa = A / K;
else
needa = A / K + 1;
if (B % L == 0)
needb = B / L;
else
needb = B / L + 1;
int total = needa + needb;
if (total > N)
return false ;
else
return true ;
}
public static void Main(String[] args)
{
int A = 3, B = 3, N = 3;
int K = 4, M = 2;
if (isPossible(A, B, N, K, M))
Console.Write( "YES" + "\n" );
else
Console.Write( "NO" + "\n" );
}
}
|
Javascript
<script>
function isPossible(A, B, N, K, L)
{
let needa, needb;
if (A % K == 0)
needa = Math.floor(A / K);
else
needa = Math.floor(A / K) + 1;
if (B % L == 0)
needb = Math.floor(B / L);
else
needb = Math.floor(B / L) + 1;
let total = needa + needb;
if (total > N)
return false ;
else
return true ;
}
let A = 3, B = 3, N = 3;
let K = 4, M = 2;
if (isPossible(A, B, N, K, M))
document.write( "YES" + "<br/>" );
else
document.write( "NO" + "<br/>" );
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...