less than 1 minute read

Problem Statement

leetcode problem link

Brute Force [Accepted]

class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
        curr_sum = sum(nums)
        if curr_sum % k == 0:
            return 0

        res = 0
        while sum(nums) % k != 0:
            max_val = max(nums)
            index = nums.index(max_val)
            nums[index] -= 1
            res += 1

        return res

Editorial

Approach: Sum Modulo

class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
        return sum(nums) % k