给定一个由不同数字组成的排序数组arr [] ,任务是查找形成几何级数的最长子数组的长度。
例子:
Input: arr[]={1, 2, 4, 7, 14, 28, 56, 89}
Output: 4
Explanation:
The subarrays {1, 2, 4} and {7, 14, 28, 56} forms a GP.
Since {7, 14, 28, 56} is he longest, the required output is 4.
Input: arr[]={3, 6, 7, 12, 24, 28, 56}
Output: 2
天真的方法:解决问题的最简单方法是生成所有可能的子数组,并为每个子数组检查是否形成GP。不断更新找到的此类子阵列的最大长度。最后,打印获得的最大长度。
时间复杂度: O(N 3 )
辅助空间: O(N)
高效方法:可以通过以下步骤优化上述方法:
- 遍历数组并选择一对相邻的元素,即arr [i]和arr [i + 1] ,作为“几何级数”的前两个项。
- 如果arr [i + 1]不能被arr [i]整除,则不能考虑将其用于公共比率。否则,将arr [i + 1] / arr [i]作为当前几何级数的公共比例。
- 如果后续元素具有相同的公共比例,则增加并存储“几何级数”的长度。否则,更新等于新对相邻元素之比的公共比例。
- 最后,返回形成几何级数的最长子数组的长度作为输出。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to return the length of
// the longest subarray forming a
// GP in a sorted array
int longestGP(int A[], int N)
{
// Base Case
if (N < 2)
return N;
// Stores the length of GP
// and the common ratio
int length = 1, common_ratio = 1;
// Stores the maximum
// length of the GP
int maxlength = 1;
// Traverse the array
for (int i = 0; i < N - 1; i++) {
// Check if the common ratio
// is valid for GP
if (A[i + 1] % A[i] == 0) {
// If the current common ratio
// is equal to previous common ratio
if (A[i + 1] / A[i] == common_ratio) {
// Increment the length of the GP
length = length + 1;
// Store the max length of GP
maxlength
= max(maxlength, length);
}
// Otherwise
else {
// Update the common ratio
common_ratio = A[i + 1] / A[i];
// Update the length of GP
length = 2;
}
}
else {
// Store the max length of GP
maxlength
= max(maxlength, length);
// Update the length of GP
length = 1;
}
}
// Store the max length of GP
maxlength = max(maxlength, length);
// Return the max length of GP
return maxlength;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 1, 2, 4, 7, 14, 28, 56, 89 };
// Length of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << longestGP(arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to return the length of
// the longest subarray forming a
// GP in a sorted array
static int longestGP(int A[], int N)
{
// Base Case
if (N < 2)
return N;
// Stores the length of GP
// and the common ratio
int length = 1, common_ratio = 1;
// Stores the maximum
// length of the GP
int maxlength = 1;
// Traverse the array
for(int i = 0; i < N - 1; i++)
{
// Check if the common ratio
// is valid for GP
if (A[i + 1] % A[i] == 0)
{
// If the current common ratio
// is equal to previous common ratio
if (A[i + 1] / A[i] == common_ratio)
{
// Increment the length of the GP
length = length + 1;
// Store the max length of GP
maxlength = Math.max(maxlength, length);
}
// Otherwise
else
{
// Update the common ratio
common_ratio = A[i + 1] / A[i];
// Update the length of GP
length = 2;
}
}
else
{
// Store the max length of GP
maxlength = Math.max(maxlength, length);
// Update the length of GP
length = 1;
}
}
// Store the max length of GP
maxlength = Math.max(maxlength, length);
// Return the max length of GP
return maxlength;
}
// Driver code
public static void main (String[] args)
{
// Given array
int arr[] = { 1, 2, 4, 7, 14, 28, 56, 89 };
// Length of the array
int N = arr.length;
// Function call
System.out.println(longestGP(arr, N));
}
}
// This code is contributed by jana_sayantan
Python3
# Python3 program to implement
# the above approach
# Function to return the length of
# the longest subarray forming a
# GP in a sorted array
def longestGP(A, N):
# Base Case
if (N < 2):
return N
# Stores the length of GP
# and the common ratio
length = 1
common_ratio = 1
# Stores the maximum
# length of the GP
maxlength = 1
# Traverse the array
for i in range(N - 1):
# Check if the common ratio
# is valid for GP
if (A[i + 1] % A[i] == 0):
# If the current common ratio
# is equal to previous common ratio
if (A[i + 1] // A[i] == common_ratio):
# Increment the length of the GP
length = length + 1
# Store the max length of GP
maxlength = max(maxlength, length)
# Otherwise
else:
# Update the common ratio
common_ratio = A[i + 1] // A[i]
# Update the length of GP
length = 2
else:
# Store the max length of GP
maxlength = max(maxlength, length)
# Update the length of GP
length = 1
# Store the max length of GP
maxlength = max(maxlength, length)
# Return the max length of GP
return maxlength
# Driver Code
# Given array
arr = [ 1, 2, 4, 7, 14, 28, 56, 89 ]
# Length of the array
N = len(arr)
# Function call
print(longestGP(arr, N))
# This code is contributed by sanjoy_62
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to return the length of
// the longest subarray forming a
// GP in a sorted array
static int longestGP(int []A, int N)
{
// Base Case
if (N < 2)
return N;
// Stores the length of GP
// and the common ratio
int length = 1, common_ratio = 1;
// Stores the maximum
// length of the GP
int maxlength = 1;
// Traverse the array
for(int i = 0; i < N - 1; i++)
{
// Check if the common ratio
// is valid for GP
if (A[i + 1] % A[i] == 0)
{
// If the current common ratio
// is equal to previous common ratio
if (A[i + 1] / A[i] == common_ratio)
{
// Increment the length of the GP
length = length + 1;
// Store the max length of GP
maxlength = Math.Max(maxlength,
length);
}
// Otherwise
else
{
// Update the common ratio
common_ratio = A[i + 1] /
A[i];
// Update the length of GP
length = 2;
}
}
else
{
// Store the max length of GP
maxlength = Math.Max(maxlength,
length);
// Update the length of GP
length = 1;
}
}
// Store the max length of GP
maxlength = Math.Max(maxlength,
length);
// Return the max length of GP
return maxlength;
}
// Driver code
public static void Main(String[] args)
{
// Given array
int []arr = {1, 2, 4, 7,
14, 28, 56, 89};
// Length of the array
int N = arr.Length;
// Function call
Console.WriteLine(longestGP(arr, N));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
4
时间复杂度: O(N)
空间复杂度: O(1)