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++
// C++ implementation to find the
// minimum operations make all elements
// equal using the second array
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum
// operations required to make
// all elements of the array equal
int minOperations(int a[], int b[], int n)
{
// Minimum element of A[]
int minA = *min_element(a, a + n);
// Traverse through all final values
for (int x = minA; x >= 0; x--) {
// Variable indicating
// whether all elements
// can be converted to x or not
bool check = 1;
// Total operations
int operations = 0;
// Traverse through
// all array elements
for (int i = 0; i < n; i++) {
if (x % b[i] == a[i] % b[i]) {
operations +=
(a[i] - x) / b[i];
}
// All elements can't
// be converted to x
else {
check = 0;
break;
}
}
if (check)
return operations;
}
return -1;
}
// Driver Code
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
// Java implementation to find the
// minimum operations make all elements
// equal using the second array
import java.util.*;
class GFG{
// Function to find the minimum
// operations required to make
// all elements of the array equal
static int minOperations(int a[], int b[], int n)
{
// Minimum element of A[]
int minA = Arrays.stream(a).min().getAsInt();
// Traverse through all final values
for (int x = minA; x >= 0; x--)
{
// Variable indicating
// whether all elements
// can be converted to x or not
boolean check = true;
// Total operations
int operations = 0;
// Traverse through
// all array elements
for (int i = 0; i < n; i++)
{
if (x % b[i] == a[i] % b[i])
{
operations += (a[i] - x) / b[i];
}
// All elements can't
// be converted to x
else
{
check = false;
break;
}
}
if (check)
return operations;
}
return -1;
}
// Driver Code
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));
}
}
// This code is contributed by AbhiThakur
Python3
# Python3 implementation to find the
# minimum operations make all elements
# equal using the second array
# Function to find the minimum
# operations required to make
# all elements of the array equal
def minOperations(a, b, n):
# Minimum element of A
minA = min(a);
# Traverse through all final values
for x in range(minA, -1, -1):
# Variable indicating
# whether all elements
# can be converted to x or not
check = True;
# Total operations
operations = 0;
# Traverse through
# all array elements
for i in range(n):
if (x % b[i] == a[i] % b[i]):
operations += (a[i] - x) / b[i];
# All elements can't
# be converted to x
else:
check = False;
break;
if (check):
return operations;
return -1;
# Driver Code
if __name__ == '__main__':
N = 5;
A = [ 5, 7, 10, 5, 15 ];
B = [ 2, 2, 1, 3, 5 ];
print(int(minOperations(A, B, N)));
# This code is contributed by amal kumar choubey
C#
// C# implementation to find the
// minimum operations make all elements
// equal using the second array
using System;
using System.Linq;
class GFG{
// Function to find the minimum
// operations required to make
// all elements of the array equal
static int minOperations(int []a, int []b, int n)
{
// Minimum element of A[]
int minA = a.Max();
// Traverse through all final values
for(int x = minA; x >= 0; x--)
{
// Variable indicating
// whether all elements
// can be converted to x or not
bool check = true;
// Total operations
int operations = 0;
// Traverse through
// all array elements
for(int i = 0; i < n; i++)
{
if (x % b[i] == a[i] % b[i])
{
operations += (a[i] - x) / b[i];
}
// All elements can't
// be converted to x
else
{
check = false;
break;
}
}
if (check)
return operations;
}
return -1;
}
// Driver Code
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));
}
}
// This code is contributed by SoumikMondal
JavaScript
<script>
// javascript implementation to find the
// minimum operations make all elements
// equal using the second array
// Function to find the minimum
// operations required to make
// all elements of the array equal
function minOperations(a , b , n)
{
// Minimum element of A
var minA = Math.max.apply(Math,a);;
// Traverse through all final values
for (x = minA; x >= 0; x--) {
// Variable indicating
// whether all elements
// can be converted to x or not
var check = true;
// Total operations
var operations = 0;
// Traverse through
// all array elements
for (i = 0; i < n; i++) {
if (x % b[i] == a[i] % b[i]) {
operations += (a[i] - x) / b[i];
}
// All elements can't
// be converted to x
else {
check = false;
break;
}
}
if (check)
return operations;
}
return -1;
}
// Driver Code
var N = 5;
var A = [ 5, 7, 10, 5, 15 ];
var B = [ 2, 2, 1, 3, 5 ];
document.write(minOperations(A, B, N));
// This code is contributed by Rajput-Ji
</script>
Time Complexity: O(N * min(Ai))
Auxiliary Space: O(1)
Similar Reads
Minimum operations required to make all the array elements equal Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets d
6 min read
Minimum Cost to make all array elements equal using given operations Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is t
12 min read
Minimum operation to make all elements equal in array Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
Minimum cost to equal all elements of array using two operation Given an array arr[] of n positive integers. There are two operations allowed: Operation 1 : Pick any two indexes, increase value at one index by 1 and decrease value at another index by 1. It will cost a. Operation 2 : Pick any index and increase its value by 1. It will cost b. The task is to find
8 min read
Minimize steps to make Array elements equal by using giving operations Given an arr[] of length N. Then the task is to find minimum steps to make all the elements of the given array equal. Two types of operations can be performed as given below: Choose a prefix of size K, where arr[K] = max element in that chosen sub-array and convert all elements equal to max. Choose
8 min read