Related Articles
Minimum pair sum operations to make array each element divisible by 4
• Difficulty Level : Medium
• Last Updated : 25 Jul, 2018

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}.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach : Assume the count of elements leaving remainder 1, 2, 3 when divided by 4 are brr, brr and brr.
If (brr + 2 * brr + 3 * brr) is not a multiple of 4, solution does not exist.

Now greedily pair elements of brr with brr and elements of brr with brr. This helps us to achieve fixing a maximum of 2 elements at a time. Now, we can either we left with only 1 brr element or none. If we are left with 1 brr element, then we can pair with 2 remaining brr or brr elements. This will incur a total of 2 operations.

At last, we would be only left with brr or brr 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++

 `// CPP program to find Minimum number``// of operations to convert an array ``// so that arr[i] % 4 is zero.``#include ``using` `namespace` `std;`` ` `// Function to find minimum operations.``int` `minimumOperations(``int` `arr[], ``int` `n)``{   ``    ``// Counting of all the elements``    ``// leaving remainder 1, 2, 3 when``    ``// divided by 4 in the array brr.``    ``// at positions 1, 2 and 3 respectively.``    ``int` `brr[] = { 0, 0, 0, 0 };``    ``for` `(``int` `i = 0; i < n; i++)``        ``brr[arr[i] % 4]++;`` ` `    ``// If it is possible to convert the ``    ``// array so that arr[i] % 4 is zero.``    ``if` `((brr + 2 * brr + 3 * brr) % 4 == 0) ``    ``{``        ``// Pairing the elements of brr3 and brr1.``        ``int` `min_opr = min(brr, brr);``        ``brr -= min_opr;``        ``brr -= min_opr;`` ` `        ``// Pairing the brr2 elements.``        ``min_opr += brr / 2;`` ` `        ``// Assigning the remaining brr2 elements.``        ``brr %= 2;`` ` `        ``// If we are left with one brr2 element.``        ``if` `(brr) {`` ` `            ``// Here we need only two operations``            ``// to convert the remaining one``            ``// brr2 element to convert it.``            ``min_opr += 2;`` ` `            ``// Now there is no brr2 element.``            ``brr = 0;`` ` `            ``// Remaining brr3 elements.``            ``if` `(brr)             ``                ``brr -= 2;            `` ` `            ``// Remaining brr1 elements.``            ``if` `(brr) ``                ``brr -= 2;            ``        ``}`` ` `        ``// If we are left with brr1 and brr2``        ``// elements then, we have to take four``        ``// of them and fixing them all together``        ``// in 3 operations.``        ``if` `(brr)        ``            ``min_opr += (brr / 4) * 3;        ``        ``if` `(brr)        ``            ``min_opr += (brr / 4) * 3;        `` ` `        ``// Returns the minimum operations.``        ``return` `min_opr;``    ``}`` ` `    ``// If it is not possible to convert the array.``    ``return` `-1;    ``}`` ` `// Driver function``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 1, 2, 3, 8 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << minimumOperations(arr, n);``}`

## Java

 `// Java program to find Minimum number``// of operations to convert an array ``// so that arr[i] % 4 is zero.`` ` `class` `GFG {``  ` `// Function to find minimum operations.``static` `int` `minimumOperations(``int` `arr[], ``int` `n)``{   `` ` `    ``// Counting of all the elements``    ``// leaving remainder 1, 2, 3 when``    ``// divided by 4 in the array brr.``    ``// at positions 1, 2 and 3 respectively.``    ``int` `brr[] = { ``0``, ``0``, ``0``, ``0` `};``    ``for` `(``int` `i = ``0``; i < n; i++)``        ``brr[arr[i] % ``4``]++;``  ` `    ``// If it is possible to convert the ``    ``// array so that arr[i] % 4 is zero.``    ``if` `((brr[``1``] + ``2` `* brr[``2``] + ``3` `* brr[``3``]) % ``4` `== ``0``) ``    ``{``        ``// Pairing the elements of brr3 and brr1.``        ``int` `min_opr = Math.min(brr[``3``], brr[``1``]);``        ``brr[``3``] -= min_opr;``        ``brr[``1``] -= min_opr;``  ` `        ``// Pairing the brr2 elements.``        ``min_opr += brr[``2``] / ``2``;``  ` `        ``// Assigning the remaining brr2 elements.``        ``brr[``2``] %= ``2``;``  ` `        ``// If we are left with one brr2 element.``        ``if` `(brr[``2``] == ``1``) {``  ` `            ``// Here we need only two operations``            ``// to convert the remaining one``            ``// brr2 element to convert it.``            ``min_opr += ``2``;``  ` `            ``// Now there is no brr2 element.``            ``brr[``2``] = ``0``;``  ` `            ``// Remaining brr3 elements.``            ``if` `(brr[``3``] == ``1``)             ``                ``brr[``3``] -= ``2``;            ``  ` `            ``// Remaining brr1 elements.``            ``if` `(brr[``1``]== ``1``) ``                ``brr[``1``] -= ``2``;            ``        ``}``  ` `        ``// If we are left with brr1 and brr2``        ``// elements then, we have to take four``        ``// of them and fixing them all together``        ``// in 3 operations.``        ``if` `(brr[``1``] == ``1``)        ``            ``min_opr += (brr[``1``] / ``4``) * ``3``;        ``        ``if` `(brr[``3``] == ``1``)        ``            ``min_opr += (brr[``3``] / ``4``) * ``3``;        ``  ` `        ``// Returns the minimum operations.``        ``return` `min_opr;``    ``}``  ` `    ``// If it is not possible to convert the array.``    ``return` `-``1``;    ``}``  ` `// Driver function``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));``}``}`` ` `// This code is contributed by Prerna Saini.`

