📌  相关文章
📜  使用第二个数组使所有元素相等的最小操作

📅  最后修改于: 2021-05-17 18:21:09             🧑  作者: Mango

给定两个数组A []B []都具有N个元素。查找使A的所有元素等于第二个数组B的最小操作数。一个操作包括:

A[i] = A[i] - B[i], 0 <= i <= n-1 

注意:如果不可能使数组元素等于print -1。
例子:

方法:

  • 可以观察到,如果可以使A的所有元素相等,则最大可能值是A的最小元素。
  • 因此,当A的所有元素都相等时,我们可以遍历最终值x。使用上述观察结果– 0 <= x <= min(A [i])。对于每个x,遍历A并检查是否可以使用B [i]使A [i]等于x:
A[i] - k*B[i] = x
Take mod with B[i] on both sides
A[i] %B[i] = x %B[i]  ->  
must be satisfied for A[i] to be converted to x
  • 如果满足条件,则此元素所需的操作数=(A [i] – x)/ B [i]

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum operations make all elements
// equal using the second array
 
#include 
 
using namespace std;
 
// Function to find the minimum
// operations required to make
// all elements of the array equal
int minOperations(int a[], int b[], int n)
{
    // Minimum element of A[]
    int minA = *min_element(a, a + n);
 
    // Traverse through all final values
    for (int x = minA; x >= 0; x--) {
         
        // Variable indicating
        // whether all elements
        // can be converted to x or not
        bool check = 1;
 
        // Total operations
        int operations = 0;
         
        // Traverse through
        // all array elements
        for (int i = 0; i < n; i++) {
            if (x % b[i] == a[i] % b[i]) {
                operations +=
                    (a[i] - x) / b[i];
            }
 
            // All elements can't
            // be converted to x
            else {
                check = 0;
                break;
            }
        }
        if (check)
            return operations;
    }
    return -1;
}
 
// Driver Code
int main()
{
    int N = 5;
    int A[N] = { 5, 7, 10, 5, 15 };
    int B[N] = { 2, 2, 1, 3, 5 };
 
    cout << minOperations(A, B, N);
    return 0;
}


Java
// Java implementation to find the
// minimum operations make all elements
// equal using the second array
import java.util.*;
class GFG{
 
// Function to find the minimum
// operations required to make
// all elements of the array equal
static int minOperations(int a[], int b[], int n)
{
    // Minimum element of A[]
    int minA = Arrays.stream(a).min().getAsInt();
 
    // Traverse through all final values
    for (int x = minA; x >= 0; x--)
    {
         
        // Variable indicating
        // whether all elements
        // can be converted to x or not
        boolean check = true;
 
        // Total operations
        int operations = 0;
         
        // Traverse through
        // all array elements
        for (int i = 0; i < n; i++)
        {
            if (x % b[i] == a[i] % b[i])
            {
                operations += (a[i] - x) / b[i];
            }
 
            // All elements can't
            // be converted to x
            else
            {
                check = false;
                break;
            }
        }
        if (check)
            return operations;
    }
    return -1;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5;
    int A[] = { 5, 7, 10, 5, 15 };
    int B[] = { 2, 2, 1, 3, 5 };
 
    System.out.print(minOperations(A, B, N));
}
}
 
// This code is contributed by AbhiThakur


Python3
# Python3 implementation to find the
# minimum operations make all elements
# equal using the second array
 
# Function to find the minimum
# operations required to make
# all elements of the array equal
def minOperations(a, b, n):
     
    # Minimum element of A
    minA = min(a);
 
    # Traverse through all final values
    for x in range(minA, -1, -1):
 
        # Variable indicating
        # whether all elements
        # can be converted to x or not
        check = True;
 
        # Total operations
        operations = 0;
 
        # Traverse through
        # all array elements
        for i in range(n):
            if (x % b[i] == a[i] % b[i]):
                operations += (a[i] - x) / b[i];
 
            # All elements can't
            # be converted to x
            else:
                check = False;
                break;
 
        if (check):
            return operations;
 
    return -1;
 
# Driver Code
if __name__ == '__main__':
     
    N = 5;
    A = [ 5, 7, 10, 5, 15 ];
    B = [ 2, 2, 1, 3, 5 ];
 
    print(int(minOperations(A, B, N)));
 
# This code is contributed by amal kumar choubey


C#
// C# implementation to find the
// minimum operations make all elements
// equal using the second array
using System;
using System.Linq;
 
class GFG{
 
// Function to find the minimum
// operations required to make
// all elements of the array equal
static int minOperations(int []a, int []b, int n)
{
     
    // Minimum element of A[]
    int minA = a.Max();
 
    // Traverse through all final values
    for(int x = minA; x >= 0; x--)
    {
        
       // Variable indicating
       // whether all elements
       // can be converted to x or not
       bool check = true;
        
       // Total operations
       int operations = 0;
        
       // Traverse through
       // all array elements
       for(int i = 0; i < n; i++)
       {
          if (x % b[i] == a[i] % b[i])
          {
              operations += (a[i] - x) / b[i];
          }
           
          // All elements can't
          // be converted to x
          else
          {
              check = false;
              break;
          }
       }
       if (check)
           return operations;
    }
    return -1;
}
 
// Driver Code
public static void Main(string[] args)
{
    int N = 5;
    int []A = { 5, 7, 10, 5, 15 };
    int []B = { 2, 2, 1, 3, 5 };
 
    Console.WriteLine(minOperations(A, B, N));
}
}
 
// This code is contributed by SoumikMondal


Javascript


输出:
8

时间复杂度: O(N * min(A i ))