Identical Splitting in a rectangular grid
Given a rectangular grid NxM dimensions, the task is to find the minimum number of cuts required to break the given rectangular grid into a square of size 1×1.
Examples:
Input: N = 4, M = 4
Output: 15
Input: N = 2, M = 1
Output: 1
Approach:
The above images shows the splitting of the rectangular grid. We can observe that every single cut increases the number of rectangles of different dimensions by 1. We will do the splitting until we reach the square of dimension 1×1.
So for the given rectangular dimensions of NxM, the total number of squares of dimensions 1×1 is N*M. Therefore we required N*M – 1 cuts to break the given rectangular dimensions of NxM into squares of dimension 1×1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void minimumCuts( int N, int M)
{
cout << (N * M - 1);
}
int main()
{
int N = 4, M = 4;
minimumCuts(N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minimumCuts( int N, int M)
{
System.out.print(N * M - 1 );
}
public static void main(String[] args)
{
int N = 4 , M = 4 ;
minimumCuts(N, M);
}
}
|
Python3
def minimumCuts(N, M):
print (N * M - 1 )
if __name__ = = "__main__" :
N = 4
M = 4
minimumCuts(N, M)
|
C#
using System;
class GFG{
static void minimumCuts( int N, int M)
{
Console.Write(N * M - 1);
}
public static void Main(String[] args)
{
int N = 4, M = 4;
minimumCuts(N, M);
}
}
|
Javascript
<script>
function minimumCuts(N, M)
{
document.write(N * M - 1);
}
var N = 4, M = 4;
minimumCuts(N, M);
</script>
|
Time Complexity: O(1)
Last Updated :
30 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...