Minimize operations of removing 2i -1 array elements to empty given array
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to find minimum count of steps// required to remove all the array elementsint minimumStepReqArr(int arr[], int N){ // Stores minimum count of steps required // to remove all the array elements int cntStep = 0; // Update N N += 1; // Traverse each bit of N for (int i = 31; i >= 0; i--) { // If current bit is set if (N & (1 << i)) { // Update cntStep cntStep += 1; } } return cntStep;}// Driver Codeint main(){ int arr[] = { 1, 2, 3 }; int N = sizeof(arr) / sizeof(arr[0]); cout << minimumStepReqArr(arr, N); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{ // Function to find minimum count of steps // required to remove all the array elements static int minimumStepReqArr(int[] arr, int N) { // Stores minimum count of steps required // to remove all the array elements int cntStep = 0; // Update N N += 1; // Traverse each bit of N for (int i = 31; i >= 0; i--) { // If current bit is set if ((N & (1 << i)) != 0) { // Update cntStep cntStep += 1; } } return cntStep; } // Driver code public static void main(String[] args) { int[] arr = { 1, 2, 3 }; int N = arr.length; System.out.println(minimumStepReqArr(arr, N)); }}// This code is contributed by susmitakundugoaldanga |
Python3
# Python3 program to implement# the above approach# Function to find minimum count of steps# required to remove all the array elementsdef minimumStepReqArr(arr, N): # Stores minimum count of steps required # to remove all the array elements cntStep = 0 # Update N N += 1 i = 31 while(i >= 0): # If current bit is set if (N & (1 << i)): # Update cntStep cntStep += 1 i -= 1 return cntStep# Driver Codeif __name__ == '__main__': arr = [ 1, 2, 3 ] N = len(arr) print(minimumStepReqArr(arr, N))# This code is contributed by SURENDRA_GANGWAR |
C#
// C# program to implement// the above approachusing System;class GFG{ // Function to find minimum count of steps // required to remove all the array elements static int minimumStepReqArr(int[] arr, int N) { // Stores minimum count of steps required // to remove all the array elements int cntStep = 0; // Update N N += 1; // Traverse each bit of N for (int i = 31; i >= 0; i--) { // If current bit is set if ((N & (1 << i)) != 0) { // Update cntStep cntStep += 1; } } return cntStep; } // Driver code static void Main() { int[] arr = { 1, 2, 3 }; int N = arr.Length; Console.WriteLine(minimumStepReqArr(arr, N)); }}// This code is contributed by divyesh072019 |
Javascript
<script> // Javascript program to implement the above approach // Function to find minimum count of steps // required to remove all the array elements function minimumStepReqArr(arr, N) { // Stores minimum count of steps required // to remove all the array elements let cntStep = 0; // Update N N += 1; // Traverse each bit of N for (let i = 31; i >= 0; i--) { // If current bit is set if ((N & (1 << i)) != 0) { // Update cntStep cntStep += 1; } } return cntStep; } let arr = [ 1, 2, 3 ]; let N = arr.length; document.write(minimumStepReqArr(arr, N));// This code is contributed by suresh07.</script> |
1
Time Complexity: O(31)
Auxiliary Space: O(1)


