Find if neat arrangement of cups and shelves can be made in C++


Concept

With respect of given three different types of cups (p[]) and saucers (q[]), and m numberof shelves, determine if neat arrangement of cups and shelves can be made.

Now, arrangement of the cups and saucers will be neat if it follows the following rules −

  • According to first rule, no shelf can contain both cups and saucers.
  • According to second rule, there can be no more than 5 cups in any shelf.
  • According to third rule, there can be no more than 10 saucers in any shelf.

Input

p[] = {4, 3, 7}
q[] = {5, 9, 10}
m = 11

Output

Yes

Explanation

Total cups = 14, shelves required = 3

Total saucers = 24, shelves required = 3

So, total required shelves = 3 + 3 = 6,

which is smaller than given number of shelves m. So, output is Yes.

Input

p[] = {5, 8, 5}
q[] = {4, 10, 11}
m = 3

Output

No

Total cups = 18, shelves required = 4

Total saucers = 25, shelves required = 3

So, total required shelves = 4 + 3 = 7,

which is larger than given number of shelves m. So, output is No.

Method

For arranging the cups and the saucers, determine the total number of cups p and total number of saucers q. Because, it is not possible to be more than 5 cups in the sameshelf, therefore determine the maximum number of shelves required for cup by the formula (p+5-1)/5 and the maximum number of shelves required for saucers by implementing the formula (q+10-1)/10. It has been seen that if sum of these two values is equal to or smaller than m then the arrangement is possible else not.

Example

 Live Demo

// C++ code to find if neat
// arrangement of cups and
// shelves can be made
#include<bits/stdc++.h>
using namespace std;
// Shows function to check arrangement
void canArrange1(int p[], int q[], int m){
   int sump = 0, sumq = 0;
   // Used to calculate total number
   // of cups
   for(int i = 0; i < 3; i++)
      sump += p[i];
   // Used to calculate total number
   // of saucers
   for(int i = 0; i < 3; i++)
      sumq += q[i];
   // Now adding 5 and 10 so that if the
   // total sum is smaller than 5 and
   // 10 then we can get 1 as the
   // answer and not 0
   int mp = (sump + 5 - 1) / 5;
   int mq = (sumq + 10 - 1) / 10;
   if(mp + mq <= m)
      cout << "Yes";
   else
      cout << "No";
}
// Driver code
int main(){
   // Shows number of cups of each type
   int p[] = {4, 3, 7};
   // Shows number of saucers of each type
   int q[] = {5, 9, 10};
   // Shows number of shelves
   int m = 10;
   // ndicates calling function
   canArrange1(p, q, m);
   return 0;
}

Output

Yes

Updated on: 24-Jul-2020

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements