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.
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.
- 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:
Time Complexity: O(NlogN)
- Minimum number of given operations required to make two strings equal
- Minimum deletions required to make GCD of the array equal to 1
- Minimum increment by k operations to make all elements equal
- Minimum operations to make frequency of all characters equal K
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of operations required to reduce N to 1
- Minimum number of operations required to delete all elements of the array
- Minimum operations to make GCD of array a multiple of k
- Minimum operations to make sum of neighbouring elements <= X
- Minimum number of pairs required to make two strings same
- Minimum swaps required to make a binary string alternating
- Minimum array insertions required to make consecutive difference <= K
- Check whether it is possible to make both arrays equal by modifying a single element
- Make array elements equal in Minimum Steps
- Minimum value of X to make all array elements equal by either decreasing or increasing by X