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:
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 numbersint 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 rowint maximumRowValue(int n, int m) { return gcd(n, m); }Â
// Driver Codeint 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 approachimport 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 numbersdef gcd(a, b):Â Â Â Â if (b == 0):Â Â Â Â Â Â Â Â return aÂ
    return gcd(b, a % b)Â
# Function to count maximum persons# that can be placed in a rowdef maximumRowValue(n, m):Â Â Â Â return gcd(n, m)Â
# Driver Codeif __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 approachusing 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> |
Â
Â
2
Â
Time Complexity: O(log N)
Auxiliary Space: O(1)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Information on that Topic: geeksforgeeks.org/place-n-boys-and-m-girls-in-different-rows-such-that-count-of-persons-placed-in-each-row-is-maximized/ […]