Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Place N boys and M girls in different rows such that count of persons placed in each row is maximized

  • Last Updated : 31 Mar, 2021

Given two integers N and M representing the number of boys and girls, the task is to arrange them in number of different rows of same size such that each row contains the maximum number of students possible and each row should contain either boys or girls.
Note: No row can contain both boys and girls.

Example:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: N = 4, M = 2
Output: 2
Explanation:
The following order of arrangement satisfies the given conditions:
1st Row: B1, B2 
2nd Row: B3, B4
3rd Row: G1, G2
Clearly, every row has either boys or girls.

Input: N = 6, M = 6
Output: 6
Explanation:
The following order of arrangement satisfies the given conditions:
1st Row: B1, B2, B3, B4, B5, B6
2nd Row: G1, G2, G3, G4, G5, G6

 

Approach: Follow the steps below to solve the problem 

  • Since each row can contain either boys or girls and size of all rows must be same, the most optimal arrangement possible is by placing greater common divisor of (N, M) number of elements in each row.
  • Therefore, print GCD(N, M) as the required answer.

Below is the implementation of the above approach:



C++14




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// GCD of two numbers
int gcd(int a, int b)
{
    if (b == 0)
        return a;
 
    return gcd(b, a % b);
}
 
// Function to count maximum persons
// that can be placed in a row
int maximumRowValue(int n, int m) { return gcd(n, m); }
 
// Driver Code
int main()
{
    // Input
    int N = 4;
    int M = 2;
 
    // Function to count maximum
    // persons that can be placed in a row
    cout << maximumRowValue(N, M);
}

Java




// Java Program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to calculate
  // GCD of two numbers
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
 
    return gcd(b, a % b);
  }
 
  // Function to count maximum persons
  // that can be placed in a row
  static int maximumRowValue(int n, int m) {
    return gcd(n, m);
  }
 
  // Driver Code
  public static void main(String args[])
  {
    // Input
    int N = 4;
    int M = 2;
 
    // Function to count maximum
    // persons that can be placed in a row
    System.out.print(maximumRowValue(N, M));
  }
}
 
// This code is contributed by SURENDRA_GANGWAR.

Python3




# Python3 Program to implement
# the above approach
 
# Function to calculate
# GCD of two numbers
def gcd(a, b):
    if (b == 0):
        return a
 
    return gcd(b, a % b)
 
# Function to count maximum persons
# that can be placed in a row
def maximumRowValue(n, m):
    return gcd(n, m)
 
# Driver Code
if __name__ == '__main__':
   
    # Input
    N = 4
    M = 2
 
    # Function to count maximum
    # persons that can be placed in a row
    print (maximumRowValue(N, M))
 
# This code is contributed by mohit kumar 29.

C#




// C# Program to implement
// the above approach
using System;
class GFG
{
 
  // Function to calculate
  // GCD of two numbers
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
 
    return gcd(b, a % b);
  }
 
  // Function to count maximum persons
  // that can be placed in a row
  static int maximumRowValue(int n, int m) { return gcd(n, m); }
 
  // Driver Code
  public static void Main(String[] args)
  {
     
    // Input
    int N = 4;
    int M = 2;
 
    // Function to count maximum
    // persons that can be placed in a row
    Console.WriteLine(maximumRowValue(N, M));
  }
}
 
// This code is contributed by code_hunt.

Javascript




<script>
// javascript Program to implement
// the above approach
 
    // Function to calculate
    // GCD of two numbers
    function gcd(a , b) {
        if (b == 0)
            return a;
 
        return gcd(b, a % b);
    }
 
    // Function to count maximum persons
    // that can be placed in a row
    function maximumRowValue(n , m) {
        return gcd(n, m);
    }
 
    // Driver Code
     
        // Input
        var N = 4;
        var M = 2;
 
        // Function to count maximum
        // persons that can be placed in a row
        document.write(maximumRowValue(N, M));
 
// This code is contributed by aashish1995
</script>

 
 

Output: 
2

 

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!