## Python3

 `# Python program to``# find Minimum number``# of operations to``# convert an array ``# so that arr[i] % 4 is zero.`` ` `# Function to find``# minimum operations.``def` `minimumOperations(arr,n):`` ` `    ``# Counting of all the elements``    ``# leaving remainder 1, 2, 3 when``    ``# divided by 4 in the array brr.``    ``# at positions 1, 2 and 3 respectively.``    ``brr ``=` `[ ``0``, ``0``, ``0``, ``0` `]``    ``for` `i ``in` `range``(n):``        ``brr[arr[i] ``%` `4``]``+``=``1``;``  ` `    ``# If it is possible to convert the ``    ``# array so that arr[i] % 4 is zero.``    ``if` `((brr[``1``] ``+` `2` `*` `brr[``2``] ``+` `3` `*` `brr[``3``]) ``%` `4` `=``=` `0``): ``     ` `        ``# Pairing the elements``        ``# of brr3 and brr1.``        ``min_opr ``=` `min``(brr[``3``], brr[``1``])``        ``brr[``3``] ``-``=` `min_opr``        ``brr[``1``] ``-``=` `min_opr``  ` `        ``# Pairing the brr2 elements.``        ``min_opr ``+``=` `brr[``2``] ``/``/` `2``  ` `        ``# Assigning the remaining``        ``# brr2 elements.``        ``brr[``2``] ``%``=` `2``  ` `        ``# If we are left with``        ``# one brr2 element.``        ``if` `(brr[``2``]):``  ` `            ``# Here we need only two operations``            ``# to convert the remaining one``            ``# brr2 element to convert it.``            ``min_opr ``+``=` `2``  ` `            ``# Now there is no brr2 element.``            ``brr[``2``] ``=` `0``  ` `            ``# Remaining brr3 elements.``            ``if` `(brr[``3``]):             ``                ``brr[``3``] ``-``=` `2`            `  ` `            ``# Remaining brr1 elements.``            ``if` `(brr[``1``]): ``                ``brr[``1``] ``-``=` `2`            `         ` `  ` `        ``# If we are left with brr1 and brr2``        ``# elements then, we have to take four``        ``# of them and fixing them all together``        ``# in 3 operations.``        ``if` `(brr[``1``]):        ``            ``min_opr ``+``=` `(brr[``1``] ``/``/` `4``) ``*` `3`        `        ``if` `(brr[``3``]):        ``            ``min_opr ``+``=` `(brr[``3``] ``/``/` `4``) ``*` `3`        `  ` `        ``# Returns the minimum operations.``        ``return` `min_opr`` ` `    ``# If it is not possible to convert the array.``    ``return` `-``1`    ` ` `# Driver function`` ` `arr ``=` `[ ``1``, ``2``, ``3``, ``1``, ``2``, ``3``, ``8` `]``n ``=``len``(arr)`` ` `print``(minimumOperations(arr, n))`` ` `# This code is contributed``# by Anant Agarwal.`

## C#

 `// C# program to find Minimum number``// of operations to convert an array ``// so that arr[i] % 4 is zero.``using` `System;`` ` `class` `GFG {`` ` `    ``// Function to find minimum operations.``    ``static` `int` `minimumOperations(``int` `[]arr, ``int` `n)``    ``{ ``     ` `        ``// Counting of all the elements``        ``// leaving remainder 1, 2, 3 when``        ``// divided by 4 in the array brr.``        ``// at positions 1, 2 and 3 respectively.``        ``int` `[]brr = { 0, 0, 0, 0 };``        ``for` `(``int` `i = 0; i < n; i++)``            ``brr[arr[i] % 4]++;``     ` `        ``// If it is possible to convert the ``        ``// array so that arr[i] % 4 is zero.``        ``if` `((brr + 2 * brr + 3 * brr) % 4 == 0) ``        ``{``            ``// Pairing the elements of brr3 and brr1.``            ``int` `min_opr = Math.Min(brr, brr);``            ``brr -= min_opr;``            ``brr -= min_opr;``     ` `            ``// Pairing the brr2 elements.``            ``min_opr += brr / 2;``     ` `            ``// Assigning the remaining brr2 elements.``            ``brr %= 2;``     ` `            ``// If we are left with one brr2 element.``            ``if` `(brr == 1) {``     ` `                ``// Here we need only two operations``                ``// to convert the remaining one``                ``// brr2 element to convert it.``                ``min_opr += 2;``     ` `                ``// Now there is no brr2 element.``                ``brr = 0;``     ` `                ``// Remaining brr3 elements.``                ``if` `(brr == 1)             ``                    ``brr -= 2;         ``     ` `                ``// Remaining brr1 elements.``                ``if` `(brr== 1) ``                    ``brr -= 2;         ``            ``}``     ` `            ``// If we are left with brr1 and brr2``            ``// elements then, we have to take four``            ``// of them and fixing them all together``            ``// in 3 operations.``            ``if` `(brr == 1)     ``                ``min_opr += (brr / 4) * 3;     ``            ``if` `(brr == 1)     ``                ``min_opr += (brr / 4) * 3;     ``     ` `            ``// Returns the minimum operations.``            ``return` `min_opr;``        ``}``     ` `        ``// If it is not possible to convert the array.``        ``return` `-1; ``    ``}``     ` `    ``// Driver function``    ``public` `static` `void` `Main()``    ``{``        ``int` `[]arr = { 1, 2, 3, 1, 2, 3, 8 };``        ``int` `n = arr.Length;``        ``Console.WriteLine(minimumOperations(arr, n));``    ``}``}`` ` `// This code is contributed by  vt_m`

## PHP

 ``

Output:
```3
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up