📜  形成几何级进 (GP) 的最长子阵列

📅  最后修改于: 2021-09-06 06:27:15             🧑  作者: Mango

给定一个由不同数字组成的排序数组arr[] ,任务是找到形成几何级数的最长子数组的长度。

例子:

朴素方法:解决问题的最简单方法是生成所有可能的子数组,并为每个子数组检查它是否形成 GP。不断更新找到的此类子数组的最大长度。最后,打印得到的最大长度。
时间复杂度: O(N 3 )
辅助空间: O(N)

高效的方法:上述方法可以通过以下步骤进行优化:

  • 遍历数组并选择一对相邻元素,即arr[i]arr[i+1] ,作为几何级数的前两项。
  • 如果arr[i+1]不能被arr[i]整除,则不能将其视为公比。否则,取arr[i+1] / arr[i]作为当前 Geometric Progression 的公比。
  • 如果后续元素具有相同的公比,则增加并存储几何级数的长度。否则,更新公共比率等于新的相邻元素对的比率。
  • 最后,返回形成几何级数的最长子数组的长度作为输出。

下面是上述方法的实现:

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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live