Minimize insertions required to make ratio of maximum and minimum of all pairs of adjacent array elements at most K
Last Updated :
16 Apr, 2021
Given an array arr[] and an integer K ( > 1), the task is to find the minimum number of insertions required such that the ratio of the maximum and minimum of all pairs of adjacent elements in the array reduces to at most K.
Examples:
Input : arr[] = { 2, 10, 25, 21 }, K = 2
Output: 3
Explanation:
Following insertions makes the array satisfy the required conditions {2, 4, 7, 10, 17, 25, 21}.
Input : arr[] = {2, 4, 1}, K = 2
Output: 1
Approach: The idea is to use greedily. Follow the steps to solve the problem :
- Traverse the array arr[].
- Initialize a variable, say ans, to store the number of insertions required.
- Iterate over the range [0, N – 2] and perform the following steps:
- Calculate min(arr[i], arr[i + 1]) and max(arr[i], arr[i + 1]) and store it in variables, say a and b.
- If (b > K * a): Update a = K * a and increment ans by 1.
- Print the value of ans as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int mininsert( int arr[], int K, int N)
{
int ans = 0;
for ( int i = 0; i < N - 1; i++) {
int a = min(arr[i], arr[i + 1]);
int b = max(arr[i], arr[i + 1]);
while (K * a < b) {
a *= K;
ans++;
}
}
return ans;
}
int main()
{
int arr[] = { 2, 10, 25, 21 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
cout << mininsert(arr, K, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int mininsert( int arr[], int K, int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N - 1 ; i++) {
int a = Math.min(arr[i], arr[i + 1 ]);
int b = Math.max(arr[i], arr[i + 1 ]);
while (K * a < b) {
a *= K;
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 2 , 10 , 25 , 21 };
int K = 2 ;
int N = arr.length;
System.out.print(mininsert(arr, K, N));
}
}
|
Python3
def mininsert(arr, K, N) :
ans = 0
for i in range (N - 1 ):
a = min (arr[i], arr[i + 1 ])
b = max (arr[i], arr[i + 1 ])
while (K * a < b) :
a * = K
ans + = 1
return ans
arr = [ 2 , 10 , 25 , 21 ]
K = 2
N = len (arr)
print (mininsert(arr, K, N))
|
C#
using System;
class GFG{
static int mininsert( int [] arr, int K, int N)
{
int ans = 0;
for ( int i = 0; i < N - 1; i++) {
int a = Math.Min(arr[i], arr[i + 1]);
int b = Math.Max(arr[i], arr[i + 1]);
while (K * a < b) {
a *= K;
ans++;
}
}
return ans;
}
public static void Main( string [] args)
{
int [] arr = { 2, 10, 25, 21 };
int K = 2;
int N = arr.Length;
Console.Write(mininsert(arr, K, N));
}
}
|
Javascript
<script>
function mininsert(arr , K , N) {
var ans = 0;
for (i = 0; i < N - 1; i++) {
var a = Math.min(arr[i], arr[i + 1]);
var b = Math.max(arr[i], arr[i + 1]);
while (K * a < b) {
a *= K;
ans++;
}
}
return ans;
}
var arr = [ 2, 10, 25, 21 ];
var K = 2;
var N = arr.length;
document.write(mininsert(arr, K, N));
</script>
|
Time Complexity: O(N * log(M)), where M is the largest element in the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...