📜  具有最大GCD的最长子阵列

📅  最后修改于: 2021-05-04 21:25:06             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是找到具有最大可能GCD值的最长子数组的长度。

例子:

天真的方法:生成所有可能的子阵列,并分别找到每个子阵列的GCD,以找到最长的子阵列。该方法将花费O(N 3 )时间来解决该问题。

更好的方法:最大GCD值将始终等于阵列中存在的最大数字。假设数组中存在的最大数字是X。现在,任务是找到具有所有X的最大子数组。使用两指针方法可以完成相同的操作。下面是算法:

  • 在数组中找到最大的数字。让我们将此数字称为X。
  • i = 0开始运行循环
    • 如果arr [i]!= X,则增加i并继续。
    • 否则初始化j = i
    • j arr [j] = X时,递增j
    • 将答案更新为ans = max(ans,j – i)
    • 更新我= j的

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the length of
// the largest subarray with
// maximum possible GCD
int findLength(int* arr, int n)
{
    // To store the maximum number
    // present in the array
    int x = 0;
  
    // Finding the maximum element
    for (int i = 0; i < n; i++)
        x = max(x, arr[i]);
  
    // To store the final answer
    int ans = 0;
  
    // Two pointer
    for (int i = 0; i < n; i++) {
  
        if (arr[i] != x)
            continue;
  
        // Running a loop from j = i
        int j = i;
  
        // Condition for incrementing 'j'
        while (arr[j] == x)
            j++;
  
        // Updating the answer
        ans = max(ans, j - i);
    }
  
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 2 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findLength(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
  
    // Function to return the length of 
    // the largest subarray with 
    // maximum possible GCD 
    static int findLength(int []arr, int n) 
    { 
        // To store the maximum number 
        // present in the array 
        int x = 0; 
      
        // Finding the maximum element 
        for (int i = 0; i < n; i++) 
            x = Math.max(x, arr[i]); 
      
        // To store the final answer 
        int ans = 0; 
      
        // Two pointer 
        for (int i = 0; i < n; i++) 
        { 
            if (arr[i] != x) 
                continue; 
      
            // Running a loop from j = i 
            int j = i; 
      
            // Condition for incrementing 'j' 
            while (arr[j] == x) 
            {
                j++; 
                if (j >= n )
                break;
            }
      
            // Updating the answer 
            ans = Math.max(ans, j - i); 
        } 
        return ans; 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int arr[] = { 1, 2, 2 }; 
        int n = arr.length; 
      
        System.out.println(findLength(arr, n)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach 
  
# Function to return the length of 
# the largest subarray with 
# maximum possible GCD 
def findLength(arr, n) :
  
    # To store the maximum number 
    # present in the array 
    x = 0; 
  
    # Finding the maximum element 
    for i in range(n) : 
        x = max(x, arr[i]); 
  
    # To store the final answer 
    ans = 0; 
  
    # Two pointer 
    for i in range(n) :
  
        if (arr[i] != x) :
            continue; 
  
        # Running a loop from j = i 
        j = i; 
  
        # Condition for incrementing 'j' 
        while (arr[j] == x) :
            j += 1; 
              
            if j >= n :
                break
  
        # Updating the answer 
        ans = max(ans, j - i); 
  
    return ans; 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 1, 2, 2 ]; 
    n = len(arr); 
  
    print(findLength(arr, n)); 
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach 
using System;
  
class GFG 
{
  
    // Function to return the length of 
    // the largest subarray with 
    // maximum possible GCD 
    static int findLength(int []arr, int n) 
    { 
        // To store the maximum number 
        // present in the array 
        int x = 0; 
      
        // Finding the maximum element 
        for (int i = 0; i < n; i++) 
            x = Math.Max(x, arr[i]); 
      
        // To store the final answer 
        int ans = 0; 
      
        // Two pointer 
        for (int i = 0; i < n; i++) 
        { 
            if (arr[i] != x) 
                continue; 
      
            // Running a loop from j = i 
            int j = i; 
      
            // Condition for incrementing 'j' 
            while (arr[j] == x) 
            {
                j++; 
                if (j >= n )
                break;
            }
      
            // Updating the answer 
            ans = Math.Max(ans, j - i); 
        } 
        return ans; 
    } 
      
    // Driver code 
    public static void Main ()
    { 
        int []arr = { 1, 2, 2 }; 
        int n = arr.Length; 
      
        Console.WriteLine(findLength(arr, n)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
2