📌  相关文章
📜  使一个数组的所有元素成为另一个数组的倍数所需的最小前缀增量

📅  最后修改于: 2021-09-07 05:29:10             🧑  作者: Mango

给定两个阵列A []B []的大小为N,任务是找到操作的最小计数需要使A [I] B [I]的倍数由递增1前缀的子阵列。

例子:

方法:该问题可以使用贪心技术解决。为了最小化操作次数,想法是找到A[i]的最接近的最小大于或等于元素,它是B[i]的倍数:

  1. 从头到尾遍历数组A[]
  2. 找到B[]对应元素的最近倍数之间最小差异K
  3. 由于K等于i元素操作次数,因此0索引到第(i-1)索引的所有元素值将增加K。
  4. 现在维护一个将存储累积增量的变量进位,因此如果i元素增加K次,则将K添加到进位
  5. 进位变量的值将用于查找A[] 的(i-1)元素新值

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum count of operations
// required to make A[i] multiple of B[i] by
// incrementing prefix subarray
int MinimumMoves(int A[], int B[], int N)
{
 
    // Stores  minimum count of operations
    // required to make A[i] multiple of B[i]
    // by  incrementing prefix subarray
    int totalOperations = 0;
 
    // Stores the carry
    int carry = 0;
 
    // Stores minimum difference of
    // correspoinding element in
    // prefix subarray
    int K = 0;
 
    // Traverse the array
    for (int i = N - 1; i >= 0; i--) {
 
        // Stores the closest greater or equal number
        // to A[i] which is a multiple of B[i]
        int nearestMultiple = ceil((double)(A[i] + carry)
                                   / (double)(B[i]))
                              * B[i];
 
        // Stores minimum difference
        K = nearestMultiple - (A[i] + carry);
 
        // Update totalOperations
        totalOperations += K;
 
        // Update carry
        carry += K;
    }
 
    return totalOperations;
}
 
// Driver Code
int main()
{
 
    // Input arrays A[] and B[]
    int A[] = { 3, 4, 5, 2, 5, 5, 9 };
    int B[] = { 1, 1, 9, 6, 3, 8, 7 };
 
    // Length of arrays
    int N = sizeof(A) / sizeof(A[0]);
 
    cout << MinimumMoves(A, B, N) << endl;
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find minimum count of operations
// required to make A[i] multiple of B[i] by
// incrementing prefix subarray
static int MinimumMoves(int A[], int B[], int N)
{
 
    // Stores  minimum count of operations
    // required to make A[i] multiple of B[i]
    // by  incrementing prefix subarray
    int totalOperations = 0;
 
    // Stores the carry
    int carry = 0;
 
    // Stores minimum difference of
    // correspoinding element in
    // prefix subarray
    int K = 0;
 
    // Traverse the array
    for (int i = N - 1; i >= 0; i--)
    {
 
        // Stores the closest greater or equal number
        // to A[i] which is a multiple of B[i]
        int nearestMultiple = (int) (Math.ceil((double)(A[i] + carry)
                                   / (double)(B[i]))
                              * B[i]);
 
        // Stores minimum difference
        K = nearestMultiple - (A[i] + carry);
 
        // Update totalOperations
        totalOperations += K;
 
        // Update carry
        carry += K;
    }
    return totalOperations;
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Input arrays A[] and B[]
    int A[] = { 3, 4, 5, 2, 5, 5, 9 };
    int B[] = { 1, 1, 9, 6, 3, 8, 7 };
 
    // Length of arrays
    int N = A.length;
    System.out.print(MinimumMoves(A, B, N) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
from math import ceil,floor
 
# Function to find minimum count of operations
# required to make A[i] multiple of B[i] by
# incrementing prefix subarray
def MinimumMoves(A, B, N):
 
    # Stores  minimum count of operations
    # required to make A[i] multiple of B[i]
    # by  incrementing prefix subarray
    totalOperations = 0
 
    # Stores the carry
    carry = 0
 
    # Stores minimum difference of
    # correspoinding element in
    # prefix subarray
    K = 0
 
    # Traverse the array
    for i in range(N - 1, -1, -1):
 
        # Stores the closest greater or equal number
        # to A[i] which is a multiple of B[i]
        nearestMultiple = ceil((A[i] + carry)/ B[i])* B[i]
 
        # Stores minimum difference
        K = nearestMultiple - (A[i] + carry)
 
        # Update totalOperations
        totalOperations += K
 
        # Update carry
        carry += K
    return totalOperations
 
# Driver Code
if __name__ == '__main__':
 
    # Input arrays A[] and B[]
    A = [3, 4, 5, 2, 5, 5, 9]
    B = [1, 1, 9, 6, 3, 8, 7]
 
    # Length of arrays
    N = len(A)
    print (MinimumMoves(A, B, N))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find minimum count of operations
// required to make A[i] multiple of B[i] by
// incrementing prefix subarray
static int MinimumMoves(int[] A, int[] B, int N)
{
 
    // Stores  minimum count of operations
    // required to make A[i] multiple of B[i]
    // by  incrementing prefix subarray
    int totalOperations = 0;
 
    // Stores the carry
    int carry = 0;
 
    // Stores minimum difference of
    // correspoinding element in
    // prefix subarray
    int K = 0;
 
    // Traverse the array
    for (int i = N - 1; i >= 0; i--)
    {
 
        // Stores the closest greater or equal number
        // to A[i] which is a multiple of B[i]
        int nearestMultiple = (int) (Math.Ceiling((double)(A[i] + carry)
                                   / (double)(B[i]))
                              * B[i]);
 
        // Stores minimum difference
        K = nearestMultiple - (A[i] + carry);
 
        // Update totalOperations
        totalOperations += K;
 
        // Update carry
        carry += K;
    }
    return totalOperations;
}
 
// Driver Code
public static void Main(string[] args)
{
    // Input arrays A[] and B[]
    int[] A = { 3, 4, 5, 2, 5, 5, 9 };
    int[] B = { 1, 1, 9, 6, 3, 8, 7 };
 
    // Length of arrays
    int N = A.Length;
    Console.Write(MinimumMoves(A, B, N) +"\n");
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
22

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live