DDSA
Advertisement

3512. Minimum Operations to Make Array Sum Divisible by K

Time: O(n)
Space: O(1)

Approach

Sum all elements; answer = sum mod k.

3512.cs
C#
// Approach: Sum all elements; answer = sum mod k.
// Time: O(n) Space: O(1)

public class Solution
{
    public int MinOperations(int[] nums, int k)
    {
        // Calculate the sum of all elements in the array using LINQ
        int totalSum = nums.Sum();

        // Return the remainder when the sum is divided by k
        // This represents the minimum value needed to make the sum divisible by k
        return totalSum % k;
    }
}
Advertisement
Was this solution helpful?