Minimize cost of increments or decrements such that same indexed elements become multiple of each other
Last Updated :
29 Apr, 2021
Given two arrays A[] and B[] consisting of N integers, the task is to minimize the total cost of incrementing or decrementing array elements by 1 such that for every ith element, either A[i] is a multiple of B[i] or vice-versa.
Examples:
Input: A[] = {3, 6, 3}, B[] = {4, 8, 13}
Output: 4
Explanation:
Incrementing A[0] by 1 (3 + 1 = 4) makes it multiple of B[0](= 4).
Incrementing A[1] by 2 (6 + 2 = 8) makes it a multiple of B[1](= 8).
Decrementing B[2] by 1 (13 – 1 = 12) makes it a multiple of A[2](= 3).
Therefore, the total cost = 1 + 2 + 1 = 4.
Input: A[] = {13, 2, 31, 7}, B[] = {6, 8, 11, 3}
Output: 4
Approach: The given problem can be solved greedily. Follow the steps below to solve the problem:
- Initialize a variable, say cost, to store the required minimum cost.
- Traverse both the arrays A[] and B[] simultaneously and perform the following steps:
- Case 1: Find the cost to update A[i] to make it a multiple of B[i], which is the minimum of (B[i] % A[i]) and (A[i] – B[i] % A[i]).
- Case 2: Find the cost to update B[i] to make it a multiple of A[i], which is the minimum of (A[i] % B[i]) and (B[i] – A[i] % B[i]).
- Add the minimum of the above two costs to the variable cost for each array element.
- After completing the above steps, print the value of cost as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinimumCost( int A[], int B[],
int N)
{
int totalCost = 0;
for ( int i = 0; i < N; i++) {
int mod_A = B[i] % A[i];
int totalCost_A = min(mod_A,
A[i] - mod_A);
int mod_B = A[i] % B[i];
int totalCost_B = min(mod_B,
B[i] - mod_B);
totalCost += min(totalCost_A,
totalCost_B);
}
return totalCost;
}
int main()
{
int A[] = { 3, 6, 3 };
int B[] = { 4, 8, 13 };
int N = sizeof (A) / sizeof (A[0]);
cout << MinimumCost(A, B, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int MinimumCost( int A[], int B[], int N)
{
int totalCost = 0 ;
for ( int i = 0 ; i < N; i++)
{
int mod_A = B[i] % A[i];
int totalCost_A = Math.min(mod_A,
A[i] - mod_A);
int mod_B = A[i] % B[i];
int totalCost_B = Math.min(mod_B,
B[i] - mod_B);
totalCost += Math.min(totalCost_A,
totalCost_B);
}
return totalCost;
}
public static void main(String[] args)
{
int A[] = { 3 , 6 , 3 };
int B[] = { 4 , 8 , 13 };
int N = A.length;
System.out.print(MinimumCost(A, B, N));
}
}
|
Python3
def MinimumCost(A, B, N):
totalCost = 0
for i in range (N):
mod_A = B[i] % A[i]
totalCost_A = min (mod_A, A[i] - mod_A)
mod_B = A[i] % B[i]
totalCost_B = min (mod_B, B[i] - mod_B)
totalCost + = min (totalCost_A, totalCost_B)
return totalCost
A = [ 3 , 6 , 3 ]
B = [ 4 , 8 , 13 ]
N = len (A)
print (MinimumCost(A, B, N))
|
C#
using System;
class GFG {
static int MinimumCost( int [] A, int [] B, int N)
{
int totalCost = 0;
for ( int i = 0; i < N; i++) {
int mod_A = B[i] % A[i];
int totalCost_A = Math.Min(mod_A, A[i] - mod_A);
int mod_B = A[i] % B[i];
int totalCost_B = Math.Min(mod_B, B[i] - mod_B);
totalCost += Math.Min(totalCost_A, totalCost_B);
}
return totalCost;
}
public static void Main()
{
int [] A = { 3, 6, 3 };
int [] B = { 4, 8, 13 };
int N = A.Length;
Console.Write(MinimumCost(A, B, N));
}
}
|
Javascript
<script>
function MinimumCost(A , B , N) {
var totalCost = 0;
for (i = 0; i < N; i++) {
var mod_A = B[i] % A[i];
var totalCost_A = Math.min(mod_A, A[i] - mod_A);
var mod_B = A[i] % B[i];
var totalCost_B = Math.min(mod_B, B[i] - mod_B);
totalCost += Math.min(totalCost_A, totalCost_B);
}
return totalCost;
}
var A = [ 3, 6, 3 ];
var B = [ 4, 8, 13 ];
var N = A.length;
document.write(MinimumCost(A, B, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...