A fiber shop has n spools with whole-number lengths. An order needs at least k patch cords, and every patch cord must be cut to the exact same whole-number length L. From a spool of length a[i] you can cut floor(a[i] / L) cords of length L, discarding any remainder; cords may not be spliced across spools.
Find the largest length L (at least 1) for which the total number of cords across all spools is at least k. The order is guaranteed to be fillable at L = 1.
Line 1: two integers n and k.
Line 2: n space-separated integers, the spool lengths.
A single integer: the largest cord length that yields at least k cords.
Example 1
Input
3 7 8 4 6
Expected
2
Explanation
At L=2 the spools give 4+2+3 = 9 cords which is at least 7; at L=3 they give only 2+1+2 = 5 cords, so the longest workable length is 2.
Example 2
Input
2 4 5 5
Expected
2
Explanation
Each length-5 spool yields floor(5/L) cords; at L=2 that is 2 cords each for 4 total, and L=3 would give only 1 each, so the answer is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →