Skip to content
Discussion options

You must be logged in to vote

We need to make all elements divisible by 3 with minimum operations, where each operation is adding or subtracting 1.

For any number, there are three possible cases:

  1. If it's already divisible by 3: 0 operations needed
  2. If it's 1 more than a multiple of 3: we can subtract 1 (1 operation)
  3. If it's 2 more than a multiple of 3: we can add 1 (1 operation)

So for each number, the minimum operations needed is simply the minimum distance to the nearest multiple of 3, which can be calculated as min(num % 3, 3 - (num % 3)).

Let's implement this solution in PHP: 3190. Find Minimum Operations to Make All Elements Divisible by Three

<?php
/**
 * @param Integer[] $nums
 * @return Integer
 */
function m…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Nov 22, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants