📌  相关文章
📜  通过执行a [i] – b [i]使数组a []的所有元素等于其min元素的操作计数

📅  最后修改于: 2021-05-17 01:49:17             🧑  作者: Mango

给定两个数组a []b []的大小为N,任务是通过执行[I]进行打印操作的计需要使所有的阵列的[I]等于其最小元素的元素– B [我] ,其中总是a [i]> = b [i] 。如果不可能,则返回-1。
例子:

方法:要解决上述问题,请执行以下步骤:

  • 从数组a []中查找最小值。初始化一个变量ans = -1,该变量存储结果减法运算。
  • 从数组a []的最小元素迭代到0,并将变量curr初始化为0,该变量存储计数减法以使数组元素相等。
  • 遍历数组,并检查a [i]是否不等于x(第一个数组中的最小元素),然后使其等于最小值,否则更新curr等于零。
  • 检查curr是否不等于0,然后更新ans,因为curr最终返回ans

下面是上述方法的实现:

C++
// C++ program to count the operations
// to make all elements of array a[]
// equal to its min element
// by performing a[i] – b[i]
 
#include 
using namespace std;
 
// Function to convert all Element of
// first array equal using second array
int findMinSub(int a[], int b[], int n)
{
    // Get the minimum from first array
    int min = INT_MAX;
    for (int i = 0; i < n; i++) {
        if (a[i] < min)
            min = a[i];
    }
 
    // Variable that stores count of
    // resultant required subtraction
    // to Convert all elements equal
    int ans = -1;
 
    for (int x = min; x >= 0; x--)
 
    {
        // Stores the count subtraction to
        // make the array element
        // equal for each iteration
        int curr = 0;
 
        // Traverse the array and check if
        // a[i] is not equal to x then
        // Make it equal to minimum else
        // update current equal to zero
        for (int i = 0; i < n; i++) {
            if (a[i] != x) {
 
                if (b[i] > 0
                    && (a[i] - x) % b[i] == 0) {
                    curr += (a[i] - x) / b[i];
                }
                else {
                    curr = 0;
                    break;
                }
            }
        }
        // Check if curr is not equal to
        // zero then update the answer
        if (curr != 0) {
            ans = curr;
            break;
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
 
    int a[] = { 5, 7, 10, 5, 15 };
    int b[] = { 2, 2, 1, 3, 5 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << findMinSub(a, b, n);
    return 0;
}


Java
// Java program to count the operations
// to make all elements of array a[]
// equal to its min element
// by performing a[i] – b[i]
import java.util.*;
 
class GFG{
 
// Function to convert all element of
// first array equal using second array
static int findMinSub(int a[], int b[], int n)
{
     
    // Get the minimum from first array
    int min = Integer.MAX_VALUE;
    for(int i = 0; i < n; i++)
    {
    if (a[i] < min)
        min = a[i];
    }
 
    // Variable that stores count of
    // resultant required subtraction
    // to Convert all elements equal
    int ans = -1;
 
    for(int x = min; x >= 0; x--)
    {
         
    // Stores the count subtraction
    // to make the array element
    // equal for each iteration
    int curr = 0;
         
    // Traverse the array and check
    // if a[i] is not equal to x then
    // Make it equal to minimum else
    // update current equal to zero
    for(int i = 0; i < n; i++)
    {
        if (a[i] != x)
        {
            if (b[i] > 0 &&
                (a[i] - x) % b[i] == 0)
            {
                curr += (a[i] - x) / b[i];
            }
            else
            {
                curr = 0;
                break;
            }
        }
    }
         
    // Check if curr is not equal to
    // zero then update the answer
    if (curr != 0)
    {
        ans = curr;
        break;
    }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 5, 7, 10, 5, 15 };
    int b[] = { 2, 2, 1, 3, 5 };
    int n = a.length;
 
    System.out.print(findMinSub(a, b, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to count the operations
# to make all elements of array a[]
# equal to its min element
# by performing a[i] – b[i]
 
# Function to convert all element of
# first array equal using second array
def findMinSub(a, b, n):
     
    # Get the minimum from first array
    min = a[0]
    for i in range(0, n):
        if a[i] < min:
            min = a[i]
             
    # Variable that stores count of
    # resultant required subtraction
    # to Convert all elements equal
    ans = -1
    for x in range(min, -1, -1):
         
        # Stores the count subtraction
        # to make the array element
        # equal for each iteration
        curr = 0
 
        # Traverse the array and check
        # if a[i] is not equal to x then
        # Make it equal to minimum else
        # update current equal to zero
        for i in range(0, n):
            if a[i] != x:
                 
                if (b[i] > 0 and
                   (a[i] - x) % b[i] == 0):
                    curr += (a[i] - x) // b[i]
                else:
                    curr = 0
                    break
 
        # Check if curr is not equal to
        # zero then update the answer
        if curr != 0:
            ans = curr
            break
         
    return ans
 
# Driver code
a = [ 5, 7, 10, 5, 15 ]
b = [ 2, 2, 1, 3, 5 ]
n = len(a)
 
print(findMinSub(a, b, n))
 
# This code is contributed by jrishabh99


C#
// C# program to count the operations
// to make all elements of array a[]
// equal to its min element
// by performing a[i] – b[i]
using System;
class GFG{
 
// Function to convert all element of
// first array equal using second array
static int findMinSub(int []a, int []b, int n)
{
     
    // Get the minimum from first array
    int min = Int32.MaxValue;
     
    for(int i = 0; i < n; i++)
    {
        if (a[i] < min)
            min = a[i];
    }
 
    // Variable that stores count of
    // resultant required subtraction
    // to Convert all elements equal
    int ans = -1;
 
    for(int x = min; x >= 0; x--)
    {
         
        // Stores the count subtraction
        // to make the array element
        // equal for each iteration
        int curr = 0;
             
        // Traverse the array and check
        // if a[i] is not equal to x then
        // Make it equal to minimum else
        // update current equal to zero
        for(int i = 0; i < n; i++)
        {
            if (a[i] != x)
            {
                if (b[i] > 0 &&
                    (a[i] - x) % b[i] == 0)
                {
                    curr += (a[i] - x) / b[i];
                }
                else
                {
                    curr = 0;
                    break;
                }
            }
        }
             
        // Check if curr is not equal to
        // zero then update the answer
        if (curr != 0)
        {
            ans = curr;
            break;
        }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int []a = { 5, 7, 10, 5, 15 };
    int []b = { 2, 2, 1, 3, 5 };
    int n = a.Length;
 
    Console.Write(findMinSub(a, b, n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
8