给定一个数组arr ,任务是在数组中的所有元素都是偶数时对该数组执行操作。在一个操作中,将数组中的每个整数X替换为X/2 。找出您可以执行的最大可能操作数。
例子:
Input: arr[] = {8, 12, 40}
Output: 2
Explanation: Initially, {8, 12, 40} are present in array. Since all those integers are even,
you can perform the operation. After the operation is performed once, array becomes
{4, 6, 20}. Since all those integers are again even, we can perform the operation again.
After the operation is performed twice, array becomes {2, 3, 10}. Now, there is an odd
number “3” in the array, so no operation can be performed anymore.
Thus, you can perform the operation at most twice.
Input: arr[] = {5, 6, 8, 10}
Output: 0
Explanation: Since there is an odd number 5 in the initial array, we cant perform the
operation even once.
方法:给定的问题可以通过一些简单的观察来解决:
- 如果当前数组中的所有整数都是偶数,那么我们将所有数字除以 2。
- 因此,问题简化为找到元素 arr[i] 可以被 2 除的次数。让它成为 times[i] 。答案是所有 i 的 times[i] 的最小值。
- 我们可以通过简单地保留一个变量来更新每个阶段的答案,而不是使用额外的数组 times[],这将空间复杂度降低到 O(1),因为我们没有使用任何额外的空间。
下面是上述想法的实现。
C++
// C++ code implementation for the above approach
#include
using namespace std;
// Function to return the number
// of operations possible
int arrayDivisionByTwo(int arr[], int n)
{
// counter to store the number of times the
// current element is divisible by 2
int cnt = 0;
// variable to store the final answer
int ans = INT_MAX;
for (int i = 0; i < n; i++) {
// Initialize the counter to zero
// for each element
cnt = 0;
while (arr[i] % 2 == 0) {
// update the counter till the
// number is divisible by 2
arr[i] = arr[i] / 2;
cnt++;
}
// update the answer as
// the minimum of all the counts
ans = min(ans, cnt);
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 8, 12, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << arrayDivisionByTwo(arr, n);
return 0;
}
Java
// Java code implementation for the above approach
import java.util.*;
class GFG
{
// Function to return the number
// of operations possible
static int arrayDivisionByTwo(int arr[], int n)
{
// counter to store the number of times the
// current element is divisible by 2
int cnt = 0;
// variable to store the final answer
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
// Initialize the counter to zero
// for each element
cnt = 0;
while (arr[i] % 2 == 0) {
// update the counter till the
// number is divisible by 2
arr[i] = arr[i] / 2;
cnt++;
}
// update the answer as
// the minimum of all the counts
ans = Math.min(ans, cnt);
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 8, 12, 40 };
int n = arr.length;
System.out.print(arrayDivisionByTwo(arr, n));
}
}
// This code is contributed by Princi Singh
Python3
# Python 3 code implementation for the above approach
import sys
# Function to return the number
# of operations possible
def arrayDivisionByTwo(arr, n):
# counter to store the number of times the
# current element is divisible by 2
cnt = 0
# variable to store the final answer
ans = sys.maxsize
for i in range(n):
# Initialize the counter to zero
# for each element
cnt = 0
while(arr[i] % 2 == 0):
# update the counter till the
# number is divisible by 2
arr[i] = arr[i] // 2
cnt += 1
# update the answer as
# the minimum of all the counts
ans = min(ans, cnt)
return ans
# Driver code
if __name__ == '__main__':
arr = [8, 12, 40]
n = len(arr)
print(arrayDivisionByTwo(arr, n))
# This code is contributed by ipg2016107.
Javascript
2
时间复杂度: O(32 * n)
空间复杂度: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。