Problem Β· Array

Minimize Multiples Of Three

Learn this problem
● EasyToshibaOA

Problem statement

Given an array where elements are multiples of 3 and some elements are not, if an element is not a multiple of 3, either add to or subtract from it to make it a multiple of 3.

Return the minimum number of times you need to add or subtract from a number to make all elements multiples of 3.

Function

minimizeMultiplesOfThree(arr: int[]) β†’ int

Complete the function minimizeMultiplesOfThree in the editor.

minimizeMultiplesOfThree has the following parameter:

  1. int arr[]: an array of integers

Returns

int: the minimum number of operations required

Examples

Example 1

arr = [12, 21, 3, 4]return = 1
12, 21, and 3 are all multiples of 3, but 4 is not. To make 4 a multiple of 3, subtract 1 from it to get 3. The total number of operations made is 1 (by subtracting 1 from 4).

Example 2

arr = [4, 5]return = 2
🐰🐰

Constraints

πŸ…πŸ…
drafts saved locally
public int minimizeMultiplesOfThree(int[] arr) {
  // write your code here
}
arr[12, 21, 3, 4]
expected1
checking account