Interview Questions/Coding/Minimum Sheet Folds

Minimum Sheet Folds

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Minimum Sheet Folds

A rectangular sheet has size h by w. In one move, you may fold it parallel to one of its sides, and the new side lengths must remain integers. Find the minimum moves needed to obtain a rectangle of size h1 by w1. The target rectangle may be matched in either orientation. Return -1 if it cannot be done.

Examples

Example 1
Input:
10 8
5 4
Output:
2

Approach hint

Each dimension can be handled independently.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp