Minimum operations to make all elements equal using the second array
Last Updated :
10 Nov, 2021
Given two arrays A[] and B[] both having N elements. Find the minimum number of operations to make all elements of A equal to the second array B. An operation comprises of:
A[i] = A[i] - B[i], 0 <= i <= n-1
Note: If it’s not possible to make array elements equal print -1.
Examples:
Input: A[] = {5, 7, 10, 5, 15}, B[] = {2, 2, 1, 3, 5}
Output: 8
Explanation:
Following are the operations:
1) Choosing index 1 -> 5 5 10 5 15
2) Choosing index 2 -> 5 5 9 5 15
3) Choosing index 2 -> 5 5 8 5 15
4) Choosing index 2 -> 5 5 7 5 15
5) Choosing index 2 -> 5 5 6 5 15
6) Choosing index 2 -> 5 5 5 5 15
7) Choosing index 4 -> 5 5 5 5 10
8) Choosing index 4 -> 5 5 5 5 5
Input: A[] = {3, 5, 8, 2}, B[] = {1, 2, 1, 1}
Output: 12
Approach:
- It can be observed that the maximum possible value if all elements of A can be made equal is the minimum element of A.
- So we can iterate over the final value x, when all elements of A become equal. Using above observation – 0 <= x <= min(A[i]). For each x, traverse A and check whether A[i] can be made equal to x using B[i] :
A[i] - k*B[i] = x
Take mod with B[i] on both sides
A[i] %B[i] = x %B[i] ->
must be satisfied for A[i] to be converted to x
- If condition is satisfied, then number of operations required for this element = (A[i] – x)/B[i]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int a[], int b[], int n)
{
int minA = *min_element(a, a + n);
for ( int x = minA; x >= 0; x--) {
bool check = 1;
int operations = 0;
for ( int i = 0; i < n; i++) {
if (x % b[i] == a[i] % b[i]) {
operations +=
(a[i] - x) / b[i];
}
else {
check = 0;
break ;
}
}
if (check)
return operations;
}
return -1;
}
int main()
{
int N = 5;
int A[N] = { 5, 7, 10, 5, 15 };
int B[N] = { 2, 2, 1, 3, 5 };
cout << minOperations(A, B, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minOperations( int a[], int b[], int n)
{
int minA = Arrays.stream(a).min().getAsInt();
for ( int x = minA; x >= 0 ; x--)
{
boolean check = true ;
int operations = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (x % b[i] == a[i] % b[i])
{
operations += (a[i] - x) / b[i];
}
else
{
check = false ;
break ;
}
}
if (check)
return operations;
}
return - 1 ;
}
public static void main(String[] args)
{
int N = 5 ;
int A[] = { 5 , 7 , 10 , 5 , 15 };
int B[] = { 2 , 2 , 1 , 3 , 5 };
System.out.print(minOperations(A, B, N));
}
}
|
Python3
def minOperations(a, b, n):
minA = min (a);
for x in range (minA, - 1 , - 1 ):
check = True ;
operations = 0 ;
for i in range (n):
if (x % b[i] = = a[i] % b[i]):
operations + = (a[i] - x) / b[i];
else :
check = False ;
break ;
if (check):
return operations;
return - 1 ;
if __name__ = = '__main__' :
N = 5 ;
A = [ 5 , 7 , 10 , 5 , 15 ];
B = [ 2 , 2 , 1 , 3 , 5 ];
print ( int (minOperations(A, B, N)));
|
C#
using System;
using System.Linq;
class GFG{
static int minOperations( int []a, int []b, int n)
{
int minA = a.Max();
for ( int x = minA; x >= 0; x--)
{
bool check = true ;
int operations = 0;
for ( int i = 0; i < n; i++)
{
if (x % b[i] == a[i] % b[i])
{
operations += (a[i] - x) / b[i];
}
else
{
check = false ;
break ;
}
}
if (check)
return operations;
}
return -1;
}
public static void Main( string [] args)
{
int N = 5;
int []A = { 5, 7, 10, 5, 15 };
int []B = { 2, 2, 1, 3, 5 };
Console.WriteLine(minOperations(A, B, N));
}
}
|
Javascript
<script>
function minOperations(a , b , n)
{
var minA = Math.max.apply(Math,a);;
for (x = minA; x >= 0; x--) {
var check = true ;
var operations = 0;
for (i = 0; i < n; i++) {
if (x % b[i] == a[i] % b[i]) {
operations += (a[i] - x) / b[i];
}
else {
check = false ;
break ;
}
}
if (check)
return operations;
}
return -1;
}
var N = 5;
var A = [ 5, 7, 10, 5, 15 ];
var B = [ 2, 2, 1, 3, 5 ];
document.write(minOperations(A, B, N));
</script>
|
Time Complexity: O(N * min(Ai))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...