Open In App

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

Last Updated : 06 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ Implementation
#include <iostream>
using namespace std;
 
// Function to calculate rooms required
int totalRooms(int n, int m, int k)
{
 
    int count = 0;
 
    // For girls
    count += (n / k);
 
    // Extra space required
    if (n % k != 0) {
        count++;
    }
 
    // For boys
    count += (m / k);
 
    // Extra space required
    if (m % k != 0) {
        count++;
    }
 
    return count;
}
 
// Driver code
int main()
{
 
    int n = 13;
    int m = 7;
    int k = 2;
 
    // Function call
    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;
 
        // Function to calculate rooms required
        int count = 0;
 
        // For girls
        count += (n / k);
 
        // Extra space required
        if (n % k != 0) {
            count++;
        }
 
        // For boys
        count += (m / k);
 
        // Extra space required
        if (m % k != 0) {
            count++;
        }
 
        // Print the total number of rooms required
        System.out.println(count);
    }
}


Python




n = 13 # n=girl
m = 7 # m=boy
k = 2 # k=number of seats in each room
 
count = 0 # declearing count variable
 
''' Checking for girls '''
 
count += (n//k)
 
if (n%k !=0):
    count+=1
 
 
''' Checking for boys '''
 
count += (m//k)
 
if (m % k != 0):
    count+=1
 
print(count) # print the count


C#




// C# Implementation
using System;
 
public class GFG {
    public static void Main()
    {
        int n = 13;
        int m = 7;
        int k = 2;
 
        // Function to calculate rooms required
        int count = 0;
 
        // For girls
        count += (n / k);
 
        // Extra space required
        if (n % k != 0) {
            count++;
        }
 
        // For boys
        count += (m / k);
 
        // Extra space required
        if (m % k != 0) {
            count++;
        }
 
        // Print the total number of rooms required
        Console.WriteLine(count);
    }
}
 
// This code is contributed by Pushpesh Raj


Javascript




function totalRooms(n, m, k) {
  let count = 0;
 
  // For girls
  count += Math.floor(n / k);
 
  // Extra space required
  if (n % k !== 0) {
    count++;
  }
 
  // For boys
  count += Math.floor(m / k);
 
  // Extra space required
  if (m % k !== 0) {
    count++;
  }
 
  return count;
}
 
// Driver code
const n = 13;
const m = 7;
const k = 2;
 
// Function call
console.log(totalRooms(n, m, k));


Output

11

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads