Modify Array such that no element is smaller/greater than half/double of its adjacent elements
Last Updated :
17 Jan, 2023
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.
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:
Similar Reads
Rearrange array such that even index elements are smaller and odd index elements are greater Given an array, rearrange the array such that : If index i is even, arr[i] <= arr[i+1]If index i is odd, arr[i] >= arr[i+1] Note: There can be multiple answers. Examples: Input : arr[] = {2, 3, 4, 5} Output : arr[] = {2, 4, 3, 5} Explanation : Elements at even indexes are smaller and elements
10 min read
Rearrange the Array to maximize the elements which is smaller than both its adjacent elements Given an array arr[] consisting of N distinct integers, the task is to rearrange the array elements such that the count of elements that are smaller than their adjacent elements is maximum. Note: The elements left of index 0 and right of index N-1 are considered as -INF. Examples: Input: arr[] = {1,
7 min read
Rearrange an array such that every odd indexed element is greater than it previous Given an unsorted array, rearrange the array such that the number at the odd index is greater than the number at the previous even index. There may be multiple outputs, we need to print one of them. Note: Indexing is based on an array, so it always starts from 0. Examples: Input : arr[] = {5, 2, 3,
7 min read
Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements Given an array of N integers, the task is to arrange them in a circular arrangement in such a way that the element is strictly less than the sum of its adjacent elements. In case such an arrangement is not possible, then print -1. Note that there can be multiple ways of arranging the elements such t
8 min read
Minimize length of an array by repeatedly removing elements that are smaller than the next element Given an array arr[] consisting of N integers, the task is to repeatedly remove elements that are smaller than the next element. Examples: Input: arr[] = {20, 10, 25, 30, 40}Output: 40Explanation:Below are the operations that are performed: Current array: arr[] = {20, 10, 25, 30, 40} Currently, arr[
15 min read