给定一个由N 个正整数组成的数组arr[] ,任务是找到最长子数组的长度,该子数组的元素乘积等于 2 的完美幂。
例子:
Input: arr[] = {2, 5, 4, 4, 6}
Output: 2
Explanation:
The subarray having maximum length whose product is perfect power of 2 is {4, 4}.
Product = 4 * 4 = 16, which is a perfect power of 2.
Input: arr[] = {2, 5, 4, 6, 8, 8, 2}
Output: 3
Explanation:
The subarray having maximum length whose product is perfect power of 2 is {8, 8, 2}.
Product = 8 * 8 * 2 = 128, which is a perfect power of 2.
朴素方法:最简单的方法是生成给定数组的所有可能子数组,并检查任何子数组元素的乘积是否为 2的完美幂。如果发现为真,则将最大长度更新为该子数组的长度。最后,打印检查所有可能的子数组后得到的子数组的最大长度。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是基于这样一个事实,即对于任何 2 的幂的数,它也可以表示为具有 2 的完全幂的数的乘积。因此,该思想是使用Kadane 算法以找到所有元素都具有2 的完美幂的子阵列的最大长度。以下是步骤:
- 将maxLength和ans初始化为0 ,这将分别存储子数组的最大长度和子数组的结果最大长度。
- 遍历给定数组并执行以下操作:
- 如果当前元素是 2 的完美幂,则将maxLength增加1 。否则将maxLength更新为0 。
- 更新ANS上述步骤后的最大ANS和最大长度的。
- 经过上述步骤后,打印ans的值作为最大长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check whether a number
// is power of 2 or not
bool isPower(int x)
{
return (x && (!(x & (x - 1))));
}
// Function to find maximum length
// subarray having product of element
// as a perfect power of 2
int maximumlength(int arr[], int N)
{
// Stores current subarray length
int max_length = 0;
// Stores maximum subarray length
int max_len_subarray = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// If arr[i] is power of 2
if (isPower(arr[i]) == 1) {
// Increment max_length
max_length++;
// Update max_len_subarray
max_len_subarray
= max(max_length,
max_len_subarray);
}
// Otherwise
else {
max_length = 0;
}
}
// Print the maximum length
cout << max_len_subarray;
}
// Driver Code
int main()
{
// Given arr[]
int arr[] = { 2, 5, 4, 6, 8, 8, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
maximumlength(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check whether a number
// is power of 2 or not
public static boolean isPower(int x)
{
if (x != 0 && (x & (x - 1)) == 0)
return true;
return false;
}
// Function to find maximum length
// subarray having product of element
// as a perfect power of 2
public static void maximumlength(int arr[],
int N)
{
// Stores current subarray length
int max_length = 0;
// Stores maximum subarray length
int max_len_subarray = 0;
// Traverse the given array
for(int i = 0; i < N; i++)
{
// If arr[i] is power of 2
if (isPower(arr[i]))
{
// Increment max_length
max_length++;
// Update max_len_subarray
max_len_subarray = Math.max(max_length,
max_len_subarray);
}
// Otherwise
else
{
max_length = 0;
}
}
// Print the maximum length
System.out.println(max_len_subarray);
}
// Driver Code
public static void main(String args[])
{
// Given arr[]
int arr[] = { 2, 5, 4, 6, 8, 8, 2 };
int N = arr.length;
// Function Call
maximumlength(arr, N);
}
}
// This code is contributed by hemanthswarna1506
Python3
# Python3 program for the
# above approach
# Function to check whether
# a number is power of 2
# or not
def isPower(x):
if (x != 0 and
(x & (x - 1)) == 0):
return True;
return False;
# Function to find maximum
# length subarray having
# product of element
# as a perfect power of 2
def maximumlength(arr, N):
# Stores current
# subarray length
max_length = 0;
# Stores maximum
# subarray length
max_len_subarray = 0;
# Traverse the given array
for i in range(N):
# If arr[i] is power of 2
if (isPower(arr[i])):
# Increment max_length
max_length += 1;
# Update max_len_subarray
max_len_subarray = max(max_length,
max_len_subarray);
# Otherwise
else:
max_length = 0;
# Prthe maximum length
print(max_len_subarray);
# Driver Code
if __name__ == '__main__':
# Given arr
arr = [2, 5, 4,
6, 8, 8, 2];
N = len(arr);
# Function Call
maximumlength(arr, N);
# This code is contributed by gauravrajput1
C#
// C# program for the above approach
using System;
class GFG{
// Function to check whether a number
// is power of 2 or not
public static bool isPower(int x)
{
if (x != 0 && (x & (x - 1)) == 0)
return true;
return false;
}
// Function to find maximum length
// subarray having product of element
// as a perfect power of 2
public static void maximumlength(int[] arr,
int N)
{
// Stores current subarray length
int max_length = 0;
// Stores maximum subarray length
int max_len_subarray = 0;
// Traverse the given array
for(int i = 0; i < N; i++)
{
// If arr[i] is power of 2
if (isPower(arr[i]))
{
// Increment max_length
max_length++;
// Update max_len_subarray
max_len_subarray = Math.Max(max_length,
max_len_subarray);
}
// Otherwise
else
{
max_length = 0;
}
}
// Print the maximum length
Console.WriteLine(max_len_subarray);
}
// Driver Code
public static void Main()
{
// Given arr[]
int[] arr = { 2, 5, 4, 6, 8, 8, 2 };
int N = arr.Length;
// Function Call
maximumlength(arr, N);
}
}
// This code is contributed by sanjoy_62
3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live