Minimize rooms with K seats to accommodate N girls and M boys
Last Updated :
06 Jul, 2023
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;
}
|
Java
import java.util.*;
class Main {
public static void main(String[] args)
{
int n = 13 ;
int m = 7 ;
int k = 2 ;
int count = 0 ;
count += (n / k);
if (n % k != 0 ) {
count++;
}
count += (m / k);
if (m % k != 0 ) {
count++;
}
System.out.println(count);
}
}
|
Python
n = 13
m = 7
k = 2
count = 0
count + = (n / / k)
if (n % k ! = 0 ):
count + = 1
count + = (m / / k)
if (m % k ! = 0 ):
count + = 1
print (count)
|
C#
using System;
public class GFG {
public static void Main()
{
int n = 13;
int m = 7;
int k = 2;
int count = 0;
count += (n / k);
if (n % k != 0) {
count++;
}
count += (m / k);
if (m % k != 0) {
count++;
}
Console.WriteLine(count);
}
}
|
Javascript
function totalRooms(n, m, k) {
let count = 0;
count += Math.floor(n / k);
if (n % k !== 0) {
count++;
}
count += Math.floor(m / k);
if (m % k !== 0) {
count++;
}
return count;
}
const n = 13;
const m = 7;
const k = 2;
console.log(totalRooms(n, m, k));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...