给定一个大小为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
Javascript
1
时间复杂度: O(31)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。