📌  相关文章
📜  使数组每个元素可被 4 整除的最小对求和运算

📅  最后修改于: 2022-05-13 01:57:50.382000             🧑  作者: Mango

使数组每个元素可被 4 整除的最小对求和运算

给定一个长度为n的正整数数组。我们的任务是找到转换数组的最小操作数,以使每个iarr[i] % 4为零。在每个操作中,我们可以从数组中取出任意两个元素,将它们都删除,然后将它们的总和放回数组中。
例子:

方法:假设除以4时剩余1、2、3的元素计数为brr[1]brr[2]brr[3]
如果(brr[1] + 2 * brr[2] + 3 * brr[3])不是4的倍数,则不存在解。
现在贪婪地将brr[2]的元素与brr[2]brr[1]的元素与brr[3] 配对。这有助于我们一次最多修复2 个元素。现在,我们要么只留下1 个brr[2]元素,要么没有。如果我们剩下1 个brr[2]元素,那么我们可以与剩余的2 个brr[1]brr[3]元素配对。这将导致总共2 次操作。
最后,我们将只剩下brr[1]brr[3]元素(如果可能的话)。这只能我们以一种方式解决。这需要其中的4 个,并在3 个操作中将它们全部固定在一起。因此,我们能够修复数组的所有元素。
下面是实现:

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[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] == 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[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


Javascript


输出:

3