Open In App

Modify Array such that no element is smaller/greater than half/double of its adjacent elements

Last Updated : 17 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, arr[] of size N. Find the minimum number of elements we need to insert between array elements such that the maximum element from two adjacent elements is not more than twice bigger than the minimum element i.e., max(arr[i], arr[i+1]) ? 2 * min(arr[i], arr[i+1]) where 0 ? i < N – 1.

Examples:

Input: N = 5, A[] = {1, 2, 3, 4, 3}
Output: 0
Explanation:

  • Element 1 whose value is 1 and neighbour value is 2. max(arr[i], arr[i+1]) ? 2 * (min(arr[i], arr[i+1])) is true, no need to insert anything
  • Element 2 whose value is 2 and neighbour value is 3. max(arr[i], arr[i+1]) ? 2 * (min(arr[i], arr[i+1])) is true, no need to insert anything
  • Element 3 whose value is 3 and neighbour value is 4. max(arr[i], arr[i+1]) ? 2 * (min(arr[i], arr[i+1])) is true, no need to insert anything
  • Element 4 whose value is 4 and neighbour value is 3. max(arr[i], arr[i+1]) ? 2 * (min(arr[i], arr[i+1])) is true, no need to insert anything

Therefore, the minimum number of insertions required is 0.

Input: N = 4, A[] = {4, 2, 10, 1}
Output: 5
Explanation:

  • Element 1 whose value is 4 and neighbour value is 2,   max(arr[i], arr[i+1]) ? 2 * (min(arr[i], arr[i+1])) is true, no need to insert anything.
  • Element 2 whose value is 2 and neighbour value is 10, max(arr[i], arr[i+1]) ? 2 * (min(arr[i], arr[i+1])) is not true, so we can insert 5, 3.
  • Element 3 whose value is 10 and neighbour value is 1,   max(arr[i], arr[i+1]) ? 2 * (min(arr[i], arr[i+1])) is not true, so we can insert 5, 3, 2

Therefore, the minimum number of insertions required is 0 + 2 + 3 = 5.

Approach: This problem can be solved based on the following idea:

Iterate from the start of the array till the second last element. Inside the loop check, if the Ratio of the maximum element and minimum element between adjacent elements is fulfilling the ideal condition (i.e. ? 2), till it is not true try to insert a maximum value that we can insert (i.e. ceil(maxEle/2), ceil will found upper bound if the result is in decimal) and we will also increase the counter by 1 every time.

Follow the steps below to solve the problem:

  • Initialize the count = 0, to count the minimum number of insertions required.
  • Iterate over the array A[].
  • If maxEle / minEle > 2, update the maxEle = ceil(maxEle / 2) and increment the count of insertion.
  • After iterating, return the count.

Below is the implementation of the above approach:

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum
// insertion required
int insertion(int A[], int n)
{
 
    int count = 0;
 
    // Create float variable as ratio
    // can be in decimal value
    for (int i = 0; i < n - 1; ++i) {
 
        float minEle = min(A[i], A[i + 1]);
        float maxEle = max(A[i], A[i + 1]);
 
        // Maximum and minimum from
        // adjacent values
        while (maxEle / minEle > 2.0) {
 
            // This loop will stop when
            // ratio between maxi/mini <= 2
            maxEle = ceil(maxEle / 2);
 
            // Decrease maxi and see if we
            // can insert that element in
            // array or not
            count++;
 
            // Increase insertion
            // counter by 1
        }
    }
 
    // Return the minimum
    // insertion required
    return count;
}
 
// Driver code
int main()
{
    int n = 4;
    int A[] = { 4, 2, 10, 1 };
 
    // Function Call
    cout << insertion(A, n);
 
    return 0;
}


Java




