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 untill 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program of the above approach
#include <iostream>
using namespace std;
  
// Function to find the minimum cuts
void minimumCuts(int N, int M)
{
  
    // Print the minimum cuts using
    // the formula
    cout << (N * M - 1);
}
  
// Driver Code
int main()
{
  
    // Given dimensions
    int N = 4, M = 4;
  
    // Function call
    minimumCuts(N, M);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program of the above approach
import java.util.*;
  
class GFG{
  
// Function to find the minimum cuts
static void minimumCuts(int N, int M)
{
      
    // Print the minimum cuts using
    // the formula
    System.out.print(N * M - 1);
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given dimensions
    int N = 4, M = 4;
  
    // Function call
    minimumCuts(N, M);
}
}
  
// This code is contributed by Rohit_ranjan

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program of the above approach
  
# Function to find the minimum cuts
def minimumCuts(N, M):
      
    # Print the minimum cuts using
    # the formula
    print(N * M - 1)
  
# Driver Code 
if __name__ == "__main__":
      
    # Given dimensions
    N = 4
    M = 4
      
    # Function call
    minimumCuts(N, M)
      
# This code is contributed by coder001

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program of the above approach
using System;
  
class GFG{
  
// Function to find the minimum cuts
static void minimumCuts(int N, int M)
{
      
    // Print the minimum cuts using
    // the formula
    Console.Write(N * M - 1);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given dimensions
    int N = 4, M = 4;
  
    // Function call
    minimumCuts(N, M);
}
}
  
// This code is contributed by Princi Singh

chevron_right


Output:

15

Time Complexity: O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.