Skip to content
the poor coder

Hackerrank - Restaurant Solution

Poor Man
Published
Updated
Reading time
1 min read

Martha is interviewing at Subway. One of the rounds of the interview requires her to cut a bread of size  into smaller identical pieces such that each piece is a square having maximum possible side length with no left over piece of bread.

Input Format

The first line contains an integer .  lines follow. Each line contains two space separated integers  and  which denote length and breadth of the bread.

Constraints

Output Format

lines, each containing an integer that denotes the number of squares of maximum size, when the bread is cut as per the given condition.

Sample Input 0

Text
2
2 2
6 9

Sample Output 0

Text
1
6

Explanation 0

The 1st testcase has a bread whose original dimensions are 2x2, the bread is uncut and is a square. Hence the answer is 1.
The 2nd testcase has a bread of size 6x9. We can cut it into 54 squares of size 1x1, 6 of size 3x3. For other sizes we will have leftovers. Hence, the number of squares of maximum size that can be cut is 6.

Solution in Python#

Python
import math

def best(x,y):
    prod = x*y
    for i in range(2,int(math.sqrt(prod))+1)[::-1]:
        if not x%i and not y%i:
            return x*y//i**2
    else:
        return prod
for _ in range(int(input())):
    x,y = list(map(int,input().split()))
    print(best(x,y))

Keep going

2020

Hackerrank - Bus Station Solution

There are groups of friends, and each group is numbered from 1 to . The th group contains people. They live near a bus stop, and only a single bus operates on this route. An empty bus arrives at the bus stop and all the…

2020

Hackerrank - K Candy Store Solution

Jim enters a candy shop which has N different types of candies, each candy is of the same price. Jim has enough money to buy K candies. In how many different ways can he purchase K candies if there are infinite candies…

2020

Hackerrank - Diwali Lights Solution

On the eve of Diwali, Hari is decorating his house with a serial light bulb set. The serial light bulb set has N bulbs placed sequentially on a string which is programmed to change patterns every second. If at least one…

ESC