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++
// 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;
}
Java
// 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
Python3
# 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
C#
// 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
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>
Time Complexity: O(31)
Auxiliary Space: O(1)
Similar Reads
Minimize the non-zero elements in the Array by given operation Given an array arr[] of length N, the task is to minimize the count of the number of non-zero elements by adding the value of the current element to any of its adjacent element and subtracting from the current element at most once.Examples: Input: arr[] = { 1, 0, 1, 0, 0, 1 } Output: 2 Explanation:
5 min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Minimize remaining array sizes by removing equal pairs of first array elements Given two binary arrays L1[] and L2[], each of size N, the task is to minimize the count of remaining array elements after performing the following operations: If the first element of both the arrays is same, then remove it from both the arrays.Otherwise, remove the first element from L1[] and appen
8 min read
Minimum operations to make all Array elements 0 by MEX replacement Given an array of N integers. You can perform an operation that selects a contiguous subarray and replaces all its elements with the MEX (smallest non-negative integer that does not appear in that subarray), the task is to find the minimum number of operations required to make all the elements of th
5 min read
Minimize operations to convert Array elements to 0s Given an array nums[] of length N containing non-negative integers, the task is to convert all elements from index 0 to N-2 to zero, after doing the below operations minimum number of times. In one operation select two indices i and j such that i < j and all the elements between i and j has to be
6 min read
Minimum possible sum of array elements after performing the given operation Given an array arr[] of size N and a number X. If any sub array of the array(possibly empty) arr[i], arr[i+1], ... can be replaced with arr[i]/x, arr[i+1]/x, .... The task is to find the minimum possible sum of the array which can be obtained. Note: The given operation can only be performed once.Exa
9 min read