Minimum prefix increments required to make all elements of an array multiples of another array
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 the 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]:
- Traverse the array A[] from the end to the beginning.
- Find the minimum difference K, between the nearest multiple of the corresponding element of B[].
- 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.
- 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
int MinimumMoves( int A[], int B[], int N)
{
int totalOperations = 0;
int carry = 0;
int K = 0;
for ( int i = N - 1; i >= 0; i--) {
int nearestMultiple = ceil (( double )(A[i] + carry)
/ ( double )(B[i]))
* B[i];
K = nearestMultiple - (A[i] + carry);
totalOperations += K;
carry += K;
}
return totalOperations;
}
int main()
{
int A[] = { 3, 4, 5, 2, 5, 5, 9 };
int B[] = { 1, 1, 9, 6, 3, 8, 7 };
int N = sizeof (A) / sizeof (A[0]);
cout << MinimumMoves(A, B, N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MinimumMoves( int A[], int B[], int N)
{
int totalOperations = 0 ;
int carry = 0 ;
int K = 0 ;
for ( int i = N - 1 ; i >= 0 ; i--)
{
int nearestMultiple = ( int ) (Math.ceil(( double )(A[i] + carry)
/ ( double )(B[i]))
* B[i]);
K = nearestMultiple - (A[i] + carry);
totalOperations += K;
carry += K;
}
return totalOperations;
}
public static void main(String[] args)
{
int A[] = { 3 , 4 , 5 , 2 , 5 , 5 , 9 };
int B[] = { 1 , 1 , 9 , 6 , 3 , 8 , 7 };
int N = A.length;
System.out.print(MinimumMoves(A, B, N) + "\n" );
}
}
|
Python3
from math import ceil,floor
def MinimumMoves(A, B, N):
totalOperations = 0
carry = 0
K = 0
for i in range (N - 1 , - 1 , - 1 ):
nearestMultiple = ceil((A[i] + carry) / B[i]) * B[i]
K = nearestMultiple - (A[i] + carry)
totalOperations + = K
carry + = K
return totalOperations
if __name__ = = '__main__' :
A = [ 3 , 4 , 5 , 2 , 5 , 5 , 9 ]
B = [ 1 , 1 , 9 , 6 , 3 , 8 , 7 ]
N = len (A)
print (MinimumMoves(A, B, N))
|
C#
using System;
class GFG{
static int MinimumMoves( int [] A, int [] B, int N)
{
int totalOperations = 0;
int carry = 0;
int K = 0;
for ( int i = N - 1; i >= 0; i--)
{
int nearestMultiple = ( int ) (Math.Ceiling(( double )(A[i] + carry)
/ ( double )(B[i]))
* B[i]);
K = nearestMultiple - (A[i] + carry);
totalOperations += K;
carry += K;
}
return totalOperations;
}
public static void Main( string [] args)
{
int [] A = { 3, 4, 5, 2, 5, 5, 9 };
int [] B = { 1, 1, 9, 6, 3, 8, 7 };
int N = A.Length;
Console.Write(MinimumMoves(A, B, N) + "\n" );
}
}
|
Javascript
<script>
function MinimumMoves(A, B, N)
{
let totalOperations = 0;
let carry = 0;
let K = 0;
for (let i = N - 1; i >= 0; i--)
{
let nearestMultiple = (Math.ceil((A[i] + carry)
/ (B[i]))
* B[i]);
K = nearestMultiple - (A[i] + carry);
totalOperations += K;
carry += K;
}
return totalOperations;
}
let A = [ 3, 4, 5, 2, 5, 5, 9 ];
let B = [ 1, 1, 9, 6, 3, 8, 7 ];
let N = A.length;
document.write(MinimumMoves(A, B, N) + "<br/>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
03 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...