Square Formation

Given an a x b rectangle, your task is to cut it into squares.

On each move, you can select a rectangle and cut it into two rectangles in such a way that all side lengths remain integers.

What is the minimum possible number of moves?

Example 1:

Input: a = 3, b = 5

Output: 3

Explanation:

  1. Cut 3×5 → 3×3 + 3×2
  2. Cut 3×2 → 2×2 + 1×2
  3. Cut 1×2 → 1×1 + 1×1

Now all pieces are squares.

Example 2:

Input: a = 5, b = 10

Output: 1

Explanation : Cut once: 5×10 → 5×5 + 5×5 . Both pieces are already squares.

Now Your Turn!

Pick the correct output for the given input

Input: a = 2, b = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= a, b <= 500

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int rectangleCutting(int a, int b) {
// Write your code here
return 0;
}
};
Test Case

Input:

A
B