Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Cutting Ribbons



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Ribbon Cutting Problem

Ribbon Cutting Challenge

Problem Statement

You have an array lengths where each element denotes the length of a ribbon piece, and a number n representing how many equal-length ribbons you want to obtain. You can cut any ribbon into multiple pieces with positive integer lengths, or leave it as is.

For example, a ribbon of length 6 can be left as one piece of 6, or cut into pieces like one piece of 4 and one of 2, three pieces of 2, or six pieces of 1.

Your task is to find the largest possible positive integer length m such that you can cut the ribbons to get at least n pieces of length m. If it's impossible to get n ribbons of the same length, return 0.

Examples

Example 1:
Lengths = [10, 8, 6], n = 4
Output: 5
Explanation:
- Cut the first ribbon into two pieces: lengths 5 and 5.
- Cut the second ribbon into one piece of length 5 and one piece of length 3.
- Keep the third ribbon as length 6 (not used).
You get 3 ribbons of length 5 from the first two ribbons, and you can take one more 5-length piece by cutting the third ribbon into 5 and 1. 
Total: 4 ribbons of length 5.

Example 2:
Lengths = [8, 5, 10], n = 5
Output: 3
Explanation:
- Cut the first ribbon into two pieces: 3 and 5.
- Cut the second ribbon into one piece of length 3 and one of length 2.
- Cut the third ribbon into three pieces: 3, 3 and 4.
You get enough ribbons of length 3 to make 5 pieces.

Example 3:
Lengths = [4, 3, 3], n = 10
Output: 0
Explanation:
It is not possible to get 10 ribbons of the same positive length from the given ribbons.
    

Constraints

  • 1 ≤ lengths.length ≤ 100000
  • 1 ≤ lengths[i] ≤ 120000
  • 1 ≤ n ≤ 10^9