// Java implementation of above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
  // Function to find minimum
  // insertion required
  public static int insertion(int[] A, int n)
  {
 
    int count = 0;
 
    // Create float variable as ratio
    // can be in decimal value
    for (int i = 0; i < n - 1; ++i) {
 
      float minEle = Math.min(A[i], A[i + 1]);
      float maxEle = Math.max(A[i], A[i + 1]);
 
      // Maximum and minimum from
      // adjacent values
      while (maxEle / minEle > 2.0) {
 
        // This loop will stop when
        // ratio between maxi/mini <= 2
        maxEle = (float)Math.ceil(maxEle / 2);
 
        // Decrease maxi and see if we
        // can insert that element in
        // array or not
        count++;
 
        // Increase insertion
        // counter by 1
      }
    }
 
    // Return the minimum
    // insertion required
    return count;
  }
 
  public static void main(String[] args)
  {
    int n = 4;
    int[] A = { 4, 2, 10, 1 };
 
    // Function Call
    System.out.println(insertion(A, n));
  }
}
 
// This code is contributed by lokesh.


Python3




# Python code for the above approach
import math
 
def insertion(A, n):
    # Initialize counter to keep track
    # of the number of insertions
    count = 0
 
    # Iterate through the elements of the array
    for i in range(n - 1):
        # Find the minimum and maximum of
        # the current pair of adjacent elements
        minEle = min(A[i], A[i + 1])
        maxEle = max(A[i], A[i + 1])
 
        # Divide maxEle by 2 until the ratio between maxEle and minEle is <= 2
        while maxEle / minEle > 2:
            maxEle = math.ceil(maxEle / 2)
            # Increment counter for each insertion
            count += 1
 
    # Return the number of insertions required
    return count
 
# Driver code
n = 4
A = [4, 2, 10, 1]
print(insertion(A, n))
 
# This code is contributed by Potta Lokesh


C#




// C# code for the above approach:
using System;
using System.Collections.Generic;
 
public class Gfg
{
 
  // Function to find minimum
  // insertion required
  static int insertion(int[] A, int n)
  {
 
    int count = 0;
 
    // Create float variable as ratio
    // can be in decimal value
    for (int i = 0; i < n - 1; ++i) {
 
      float minEle = Math.Min(A[i], A[i + 1]);
      float maxEle = Math.Max(A[i], A[i + 1]);
 
      // Maximum and minimum from
      // adjacent values
      while (maxEle / minEle > 2.0) {
 
        // This loop will stop when
        // ratio between maxi/mini <= 2
        maxEle = Convert.ToSingle(Math.Ceiling(maxEle / 2));
 
        // Decrease maxi and see if we
        // can insert that element in
        // array or not
        count++;
 
        // Increase insertion
        // counter by 1
      }
    }
 
    // Return the minimum
    // insertion required
    return count;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int n = 4;
    int[] A = { 4, 2, 10, 1 };
 
    // Function Call
    Console.WriteLine(insertion(A, n));
  }
}
 
// This code is contributed by ritaagarwal.


Javascript




// Javascript implementation of above approach
 
// Function to find minimum
// insertion required
function insertion(A, n)
{
 
    let count = 0;
 
    // Create float variable as ratio
    // can be in decimal value
    for (let i = 0; i < n - 1; ++i) {
 
        let minEle = Math.min(A[i], A[i + 1]);
        let maxEle = Math.max(A[i], A[i + 1]);
 
        // Maximum and minimum from
        // adjacent values
        while (maxEle / minEle > 2.0) {
 
            // This loop will stop when
            // ratio between maxi/mini <= 2
            maxEle = Math.ceil(maxEle / 2);
 
            // Decrease maxi and see if we
            // can insert that element in
            // array or not
            count++;
 
            // Increase insertion
            // counter by 1
        }
    }
 
    // Return the minimum
    // insertion required
    return count;
}
 
// Driver code
let n = 4;
let A = [4, 2, 10, 1 ];
 
// Function Call
console.log(insertion(A, n));
 
// This code is contributed by poojaagarwal2.


Output

5

Time Complexity: O(N*log(H)), where N is the length of the array and H is the value of the maximum array element
Auxiliary Space: O(1)

Related Articles:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads