📌  相关文章
📜  最小化删除2i -1数组元素以清空给定数组的操作

📅  最后修改于: 2021-05-04 10:25:50             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是通过在每个操作中删除2个i – 1个数组元素( i是任何正整数)来清空给定的数组。找到所需的最少操作数。

例子:

方法:可以使用贪婪技术解决问题。这样做的想法是始终从数组中删除最大可能的元素数(2 i – 1 )。请按照以下步骤解决问题:

  • 初始化一个变量,例如cntSteps ,以存储清空给定数组所需的最小操作数。
  • 删除N个数组元素会将arr []修改为0个长度的数组。因此,将N的值增加1
  • 使用变量i遍历N的每一位,并为每i位检查该位是否已设置。如果发现为真,则更新cntSteps + = 1
  • 最后,打印cntSteps的值。

下面是上述方法的实现:

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


输出:
1

时间复杂度: O(31)
辅助空间: O(1)