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:
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
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int maximumRowValue( int n, int m) { return gcd(n, m); }
int main()
{
int N = 4;
int M = 2;
cout << maximumRowValue(N, M);
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int maximumRowValue( int n, int m) {
return gcd(n, m);
}
public static void main(String args[])
{
int N = 4 ;
int M = 2 ;
System.out.print(maximumRowValue(N, M));
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def maximumRowValue(n, m):
return gcd(n, m)
if __name__ = = '__main__' :
N = 4
M = 2
print (maximumRowValue(N, M))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int maximumRowValue( int n, int m) { return gcd(n, m); }
public static void Main(String[] args)
{
int N = 4;
int M = 2;
Console.WriteLine(maximumRowValue(N, M));
}
}
|
Javascript
<script>
function gcd(a , b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
function maximumRowValue(n , m) {
return gcd(n, m);
}
var N = 4;
var M = 2;
document.write(maximumRowValue(N, M));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...