Minimum number of operations required to make an array non-decreasing by adding 2^i to a subset in every i-th operation
Last Updated :
13 Jul, 2021
Given an array arr[] consisting of N integers, the task is to find the minimum number of operations required to make the array non-decreasing by choosing any subset of array arr[] and adding 2i to all elements of the subset in ith step.
Examples:
Input: arr[ ] = {1, 7, 6, 5}
Output: 2
Explanation:
One way to make the array non-decreasing is:
- Increment arr[1] and arr[3] by 20. Thereafter, the array modifies to {2, 7, 6, 6}.
- Increment arr[2] and arr[3] by 21. Thereafter, the array modifies to {2, 7, 8, 8}.
Therefore, two operations are needed to make the array non-decreasing. Also, it is the minimum count of operations.
Input: arr[ ] = {1, 2, 3, 4, 5}
Output: 0
Approach: The given problem can be solved based on the following observations:
Supposing A[] as the original array and B[] as the final, then:
- There will be only one way to make the final non-decreasing array, because there is no more than a single way to make a specific amount of addition to a number. Therefore, the task will be to minimize the max(B1?A1, B2?A2, …, Bn?An), as smaller differences lead to the use of shorter time to make the array non-decreasing.
- B[] is optimal when B[i] is the maximum value between B1, B2, …, B[i]?1 and A[i] because for each position i, B[i]?A[i] should be as small as possible and B[i-1] ? B[i] and A[i] ? B[i].
- If X operations are performed, then any array element can be increased by any integer in the range [0, 2X-1].
Follow the steps below to solve the problem:
- Initialize a variable, say val as 0, to store the maximum difference between the final array elements and the original array elements at the same indices.
- Initialize another variable, say mx as INT_MIN, to store the maximum of the prefix of the array.
- Traverse the array, arr[] using variable i and in each iteration update mx to max(mx, arr[i]) and val to max(val, mx – arr[i]).
- The highest power of 2, smaller than an integer, val, and then store it in a variable, say res.
- Finally, after completing the above steps, print the value of res as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countMinSteps( int arr[], int N)
{
int val = 0;
int mx = INT_MIN;
for ( int i = 0; i < N; i++) {
int curr = arr[i];
mx = max(mx, curr);
val = max(val, mx - curr);
}
long long res = 0;
while ((1LL << res) - 1 < val) {
++res;
}
return res;
}
int main()
{
int arr[] = { 1, 7, 6, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countMinSteps(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countMinSteps( int arr[], int N)
{
int val = 0 ;
int mx = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
int curr = arr[i];
mx = Math.max(mx, curr);
val = Math.max(val, mx - curr);
}
long res = 0 ;
while (( 1 << res) - 1 < val) {
++res;
}
return ( int )res;
}
public static void main(String[] args)
{
int arr[] = { 1 , 7 , 6 , 5 };
int N = arr.length;
System.out.println(countMinSteps(arr, N));
}
}
|
Python3
def countMinSteps(arr, N):
val = 0
mx = - 10 * * 9
for i in range (N):
curr = arr[i]
mx = max (mx, curr)
val = max (val, mx - curr)
res = 0
while (( 1 << res) - 1 < val):
res + = 1
return res
if __name__ = = '__main__' :
arr = [ 1 , 7 , 6 , 5 ]
N = len (arr)
print (countMinSteps(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countMinSteps( int []arr, int N)
{
int val = 0;
int mx = Int32.MinValue;
for ( int i = 0; i < N; i++) {
int curr = arr[i];
mx = Math.Max(mx, curr);
val = Math.Max(val, mx - curr);
}
int res = 0;
while ((1 << res) - 1 < val) {
++res;
}
return res;
}
public static void Main()
{
int []arr = { 1, 7, 6, 5 };
int N = arr.Length;
Console.Write(countMinSteps(arr, N));
}
}
|
Javascript
<script>
function countMinSteps(arr, N) {
let val = 0;
let mx = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < N; i++) {
let curr = arr[i];
mx = Math.max(mx, curr);
val = Math.max(val, mx - curr);
}
let res = 0;
while ((1 << res) - 1 < val) {
++res;
}
return res;
}
let arr = [1, 7, 6, 5];
let N = arr.length;
document.write(countMinSteps(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...