Paper Cut into Minimum Number of Squares
Last Updated :
12 Aug, 2024
Given a paper of size A x B, the task is to cut the entire paper into squares of any size. Find the minimum number of squares that can be cut from the paper.
Examples:Â
Input: A = 5, B = 8

5 squares cut from Paper of size 5 X 8
Output: 5
Explanation: We can cut the paper into 5 squares: 1 square of size 5 X 5, 1 square of size 3 X 3, 1 square of size 2 X 2 and 2 squares of size 1 X 1.
Input: A = 13, B = 11

6 squares cut from Paper of size 13 X 11
Output: 6
Explanation: We can cut the paper into 6 squares: 1 square of size 7 X 7, 1 square of size 6 X 6, 1 square of size 5 X 5, 2 squares of size 4 X 4 and 1 square of size 1 X 1.
Input: A = 6, B = 7

5 squares cut from Paper of size 6 X 7
Output: 5
Explanation: We can cut the paper into 5 squares: 1 square of size 4 X 4, 2 squares of size 3 X 3 and 2 squares of size 3 X 3.
Incorrect Approaches:
Greedy Approach: At the first sight, it might seem that the problem can be easily solved by cutting the largest square possible from the paper first, followed by cutting the largest square from the remaining paper and so on till we have cut the entire paper. But, this solution is incorrect.
Why Greedy Approach won’t work?
Consider a paper of size 6 X 7, then if we try to cut the paper greedily we will get 7 squares: 1 square of size 6 X 6 and 6 squares of size 1 X 1, whereas the correct solution is: 5. Hence, greedy approach won’t work.
Dynamic Programming with vertical or horizonal cuts: Another solution which might seem correct is using Dynamic Programming. We can maintain a dp[][] table such that dp[i][j] = minimum number of squares that can be cut from paper of size i x j. Then for paper of size A X B,
- We can try to cut it along each row: dp[i][j] = min(dp[i][j], 1 + dp[i – k][j] + dp[k][j]), where k can be in the range [1, i – 1].
- We can try to cut it along each column: dp[i][j] = min(dp[i][j], 1 + dp[i][j – k] + dp[i][k]), where k can be in the range [1, j – 1].
Finally, minimum of all cuts will be the answer. But, this solution is also incorrect.
Why cutting vertically or horizontally with Dynamic Programming Approach won’t work?
This won’t work because we are assuming that a vertical or horizontal cut will always divide the rectangle into two parts. Consider a paper of size 13 X 11, then if we try to cut the paper using DP approach, we will get 8 squares but the correct answer (as shown in Examples) is 6. Hence, Dynamic Programming won’t work.
Correct Approach using DFS and Dynamic Programming:
The idea is to cut the entire paper using DFS in bottom-up manner. In every step, find the lowest-left corner of the paper and try to cut squares of all possible size from that corner. After cutting a square, again find the lowest-left corner of the remaining paper to cut squares of all possible sizes and so on. But if we try all possible cuts from the lowest-left corner of every possible paper size, then it would be quite inefficient. We can optimize it by using Dynamic Programming to store minimum cuts for each possible paper size.
To uniquely identify any paper size, we can maintain a remainingSquares[] array, such that remainingSquares[i] stores the number of remaining squares of size 1 X 1 in the ith column of the paper. So, for a paper of size 6 X 7, remainingSquares[] = {6, 6, 6, 6, 6, 6, 6}. Also to find the lowest-left corner, we will find the first index having the maximum remaining squares. So, we can hash the value of remainingSquares[] array to find a unique key for all the possible values of remainingSquares[] array.
Below is the implementation of the above algorithm:
C++
// C++ Program to find minimum number of squares to cut
// from a paper of size A X B
#include <bits/stdc++.h>
using namespace std;
// function to get the hash key for remSquares array
long long getKey(vector<int> &remSquares, int B)
{
long long base = 1;
long long key = 0;
for (int i = 0; i < B; i++)
{
key += (remSquares[i] * 1LL * base);
base = base * 1LL * (B + 1);
}
return key;
}
// Recursive function to find the minimum number of square cuts
// for a given remSquares array
int solve(vector<int> &remSquares, int A, int B,
map<long long, int> &dp) {
// pointers to mark the start and end of range
// with maximum remaining squares
int start, end;
// Check if we have previously calculated the answer
// for the same state
int key = getKey(remSquares, B);
if (dp.find(key) != dp.end())
return dp[key];
int maxRemSquares = 0;
// Find the starting point of min height
for (int i = 0; i < B; i++) {
if (remSquares[i] > maxRemSquares) {
maxRemSquares = remSquares[i];
start = i;
}
}
// If max remaining squares = 0, then we have already
// cut the entire paper
if (maxRemSquares == 0)
return 0;
end = start;
vector<int> newRemSquares = remSquares;
int ans = INT_MAX;
// Find the ending point of min height
while (end < B) {
// length of edge of square from start till current end
int squareEdge = end - start + 1;
// If the current column does not have maximum remaining
// squares or if its impossible to cut a square of
// size squareEdge, then break out of the loop
if (newRemSquares[end] != maxRemSquares ||
newRemSquares[end] - squareEdge < 0)
break;
// If we can cut a square of size squareEdge,
// update the remainingSquares
for (int i = start; i <= end; i++)
newRemSquares[i] = maxRemSquares - squareEdge;
// Find the solution for new remainingSquares
ans = min(ans, 1 + solve(newRemSquares, A, B, dp));
end += 1;
}
return dp[key] = ans;
}
// Function to find the minimum number of squares we can cut
// using paper of size A X B
int minSquares(int A, int B) {
// if the given rectangle is a square
if (A == B)
return 1;
// Initialize remaining squares = A for all the B columns
vector<int> remSquares(B, A);
map<long long, int> dp;
return solve(remSquares, A, B, dp);
}
int main() {
// Sample Input
int A = 13, B = 11;
// Function call to get minimum number of squares for A X B
cout << minSquares(A, B);
return 0;
}
Java
// Java Program to find minimum number of squares to cut
// from a paper of size A X B
import java.util.HashMap;
import java.util.Map;
public class GfG {
// Function to get the hash key for remSquares array
private static long getKey(int[] remSquares, int B) {
long base = 1;
long key = 0;
for (int i = 0; i < B; i++) {
key += (remSquares[i] * base);
base *= (B + 1);
}
return key;
}
// Recursive function to find the minimum number of
// square cuts for a given remSquares array
private static int solve(int[] remSquares, int A, int B,
Map<Long, Integer> dp) {
// Pointers to mark the start and end of range
// with maximum remaining squares
int start = 0, end = 0;
// Check if we have previously calculated the answer
// for the same state
long key = getKey(remSquares, B);
if (dp.containsKey(key)) {
return dp.get(key);
}
int maxRemSquares = 0;
// Find the starting point of min height
for (int i = 0; i < B; i++) {
if (remSquares[i] > maxRemSquares) {
maxRemSquares = remSquares[i];
start = i;
}
}
// If max remaining squares = 0, then we have
// already cut the entire paper
if (maxRemSquares == 0)
return 0;
end = start;
int[] newRemSquares = remSquares.clone();
int ans = Integer.MAX_VALUE;
// Find the ending point of min height
while (end < B) {
// Length of edge of square from start till
// current end
int squareEdge = end - start + 1;
// If the current column does not have maximum
// remaining squares or if it's impossible to
// cut a square of size squareEdge, then break
// out of the loop
if (newRemSquares[end] != maxRemSquares
|| newRemSquares[end] - squareEdge < 0) {
break;
}
// If we can cut a square of size squareEdge,
// update the remainingSquares
for (int i = start; i <= end; i++) {
newRemSquares[i]
= maxRemSquares - squareEdge;
}
// Find the solution for new remainingSquares
ans = Math.min(
ans, 1 + solve(newRemSquares, A, B, dp));
end += 1;
}
dp.put(key, ans);
return ans;
}
// Function to find the minimum number of squares we can
// cut using paper of size A X B
public static int minSquares(int A, int B) {
// If the given rectangle is a square
if (A == B)
return 1;
// Initialize remaining squares = A for all the B
// columns
int[] remSquares = new int[B];
for (int i = 0; i < B; i++) {
remSquares[i] = A;
}
Map<Long, Integer> dp = new HashMap<>();
return solve(remSquares, A, B, dp);
}
public static void main(String[] args) {
// Sample Input
int A = 13, B = 11;
// Function call to get minimum number of squares
// for A X B
System.out.println(minSquares(A, B));
}
}
Python
# Python Program to find minimum number of squares to cut
# from a paper of size A X B
# Function to get the hash key for rem_squares array
def get_key(rem_squares, B):
base = 1
key = 0
for i in range(B):
key += rem_squares[i] * base
base *= (B + 1)
return key
# Recursive function to find the minimum number of square cuts
# for a given rem_squares array
def solve(rem_squares, A, B, dp):
# Check if we have previously calculated the answer
# for the same state
key = get_key(rem_squares, B)
if key in dp:
return dp[key]
max_rem_squares = 0
start = end = 0
# Find the starting point of the minimum height
for i in range(B):
if rem_squares[i] > max_rem_squares:
max_rem_squares = rem_squares[i]
start = i
# If max remaining squares = 0, then we have already
# cut the entire paper
if max_rem_squares == 0:
return 0
end = start
new_rem_squares = rem_squares[:]
ans = float('inf')
# Find the ending point of the minimum height
while end < B:
# Length of edge of square from start till current end
square_edge = end - start + 1
# If the current column does not have maximum
# remaining squares or if it's impossible to cut
# a square of size square_edge, then break out of the loop
if new_rem_squares[end] != max_rem_squares or new_rem_squares[end] - square_edge < 0:
break
# If we can cut a square of size square_edge, update the remaining squares
for i in range(start, end + 1):
new_rem_squares[i] = max_rem_squares - square_edge
# Find the solution for new remaining squares
ans = min(ans, 1 + solve(new_rem_squares, A, B, dp))
end += 1
dp[key] = ans
return ans
# Function to find the minimum number of squares we can cut
# using paper of size A X B
def min_squares(A, B):
# If the given rectangle is a square
if A == B:
return 1
# Initialize remaining squares = A for all the B columns
rem_squares = [A] * B
dp = {}
return solve(rem_squares, A, B, dp)
# Sample Input
A = 13
B = 11
# Function call to get minimum number of squares for A X B
print(min_squares(A, B))
C#
// C# Program to find minimum number of squares to cut
// from a paper of size A X B
using System;
using System.Collections.Generic;
class GfG {
// Function to get the hash key for remSquares array
static long GetKey(int[] remSquares, int B) {
long baseVal = 1;
long key = 0;
for (int i = 0; i < B; i++) {
key += remSquares[i] * baseVal;
baseVal *= (B + 1);
}
return key;
}
// Recursive function to find the minimum number of
// square cuts for a given remSquares array
static int Solve(int[] remSquares, int A, int B,
Dictionary<long, int> dp) {
int start = 0;
int end;
// Check if we have previously calculated the
// answer for the same state
long key = GetKey(remSquares, B);
if (dp.TryGetValue(key, out int cachedResult))
return cachedResult;
int maxRemSquares = 0;
// Find the starting point of min height
for (int i = 0; i < B; i++) {
if (remSquares[i] > maxRemSquares) {
maxRemSquares = remSquares[i];
start = i;
}
}
// If max remaining squares = 0, then we
// have already cut the entire paper
if (maxRemSquares == 0)
return 0;
end = start;
int[] newRemSquares = (int[])remSquares.Clone();
int ans = int.MaxValue;
// Find the ending point of min height
while (end < B)
{
// Length of edge of square from start till current end
int squareEdge = end - start + 1;
// If the current column does not have maximum remaining squares
// or if it's impossible to cut a square of size squareEdge,
// then break out of the loop
if (newRemSquares[end] != maxRemSquares ||
newRemSquares[end] - squareEdge < 0)
break;
// If we can cut a square of size squareEdge,
// update the remainingSquares
for (int i = start; i <= end; i++)
newRemSquares[i] = maxRemSquares - squareEdge;
// Find the solution for new remainingSquares
ans = Math.Min(ans, 1 + Solve(newRemSquares, A, B, dp));
end += 1;
}
dp[key] = ans;
return ans;
}
// Function to find the minimum number of squares
// we can cut using paper of size A X B
static int MinSquares(int A, int B) {
// If the given rectangle is a square
if (A == B)
return 1;
// Initialize remaining squares = A for all the B columns
int[] remSquares = new int[B];
for (int i = 0; i < B; i++)
remSquares[i] = A;
Dictionary<long, int> dp = new Dictionary<long, int>();
return Solve(remSquares, A, B, dp);
}
static void Main() {
// Sample Input
int A = 13, B = 11;
// Function call to get minimum number of squares for A X B
Console.WriteLine(MinSquares(A, B));
}
}
JavaScript
// Function to get the hash key for remSquares array
function getKey(remSquares, B) {
let base = 1;
let key = 0;
for (let i = 0; i < B; i++) {
key += remSquares[i] * base;
base *= (B + 1);
}
return key;
}
// Recursive function to find the minimum number of square cuts
// for a given remSquares array
function solve(remSquares, A, B, dp) {
// pointers to mark the start and end of range with maximum remaining squares
let start, end;
// Check if we have previously calculated the answer for the same state
let key = getKey(remSquares, B);
if (dp.has(key)) {
return dp.get(key);
}
let maxRemSquares = 0;
// Find the starting point of min height
for (let i = 0; i < B; i++) {
if (remSquares[i] > maxRemSquares) {
maxRemSquares = remSquares[i];
start = i;
}
}
// If max remaining squares = 0, then we have already cut the entire paper
if (maxRemSquares === 0) {
return 0;
}
end = start;
let newRemSquares = [...remSquares];
let ans = Infinity;
// Find the ending point of min height
while (end < B) {
// Length of edge of square from start till current end
let squareEdge = end - start + 1;
// If the current column does not have maximum remaining squares
// or if it's impossible to cut a square of size squareEdge, then break out of the loop
if (newRemSquares[end] !== maxRemSquares || newRemSquares[end] - squareEdge < 0) {
break;
}
// If we can cut a square of size squareEdge, update the remainingSquares
for (let i = start; i <= end; i++) {
newRemSquares[i] = maxRemSquares - squareEdge;
}
// Find the solution for new remainingSquares
ans = Math.min(ans, 1 + solve(newRemSquares, A, B, dp));
end += 1;
}
dp.set(key, ans);
return ans;
}
// Function to find the minimum number of squares we can cut using paper of size A X B
function minSquares(A, B) {
// If the given rectangle is a square
if (A === B) {
return 1;
}
// Initialize remaining squares = A for all the B columns
let remSquares = new Array(B).fill(A);
let dp = new Map();
return solve(remSquares, A, B, dp);
}
// Sample Input
let A = 13, B = 11;
// Function call to get minimum number of squares for A X B
console.log(minSquares(A, B));
Time Complexity: O(A ^ B), for each of B columns, we can have A squares.
Auxiliary Space: O(A ^ B)