Minimize operations of removing 2i -1 array elements to empty given array
Last Updated :
21 Apr, 2021
Given an array arr[] of size N, the task is to empty given array by removing 2i – 1 array elements in each operation (i is any positive integer). Find the minimum number of operations required.
Examples:
Input: arr[] = { 2, 3, 4 }
Output: 1
Explanation:
Removing (22 – 1) elements i.e { arr[0], arr[1], arr[2] } modifies arr[] to { }
Since no elements left in the array therefore, the required output is 1.
Input: arr[] = { 1, 2, 3, 4 }
Output: 2
Explanation:
Removing (21 – 1) element i.e, { arr[0] } modifies arr[] to { 2, 3, 4 }
Removing (22 – 1) elements i.e, { arr[0], arr[1], arr[2] } modifies arr[] to { }
Since no elements left in the array therefore, the required output is 2.
Approach: The problem can be solved using Greedy technique. The idea is to always remove the maximum possible count(2i – 1) of elements from the array. Follow the steps below to solve the problem:
- Initialize a variable, say cntSteps, to store the minimum count of operations required to empty given array.
- Removing N array elements modifies arr[] to 0 length array. Therefore, increment the value of N by 1.
- Traverse each bit of N using variable i and for every ith bit, check if the bit is set or not. If found to be true, then update cntSteps += 1
- Finally, print the value of cntSteps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumStepReqArr( int arr[], int N)
{
int cntStep = 0;
N += 1;
for ( int i = 31; i >= 0; i--) {
if (N & (1 << i)) {
cntStep += 1;
}
}
return cntStep;
}
int main()
{
int arr[] = { 1, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimumStepReqArr(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minimumStepReqArr( int [] arr, int N)
{
int cntStep = 0 ;
N += 1 ;
for ( int i = 31 ; i >= 0 ; i--)
{
if ((N & ( 1 << i)) != 0 )
{
cntStep += 1 ;
}
}
return cntStep;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 };
int N = arr.length;
System.out.println(minimumStepReqArr(arr, N));
}
}
|
Python3
def minimumStepReqArr(arr, N):
cntStep = 0
N + = 1
i = 31
while (i > = 0 ):
if (N & ( 1 << i)):
cntStep + = 1
i - = 1
return cntStep
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ]
N = len (arr)
print (minimumStepReqArr(arr, N))
|
C#
using System;
class GFG
{
static int minimumStepReqArr( int [] arr, int N)
{
int cntStep = 0;
N += 1;
for ( int i = 31; i >= 0; i--)
{
if ((N & (1 << i)) != 0)
{
cntStep += 1;
}
}
return cntStep;
}
static void Main()
{
int [] arr = { 1, 2, 3 };
int N = arr.Length;
Console.WriteLine(minimumStepReqArr(arr, N));
}
}
|
Javascript
<script>
function minimumStepReqArr(arr, N)
{
let cntStep = 0;
N += 1;
for (let i = 31; i >= 0; i--)
{
if ((N & (1 << i)) != 0)
{
cntStep += 1;
}
}
return cntStep;
}
let arr = [ 1, 2, 3 ];
let N = arr.length;
document.write(minimumStepReqArr(arr, N));
</script>
|
Time Complexity: O(31)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...