Techno Blender
Digitally Yours.

Minimize rooms with K seats to accomodate N girls and M boys

0 54


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given three positive integers N, M, and K, the task is to find the minimum number of rooms required to accommodate all students if there are N girls and M boys, and each room has K seats. It is not allowed for a boy and a girl to stay in the same room.

Examples:

Input: N = 13, M = 7, K = 2
Output: 11
Explanation: Room required for girls = 7 (2 + 2 + 2 + 2 + 2 + 2 + 1) Room required for boys = 4 (2 + 2 + 2 + 1)

Input: N = 5, M = 5, K = 3
Output: 4
Explanation: Rooms required for girls = 2 (3 + 2) Room required for boys = 2 (3 + 2). So, we output total seats 2 + 2 = 4

Approach: This can be solved with the following idea:

This can be solved by mathematical observation. Calculating rooms required for boys and girls separately.

Steps involved in the implementation of code:

  • If the number of students in a particular gender is exactly divisible by the number of seats in a room, then the number of rooms required for that gender can be calculated as the integer division of the number of students in that gender by the number of seats in a room.
  • If the number of students in a particular gender is not exactly divisible by the number of seats in a room, then the number of rooms required for that gender can be calculated as the integer division of the number of students in that gender by the number of seats in a room, plus one. This is because an additional room is required to accommodate the remaining students.
  • Steps 1 and 2 can be applied separately for boys and girls to calculate the minimum number of rooms required for each gender.
  • The minimum number of rooms required to accommodate all students can be calculated as the maximum of the minimum number of rooms required for boys and girls, i.e., answer = max(ceil(M/K), ceil(N/K)).
  • Finally, return the answer as the minimum number of rooms required to accommodate all students.

Below is the implementation of the code:

C++14

#include <iostream>

using namespace std;

  

int totalRooms(int n, int m, int k)

{

  

    int count = 0;

  

    

    count += (n / k);

  

    

    if (n % k != 0) {

        count++;

    }

  

    

    count += (m / k);

  

    

    if (m % k != 0) {

        count++;

    }

  

    return count;

}

  

int main()

{

  

    int n = 13;

    int m = 7;

    int k = 2;

  

    

    cout << totalRooms(n, m, k);

    return 0;

}

Time Complexity: O(1)
Auxiliary Space: O(1)


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given three positive integers N, M, and K, the task is to find the minimum number of rooms required to accommodate all students if there are N girls and M boys, and each room has K seats. It is not allowed for a boy and a girl to stay in the same room.

Examples:

Input: N = 13, M = 7, K = 2
Output: 11
Explanation: Room required for girls = 7 (2 + 2 + 2 + 2 + 2 + 2 + 1) Room required for boys = 4 (2 + 2 + 2 + 1)

Input: N = 5, M = 5, K = 3
Output: 4
Explanation: Rooms required for girls = 2 (3 + 2) Room required for boys = 2 (3 + 2). So, we output total seats 2 + 2 = 4

Approach: This can be solved with the following idea:

This can be solved by mathematical observation. Calculating rooms required for boys and girls separately.

Steps involved in the implementation of code:

  • If the number of students in a particular gender is exactly divisible by the number of seats in a room, then the number of rooms required for that gender can be calculated as the integer division of the number of students in that gender by the number of seats in a room.
  • If the number of students in a particular gender is not exactly divisible by the number of seats in a room, then the number of rooms required for that gender can be calculated as the integer division of the number of students in that gender by the number of seats in a room, plus one. This is because an additional room is required to accommodate the remaining students.
  • Steps 1 and 2 can be applied separately for boys and girls to calculate the minimum number of rooms required for each gender.
  • The minimum number of rooms required to accommodate all students can be calculated as the maximum of the minimum number of rooms required for boys and girls, i.e., answer = max(ceil(M/K), ceil(N/K)).
  • Finally, return the answer as the minimum number of rooms required to accommodate all students.

Below is the implementation of the code:

C++14

#include <iostream>

using namespace std;

  

int totalRooms(int n, int m, int k)

{

  

    int count = 0;

  

    

    count += (n / k);

  

    

    if (n % k != 0) {

        count++;

    }

  

    

    count += (m / k);

  

    

    if (m % k != 0) {

        count++;

    }

  

    return count;

}

  

int main()

{

  

    int n = 13;

    int m = 7;

    int k = 2;

  

    

    cout << totalRooms(n, m, k);

    return 0;

}

Time Complexity: O(1)
Auxiliary Space: O(1)

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment