Given two integers N and M that represent the length and breadth of a rectangular plot and another integer K that represent the number of persons. Each person will divide the plot into two parts such that it will be the largest possible square from the plot and will leave the second part for others, it continues until the plot is over or every person gets the plot. Now, The task is to determine the area of the plot left in the end.
Examples:
Input: N = 5, M = 3, K = 2
Output: 2
1st person divides the 5×3 plot into 2 parts i.e 3×3 and 2×3
and will get the plot having dimension 3×3. The other person divides the 2×3 plot again into
two parts i.e 2×2 and 1×2 and will get the plot having dimension 2×2. Now, the remaining
part of plot is having dimension 1×2 and area as 2 units.
Input: N = 4, M = 8, K = 4
Output: 0
Approach:
- If Length is greater than breadth then subtract breadth from length.
- If breadth is greater than length then subtract length from breadth.
- Repeat the above steps for all the persons while there area of the remaining plot is greater than 0.
- Print the area of the remaining plot in the end i.e. length * breadth.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int remainingArea( int N, int M, int K)
{
while (K-- && N && M) {
if (N > M)
N = N - M;
else
M = M - N;
}
if (N > 0 && M > 0)
return N * M;
else
return 0;
}
int main()
{
int N = 5, M = 3, K = 2;
cout << remainingArea(N, M, K);
return 0;
}
|
Java
class GFG {
static int remainingArea( int N, int M, int K)
{
while (K-- > 0 && N > 0 && M > 0 ) {
if (N > M)
N = N - M;
else
M = M - N;
}
if (N > 0 && M > 0 )
return N * M;
else
return 0 ;
}
public static void main(String[] args)
{
int N = 5 , M = 3 , K = 2 ;
System.out.println(remainingArea(N, M, K));
}
}
|
Python3
def remainingArea(N, M, K):
while (K > 0 and N > 0 and M > 0 ):
if (N > M):
N = N - M;
else :
M = M - N;
K = K - 1 ;
if (N > 0 and M > 0 ):
return N * M;
else :
return 0 ;
N = 5 ;
M = 3 ;
K = 2 ;
print (remainingArea(N, M, K));
|
C#
using System;
class GFG {
static int remainingArea( int N, int M, int K)
{
while (K-- > 0 && N > 0 && M > 0) {
if (N > M)
N = N - M;
else
M = M - N;
}
if (N > 0 && M > 0)
return N * M;
else
return 0;
}
static public void Main()
{
int N = 5, M = 3, K = 2;
Console.WriteLine(remainingArea(N, M, K));
}
}
|
PHP
<?php
function remainingArea( $N , $M , $K )
{
while ( $K -- && $N && $M )
{
if ( $N > $M )
$N = $N - $M ;
else
$M = $M - $N ;
}
if ( $N > 0 && $M > 0)
return $N * $M ;
else
return 0;
}
$N = 5;
$M = 3;
$K = 2;
echo remainingArea( $N , $M , $K );
?>
|
Javascript
<script>
function remainingArea(N, M, K)
{
while (K-- && N && M) {
if (N > M)
N = N - M;
else
M = M - N;
}
if (N > 0 && M > 0)
return N * M;
else
return 0;
}
var N = 5, M = 3, K = 2;
document.write( remainingArea(N, M, K));
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Oct, 2022
Like Article
Save Article