给定一个由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)