Find the minimum operations required to make Array elements Divisible by 3
Last Updated :
04 Nov, 2023
mathGiven an array A[] of size N, count the minimum number of operations required such that all the elements of the array are divisible by 3. In one operation, we can take any two elements from the array, remove them, and append their sum at the end of the array.
Note: If we can’t make all elements divisible by 3 return -1
Examples:
Input: N = 7, A[] = {1, 4, 7, 10, 13, 2, 5}
Output: 4
Explanation:
- Operation 1 :- Remove 1 and 2 and Append 3. Array becomes [4 7 10 13 5 3]
- Operation 2 :- Remove 4 and 5 and Append 9. Array becomes [7 10 13 3 9]
- Operation 3 :- Remove 7 and 13 and Append 20. Array becomes [10 3 9 20]
- Operation 4 :- Remove 10 and 20 and Append 30. Array becomes [3 9 30]
Now array has all the elements divisible by 3.
Input: N = 5, A[] = {1, 1, 1, 1, 1}
Output: -1
Explanation: It is impossible to have all the elements of the array to be divisible by 3.
Approach: The problem can be solved by the following approach:
In order to make all the element of array arr[] divisble by 3, we don’t need to make any change in the numbers which are already divisible by 3. For the rest of the numbers, they can belong to one of these groups:
- Group 1: The number leaves a remainder of 1, when divided by 3
- Group 2: The number leaves a remainder of 2, when divided by 3.
If we observe carefully, any number of Group 1 can be paired with any number of Group 2 using one operation to make a sum divisible by 3. Now, if there are any elements left in one of the group, then every three elements can pair among themselves using 2 operations to make a sum divisible by 3. If we are still left with any number which is not divisible by 3, then it is impossible to make it divisible by 3.
Below are the steps involved in the approach:
- Maintain two variables ones and twos to maintain the count of numbers with remainder one and two when divided by 3.
- Iterate in a array:
- if a[i] % 3 == 1, increment ones variable by 1.
- if a[i] % 3 == 2, increment twos variable by 1.
- If difference between ones and twos is not divisible by 3, return -1.
- Otherwise return min(ones, twos) + 2 * (difference)/3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperations(vector< int >& a)
{
int ones = 0, twos = 0;
for ( int i = 0; i < a.size(); i++) {
if (a[i] % 3 == 1)
ones++;
else if (a[i] % 3 == 2)
twos++;
}
int diff = abs (ones - twos);
if (diff % 3 != 0)
return -1;
return min(ones, twos) + 2 * (diff / 3);
}
int main()
{
int n = 7;
vector< int > a = { 1, 4, 7, 10, 13, 2, 5 };
cout << minimumOperations(a) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
public class Main {
static int minimumOperations(ArrayList<Integer> a) {
int ones = 0 , twos = 0 ;
for ( int i = 0 ; i < a.size(); i++) {
if (a.get(i) % 3 == 1 )
ones++;
else if (a.get(i) % 3 == 2 )
twos++;
}
int diff = Math.abs(ones - twos);
if (diff % 3 != 0 )
return - 1 ;
return Math.min(ones, twos) + 2 * (diff / 3 );
}
public static void main(String[] args) {
int n = 7 ;
ArrayList<Integer> a = new ArrayList<>();
a.add( 1 );
a.add( 4 );
a.add( 7 );
a.add( 10 );
a.add( 13 );
a.add( 2 );
a.add( 5 );
System.out.println(minimumOperations(a));
}
}
|
Python3
def minimum_operations(arr):
ones = 0
twos = 0
for num in arr:
if num % 3 = = 1 :
ones + = 1
elif num % 3 = = 2 :
twos + = 1
diff = abs (ones - twos)
if diff % 3 ! = 0 :
return - 1
return min (ones, twos) + 2 * (diff / / 3 )
n = 7
arr = [ 1 , 4 , 7 , 10 , 13 , 2 , 5 ]
result = minimum_operations(arr)
print (result)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int minimumOperations( int n, List< int > a)
{
int ones = 0, twos = 0;
for ( int i = 0; i < n; i++) {
if (a[i] % 3 == 1)
ones++;
else if (a[i] % 3 == 2)
twos++;
}
int diff = Math.Abs(ones - twos);
if (diff % 3 != 0)
return -1;
return Math.Min(ones, twos) + 2 * (diff / 3);
}
public static void Main()
{
int n = 7;
List< int > a = new List< int >();
a.Add(1);
a.Add(4);
a.Add(7);
a.Add(10);
a.Add(13);
a.Add(2);
a.Add(5);
Console.WriteLine(minimumOperations(n, a));
}
}
|
Javascript
function minimumOperations(arr) {
let ones = 0;
let twos = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 3 === 1) {
ones++;
} else if (arr[i] % 3 === 2) {
twos++;
}
}
const diff = Math.abs(ones - twos);
if (diff % 3 !== 0) {
return -1;
}
return Math.min(ones, twos) + 2 * Math.floor(diff / 3);
}
const n = 7;
const a = [1, 4, 7, 10, 13, 2, 5];
console.log(minimumOperations(a));
|
Time Complexity: O(N), where N is the size of the array A[]
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...