A ticketing system has nums[i] seats in each section, in a row. It wants to split the remaining seats evenly among p ushers, so the total must be a multiple of p. To get there, it can close one run of back-to-back sections (possibly none), but never all of them.
Write shortestTrim(nums, p) that returns the length of the shortest run to close so the remaining total is a multiple of p. Return 0 if no closing is needed and -1 if it's impossible without closing every section.
Input: nums = [4,2,7,1], p = 5 Output: 1
The total is 14. Closing the section with 4 seats leaves 10, a multiple of 5.
Input: nums = [2,3], p = 7 Output: -1
Closing one section leaves 3 or 2; closing both isn't allowed.
1 <= nums.length <= 10^51 <= nums[i] <= 10^91 <= p <= 10^9Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #51 Shortest Trim to Split Evenly. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.