We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Mathematics
  3. Fundamentals
  4. Cutting Paper Squares

Cutting Paper Squares

Problem
Submissions
Leaderboard
Discussions
Editorial

Mary has an piece of paper that she wants to cut into pieces according to the following rules:

  • She can only cut one piece of paper at a time, meaning she cannot fold the paper or layer already-cut pieces on top of one another.
  • Each cut is a straight line from one side of the paper to the other side of the paper. For example, the diagram below depicts the three possible ways to cut a piece of paper:
    example-cutting-squares.png

Given and , find and print the minimum number of cuts Mary must make to cut the paper into squares that are unit in size.

Input Format

A single line of two space-separated integers denoting the respective values of and .

Constraints

Output Format

Print a long integer denoting the minimum number of cuts needed to cut the entire paper into squares.

Sample Input

3 1

Sample Output

2

Explanation

Mary first cuts the piece of paper into a piece and a piece. She then cuts the piece into two pieces:

cutting-paper-squares.png

Because it took her two cuts to get pieces of size , we print as our answer.

Author

bishop15

Difficulty

Easy

Max Score

15

Submitted By

32586

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy