Skip to content
Related Articles

Related Articles

Minimize operations of removing 2i -1 array elements to empty given array
  • Last Updated : 29 Jan, 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:
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:
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 elements
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)) {
 
            // Update cntStep
            cntStep += 1;
        }
    }
 
    return cntStep;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << minimumStepReqArr(arr, N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to find minimum count of steps
# required to remove all the array elements
def 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 Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3 ]
    N = len(arr)
     
    print(minimumStepReqArr(arr, N))
 
# This code is contributed by SURENDRA_GANGWAR

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using 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

chevron_right


Output: 

1

 

Time Complexity: O(31)
Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :