Minimum operations required to make every element greater than or equal to K
Last Updated :
25 Oct, 2022
Given an array of length N. The task is to convert it into a sequence in which all elements are greater than or equal to K.The only operation allowed is taking two smallest elements of the sequence and replace them by their LCM. Find the minimum number of operations required.
If it is impossible to get such an array, print -1.
Examples:
Input : N = 4, K = 3 , arr=[1 4 5 5]
Output : 1
LCM of 1 and 4 is 4, hence Replace (1,4) with 4.
Now the array becomes [4,4,5].
Every element in this array is greater than or equal to K.
No of operations required is equal to 1.
Input : N = 5, K = 8 , arr=[4,4,4,4,4]
Output : -1
It is not possible to convert the given array.
Approach:
- The idea is to use a priority queue(min heap) which can handle delete and insert operation in log(N) time.
- The impossible case will arise when the number of elements in the priority queue is less than 2. The answer is equal to -1 in this case.
- Otherwise, take two elements from the top of the queue and replace it by their LCM.
- Do this until the smallest number that is top of the queue is less than K.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int FindMinOperation( int a[], int n, int k)
{
priority_queue< int , vector< int >,
greater< int > > Q;
for ( int i = 0; i < n; i++)
Q.push(a[i]);
int ans = 0;
while (1) {
if (Q.top() >= k)
break ;
if (Q.size() < 2)
return -1;
int x = Q.top();
Q.pop();
int y = Q.top();
Q.pop();
int z = (x * y) / __gcd(x, y);
Q.push(z);
ans++;
}
return ans;
}
int main()
{
int a[] = { 3, 5, 7, 6, 8 };
int k = 8;
int n = sizeof (a) / sizeof (a[0]);
cout << FindMinOperation(a, n, k);
}
|
Java
import java.util.PriorityQueue;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int FindMinOperation( int [] a, int n, int k)
{
PriorityQueue<Integer> Q = new PriorityQueue<>();
for ( int i = 0 ; i < n; i++)
Q.add(a[i]);
int ans = 0 ;
while ( true )
{
if (Q.peek() >= k)
break ;
if (Q.size() < 2 )
return - 1 ;
int x = Q.peek();
Q.poll();
int y = Q.peek();
Q.poll();
int z = (x * y) / gcd(x, y);
Q.add(z);
ans++;
}
return ans;
}
public static void main(String[] args)
{
int [] a = { 3 , 5 , 7 , 6 , 8 };
int k = 8 ;
int n = a.length;
System.out.println(FindMinOperation(a, n, k));
}
}
|
Python3
def gcd(a, b) :
if (a = = 0 ) :
return b
return gcd(b % a, a)
def FindMinOperation(a, n, k) :
Q = []
for i in range ( 0 , n) :
Q.append(a[i])
Q.sort()
ans = 0
while ( True ) :
if (Q[ 0 ] > = k) :
break
if ( len (Q) < 2 ) :
return - 1
x = Q[ 0 ]
Q.pop( 0 )
y = Q[ 0 ]
Q.pop( 0 )
z = (x * y) / / gcd(x, y)
Q.append(z)
Q.sort()
ans + = 1
return ans
a = [ 3 , 5 , 7 , 6 , 8 ]
k = 8
n = len (a)
print (FindMinOperation(a, n, k))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int FindMinOperation( int [] a, int n, int k)
{
List< int > Q = new List< int >();
for ( int i = 0; i < n; i++)
Q.Add(a[i]);
Q.Sort();
int ans = 0;
while ( true )
{
if (Q[0] >= k)
break ;
if (Q.Count < 2)
return -1;
int x = Q[0];
Q.RemoveAt(0);
int y = Q[0];
Q.RemoveAt(0);
int z = (x * y) / gcd(x, y);
Q.Add(z);
Q.Sort();
ans++;
}
return ans;
}
static void Main()
{
int [] a = { 3, 5, 7, 6, 8 };
int k = 8;
int n = a.Length;
Console.WriteLine(FindMinOperation(a, n, k));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function FindMinOperation(a, n, k)
{
let Q = [];
for (let i = 0; i < n; i++)
Q.push(a[i]);
Q.sort( function (a, b){ return a - b});
let ans = 0;
while ( true )
{
if (Q[0] >= k)
break ;
if (Q.length < 2)
return -1;
let x = Q[0];
Q.shift();
let y = Q[0];
Q.shift();
let z = parseInt((x * y) / gcd(x, y), 10);
Q.push(z);
Q.sort( function (a, b){ return a - b});
ans++;
}
return ans;
}
let a = [ 3, 5, 7, 6, 8 ];
let k = 8;
let n = a.length;
document.write(FindMinOperation(a, n, k));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...