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++
// CPP program to find Minimum number // of operations to convert an array // so that arr[i] % 4 is zero. #include <bits/stdc++.h> 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[1] + 2 * brr[2] + 3 * brr[3]) % 4 == 0)     {         // Pairing the elements of brr3 and brr1.         int 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 int main() { Â Â Â Â int arr[] = { 1, 2, 3, 1, 2, 3, 8 }; Â Â Â Â int n = sizeof (arr) / sizeof (arr[0]); Â Â Â Â 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 ] != 0 )                   min_opr += (brr[ 1 ] / 4 ) * 3 ;               if (brr[ 3 ] != 0 )                   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[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()     {         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
<?php // PHP program to find // Minimum number of // operations to convert // an array so that // arr[i] % 4 is zero. Â
// Function to find // minimum operations. function 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 = array (0, 0, 0, 0);     for ( $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.         $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 Code $arr = array (1, 2, 3, Â Â Â Â Â Â Â Â Â Â Â Â Â 1, 2, 3, 8); $n = count ( $arr ); echo (minimumOperations( $arr , $n )); Â
// This code is contributed by // Manish Shaw(manishshaw1) ?> |
Javascript
<script> // Java Script program to find Minimum number // of operations to convert an array // so that arr[i] % 4 is zero. Â
Â
  // Function to find minimum operations. function 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.     let brr = [0, 0, 0, 0 ];     for (let 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.         let 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 Â
    let arr= [1, 2, 3, 1, 2, 3, 8 ];     let n = arr.length;     document.write(minimumOperations(arr, n)); Â
// This code is contributed by Bobby </script> |
3
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!