给定大小为N的数组arr [] ,任务是通过在每个操作中删除2个i – 1个数组元素( i是任何正整数)来清空给定的数组。找到所需的最少操作数。
例子:
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.
方法:可以使用贪婪技术解决问题。这样做的想法是始终从数组中删除最大可能的元素数(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)