Given a paper of size A x B. Task is to cut the paper into squares of any size. Find the minimum number of squares that can be cut from the paper.
Input : 13 x 29 Output : 9 Explanation : 2 (squares of size 13x13) + 4 (squares of size 3x3) + 3 (squares of size 1x1)=9 Input : 4 x 5 Output : 5 Explanation : 1 (squares of size 4x4) + 4 (squares of size 1x1)
We know that if we want to cut minimum number of squares from the paper then we would have to cut largest square possible from the paper first and largest square will have same side as smaller side of the paper. For example if paper have the size 13 x 29, then maximum square will be of side 13. so we can cut 2 square of size 13 x 13 (29/13 = 2). Now remaining paper will have size 3 x 13. Similarly we can cut remaining paper by using 4 squares of size 3 x 3 and 3 squares of 1 x 1. So minimum 9 squares can be cut from the Paper of size 13 x 29.
Below is the implementation of above approach.
Note that the above Greedy solution doesn’t always produce optimal result. For example if input is 36 x 30, the above algorithm would produce output 6, but we can cut the paper in 5 squares
1) Three squares of size 12 x 12
2) Two squares of size 18 x 18.
Thanks to Sergey V. Pereslavtsev for pointing out the above case.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.