Medium · Hash Maps

Find X to Make Array Sum to K

Given a non-empty array a of positive integers and a target with 1 ≤ target ≤ sum(a), return the smallest integer x with min(a) ≤ x ≤ max(a) such that replacing every element larger than x by x leaves an array sum of at least target.

Examples

Example 1

[1,2,5,8] → 9

Output: x = 3

Example 2

[3,1,4,1,5] → 8

Output: x = 2

Rebuild it in the studio

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Hash Maps problems