Skip to content
Related Articles

Related Articles

Minimum prefix increments required to make all elements of an array multiples of another array
  • Last Updated : 09 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

Given two arrays A[] and B[] of size N, the task is to find the minimum count of operations required to make A[i] multiples of B[i] by incrementing prefix subarrays by 1.

Examples:

Input: A[ ] = { 3, 2, 9 }, B[ ] = { 5, 7, 4 }, N = 3
Output: 7
Explanation:
Incrementing {A[0]} twice modifies A[] to { 5, 2, 9}
Incrementing all the elements of subarray {A[0], A[1]} twice modifies A[] to { 7, 4, 9}
Incrementing all the elements of the subarray { A[0], A[1], A[2]} thrice modifies A[] to { 10, 7, 12
Therefore, total operations required = 2 + 2 + 3 = 7.

Input: A[ ] = { 3, 4, 5, 2, 5, 5, 9 }, B[ ] = { 1, 1, 9, 6, 3, 8, 7 }, N = 7
Output: 22

Approach: The problem can be solved using Greedy technique. In order to minimize the number of operations, the idea is to find the closest smallest greater or equal element of A[i] which is a multiple of B[i]:



  1. Traverse the array A[] from the end to the beginning.
  2. Find the minimum difference K,  between the nearest multiple of the corresponding element of B[].
  3. Since K is equal to the number of operations at ith element, thus the value of all elements from 0th index till (i-1)th index will increment by K. 
  4. Now maintain a variable carry which will store the cumulative increment, thus if ith element is incremented K times then add K to the carry
  5. Value of carry variable will be used to find the new value of (i-1)th element of A[].

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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.
Output: 
22

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :