Ticker

6/recent/ticker-posts

Leetcode-3190. Find Minimum Operations to Make All Elements Divisible by Three





You are given an integer array nums. In one operation, you can add or subtract 1 from any element of nums.

Return the minimum number of operations to make all elements of nums divisible by 3.

 

Example 1:

Input: nums = [1,2,3,4]

Output: 3

Explanation:

All array elements can be made divisible by 3 using 3 operations:

  • Subtract 1 from 1.
  • Add 1 to 2.
  • Subtract 1 from 4.

Example 2:

Input: nums = [3,6,9]

Output: 0


The provided solution aims to solve the problem of determining the minimum number of operations required to make all elements of a given integer array divisible by 3. In each operation, one can either add or subtract 1 from any element of the array to adjust it so that it becomes divisible by 3. The approach taken in the solution involves iterating over each element of the array and checking if it is divisible by 3. If an element is not divisible by 3 (i.e., it has a remainder when divided by 3), the solution counts that element as needing one operation to make it divisible by 3.

The code starts by initializing a counter (`count`) to zero. It then iterates through the array and for each element, it checks if it is divisible by 3 using the modulus operation (`nums[i] % 3`). If the element is not divisible by 3, it increments the counter by 1. Finally, the code returns the value of the counter, which represents the minimum number of operations needed to make all elements divisible by 3.

In the first example, where the input array is `[1, 2, 3, 4]`, the algorithm checks each element: the first three elements (1, 2, and 4) are not divisible by 3, so the counter is incremented for each of them. The third element, 3, is already divisible by 3, so no operation is needed for it. Hence, the function returns 3, indicating that three operations are required to make all elements divisible by 3. In the second example, where the array is `[3, 6, 9]`, all elements are already divisible by 3, so no operations are needed, and the function returns 0.

However, the current implementation is somewhat simplistic, as it merely counts the number of elements that are not divisible by 3, but it does not consider whether an element's remainder when divided by 3 is 1 or 2. If the remainder is 1, we would need to subtract 1 from the element to make it divisible by 3, and if the remainder is 2, we would need to add 1. Therefore, a more efficient approach would involve handling these remainders more directly to minimize the number of operations required. Despite this, the solution still works for the problem's basic requirements by simply counting the elements that need adjustment.

CODE:

class Solution {
public:
    int minimumOperations(vector<int>& nums) {
        int i,n;
        n=nums.size();
        int count=0;
        for(i=0;i<n;i++){
            if(nums[i]%3!=0){
                count++;
                }
            }
        return count;
    }
};

Also Read

Post a Comment

0 Comments