4

Modify Array such that no element is smaller/greater than half/double of its adj...

 2 weeks ago
source link: https://www.geeksforgeeks.org/modify-array-such-that-no-element-is-smaller-greater-than-half-double-of-its-adjacent-elements/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:

  • Python3
  • Javascript
// 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;
}
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:

"The DSA course helped me a lot in clearing the interview rounds. It was really very helpful in setting a strong foundation for my problem-solving skills. Really a great investment, the passion Sandeep sir has towards DSA/teaching is what made the huge difference." - Gaurav | Placed at Amazon

Before you move on to the world of development, master the fundamentals of DSA on which every advanced algorithm is built upon. Choose your preferred language and start learning today: 

DSA In JAVA/C++
DSA In Python
DSA In JavaScript
Trusted by Millions, Taught by One- Join the best DSA Course Today!

Recommended Problems
Frequently asked DSA Problems
Like Article
Suggest improvement
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK