Minimum pair sum operations to make array each element divisible by 4
Given an array of positive integers of length n. Our task is to find minimum number of operations to convert an array so that arr[i] % 4 is zero for each i. In each operation, we can take any two elements from the array, remove both of them and put back their sum in the array.
Examples:
Input : arr = {2 , 2 , 2 , 3 , 3}
Output : 3
Explanation: In 1 operation we pick 2 and 2 and put their sum back to the array , In 2 operation we pick 3 and 3 and do same for that ,now in 3 operation we pick 6 and 2 so overall 3 operation are required.
Input: arr = {4, 2, 2, 6, 6}
Output: 2
Explanation: In operation 1, we can take 2 and 2 and put back their sum i.e. 4. In operation 2, we can take 6 and 6 and put back their sum i.e. 12. And array becomes {4, 4, 12}.
Approach : Assume the count of elements leaving remainder 1, 2, 3 when divided by 4 are brr[1], brr[2] and brr[3].
If (brr[1] + 2 * brr[2] + 3 * brr[3]) is not a multiple of 4, solution does not exist.
Now greedily pair elements of brr[2] with brr[2] and elements of brr[1] with brr[3]. This helps us to achieve fixing a maximum of 2 elements at a time. Now, we can either we left with only 1 brr[2] element or none. If we are left with 1 brr[2] element, then we can pair with 2 remaining brr[1] or brr[3] elements. This will incur a total of 2 operations.
At last, we would be only left with brr[1] or brr[3] elements (if possible). This can only we fixed in one way. That is taking 4 of them and fixing them all together in 3 operations. Thus, we are able to fix all the elements of the array.
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperations( int arr[], int n)
{
int brr[] = { 0, 0, 0, 0 };
for ( int i = 0; i < n; i++)
brr[arr[i] % 4]++;
if ((brr[1] + 2 * brr[2] + 3 * brr[3]) % 4 == 0)
{
int min_opr = min(brr[3], brr[1]);
brr[3] -= min_opr;
brr[1] -= min_opr;
min_opr += brr[2] / 2;
brr[2] %= 2;
if (brr[2]) {
min_opr += 2;
brr[2] = 0;
if (brr[3])
brr[3] -= 2;
if (brr[1])
brr[1] -= 2;
}
if (brr[1])
min_opr += (brr[1] / 4) * 3;
if (brr[3])
min_opr += (brr[3] / 4) * 3;
return min_opr;
}
return -1;
}
int main()
{
int arr[] = { 1, 2, 3, 1, 2, 3, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minimumOperations(arr, n);
}
|
Java
class GFG {
static int minimumOperations( int arr[], int n)
{
int brr[] = { 0 , 0 , 0 , 0 };
for ( int i = 0 ; i < n; i++)
brr[arr[i] % 4 ]++;
if ((brr[ 1 ] + 2 * brr[ 2 ] + 3 * brr[ 3 ]) % 4 == 0 )
{
int min_opr = Math.min(brr[ 3 ], brr[ 1 ]);
brr[ 3 ] -= min_opr;
brr[ 1 ] -= min_opr;
min_opr += brr[ 2 ] / 2 ;
brr[ 2 ] %= 2 ;
if (brr[ 2 ] == 1 ) {
min_opr += 2 ;
brr[ 2 ] = 0 ;
if (brr[ 3 ] == 1 )
brr[ 3 ] -= 2 ;
if (brr[ 1 ]== 1 )
brr[ 1 ] -= 2 ;
}
if (brr[ 1 ] != 0 )
min_opr += (brr[ 1 ] / 4 ) * 3 ;
if (brr[ 3 ] != 0 )
min_opr += (brr[ 3 ] / 4 ) * 3 ;
return min_opr;
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 1 , 2 , 3 , 8 };
int n = arr.length;
System.out.println(minimumOperations(arr, n));
}
}
|
Python3
def minimumOperations(arr,n):
brr = [ 0 , 0 , 0 , 0 ]
for i in range (n):
brr[arr[i] % 4 ] + = 1 ;
if ((brr[ 1 ] + 2 * brr[ 2 ] + 3 * brr[ 3 ]) % 4 = = 0 ):
min_opr = min (brr[ 3 ], brr[ 1 ])
brr[ 3 ] - = min_opr
brr[ 1 ] - = min_opr
min_opr + = brr[ 2 ] / / 2
brr[ 2 ] % = 2
if (brr[ 2 ]):
min_opr + = 2
brr[ 2 ] = 0
if (brr[ 3 ]):
brr[ 3 ] - = 2
if (brr[ 1 ]):
brr[ 1 ] - = 2
if (brr[ 1 ]):
min_opr + = (brr[ 1 ] / / 4 ) * 3
if (brr[ 3 ]):
min_opr + = (brr[ 3 ] / / 4 ) * 3
return min_opr
return - 1
arr = [ 1 , 2 , 3 , 1 , 2 , 3 , 8 ]
n = len (arr)
print (minimumOperations(arr, n))
|
C#
using System;
class GFG {
static int minimumOperations( int []arr, int n)
{
int []brr = { 0, 0, 0, 0 };
for ( int i = 0; i < n; i++)
brr[arr[i] % 4]++;
if ((brr[1] + 2 * brr[2] + 3 * brr[3]) % 4 == 0)
{
int min_opr = Math.Min(brr[3], brr[1]);
brr[3] -= min_opr;
brr[1] -= min_opr;
min_opr += brr[2] / 2;
brr[2] %= 2;
if (brr[2] == 1) {
min_opr += 2;
brr[2] = 0;
if (brr[3] == 1)
brr[3] -= 2;
if (brr[1]== 1)
brr[1] -= 2;
}
if (brr[1] == 1)
min_opr += (brr[1] / 4) * 3;
if (brr[3] == 1)
min_opr += (brr[3] / 4) * 3;
return min_opr;
}
return -1;
}
public static void Main()
{
int []arr = { 1, 2, 3, 1, 2, 3, 8 };
int n = arr.Length;
Console.WriteLine(minimumOperations(arr, n));
}
}
|
PHP
<?php
function minimumOperations( $arr , $n )
{
$brr = array (0, 0, 0, 0);
for ( $i = 0; $i < $n ; $i ++)
$brr [ $arr [ $i ] % 4]++;
if (( $brr [1] + 2 *
$brr [2] + 3 *
$brr [3]) % 4 == 0)
{
$min_opr = min( $brr [3],
$brr [1]);
$brr [3] -= $min_opr ;
$brr [1] -= $min_opr ;
$min_opr += $brr [2] / 2;
$brr [2] %= 2;
if ( $brr [2])
{
$min_opr += 2;
$brr [2] = 0;
if ( $brr [3])
$brr [3] -= 2;
if ( $brr [1])
$brr [1] -= 2;
}
if ( $brr [1])
$min_opr += ( $brr [1] / 4) * 3;
if ( $brr [3])
$min_opr += ( $brr [3] / 4) * 3;
return $min_opr ;
}
return -1;
}
$arr = array (1, 2, 3,
1, 2, 3, 8);
$n = count ( $arr );
echo (minimumOperations( $arr , $n ));
?>
|
Javascript
<script>
function minimumOperations(arr,n)
{
let brr = [0, 0, 0, 0 ];
for (let i = 0; i < n; i++)
brr[arr[i] % 4]++;
if ((brr[1] + 2 * brr[2] + 3 * brr[3]) % 4 == 0)
{
let min_opr = Math.min(brr[3], brr[1]);
brr[3] -= min_opr;
brr[1] -= min_opr;
min_opr += brr[2] / 2;
brr[2] %= 2;
if (brr[2] == 1) {
min_opr += 2;
brr[2] = 0;
if (brr[3] == 1)
brr[3] -= 2;
if (brr[1]== 1)
brr[1] -= 2;
}
if (brr[1] == 1)
min_opr += (brr[1] / 4) * 3;
if (brr[3] == 1)
min_opr += (brr[3] / 4) * 3;
return min_opr;
}
return -1;
}
let arr= [1, 2, 3, 1, 2, 3, 8 ];
let n = arr.length;
document.write(minimumOperations(arr, n));
</script>
|
Last Updated :
07 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...