390. Minimum Nodes to Remove for K Groups

MediumLinked ListMathLinked List

Given a linked list of length n and an integer k, you want to be able to partition the list into consecutive groups of exactly k nodes each (with nothing left over). Return the minimum number of nodes to remove so the remaining length is a multiple of k. Input: '[list], k'.

Input: '[list], k'.

Output: Integer — minimum nodes to remove.

Examples

Example 1
Input: [1,2,3,4,5,6,7], 3
Output: 1
Explanation: 7 mod 3 = 1 node to remove.
Example 2
Input: [1,2,3,4], 2
Output: 0
Explanation: Already divisible.
Example 3
Input: [1,2,3], 3
Output: 0
Explanation: Exactly one group.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →