📌  相关文章
📜  所有元素均为K因子的最长子数组

📅  最后修改于: 2021-04-22 00:23:39             🧑  作者: Mango

给定大小为N且正整数K的数组A [] ,任务是找到最长子数组的长度,以使子数组所有元素都是K的因子。

例子:

天真的方法:解决此问题的最简单方法是遍历数组并生成给定数组的所有可能的子数组。对于每个子数组,检查其所有元素是否都是K的因子。如果发现为真,则存储子数组的长度(如果它是到那时为止获得的最大值)。最后,打印获得的最大长度作为所需答案。

时间复杂度: O(N 3 )
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是遍历数组并检查arr [i]是否为K的因数。如果发现为真,则增加子数组的长度。否则,存储子数组的长度(如果它是目前为止获得的最大值),并将其重置为0 。请按照以下步骤解决问题:

  • 初始化一个变量,例如MaxLen ,以存储最长子数组的长度,以使子数组中的每个元素都是K的因子。
  • 初始化一个变量,例如Len ,以存储当前子数组的长度。
  • 遍历数组,并检查K%arr [i]是否为0 。如果确定为真,则增加Len的值并更新MaxLen = max(Len,MaxLen)的值。
  • 最后,打印MaxLen的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the length of the longest
// subarray in which all elements are a factor of K
int find_longest_subarray(int A[], int N, int K)
{
 
    // Stores length of the longest subarray
    // in which all elements are a factor of K
    int MaxLen = 0;
 
    // Stores length of
    // the current subarray
    int Len = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If A[i] is a factor of K
        if (K % A[i] == 0) {
 
            // Update Len
            Len++;
 
            // Update MaxLen
            MaxLen = max(MaxLen, Len);
        }
 
        else {
            // Reset Len
            Len = 0;
        }
    }
 
    return MaxLen;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 8, 3, 10, 6, 7, 4, 9 };
    int N = sizeof(A) / sizeof(A[0]);
    ;
    int K = 60;
 
    cout << find_longest_subarray(A, N, K);
    return 0;
}


Java
// Java program to implement
// the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to find the length of the longest
    // subarray in which all elements are a factor of K
    static int find_longest_subarray(int[] A, int N,
                                     int K)
    {
        // Stores length of the longest subarray
        // in which all elements are a factor of K
        int MaxLen = 0;
 
        // Stores length of
        // the current subarray
        int Len = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < N; i++) {
 
            // If A[i] is a
            // factor of K
            if (K % A[i] == 0) {
 
                // Update Len
                Len++;
 
                // Update MaxLen
                MaxLen = Math.max(
                    MaxLen, Len);
            }
 
            // If A[i] is not a
            // factor of K
            else {
 
                // Update Len
                Len = 0;
            }
        }
 
        return MaxLen;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] A = { 2, 8, 3, 10, 6, 7, 4, 9 };
        int N = A.length;
        int K = 60;
        System.out.println(
            find_longest_subarray(A, N, K));
    }
}


Python3
# Python3 program to implement
# the above approach
  
# Function to find the length of the
# longest subarray in which all
# elements are a factor of K
def find_longest_subarray(A, N, K):
     
    # Stores length of the longest
    # subarray in which all elements
    # are a factor of K
    MaxLen = 0
  
    # Stores length of the
    # current subarray
    Len = 0
  
    # Traverse the array arr[]
    for i in range(N):
         
        # If A[i] is a factor of K
        if (K % A[i] == 0):
  
            # Update Len
            Len += 1
  
            # Update MaxLen
            MaxLen = max(MaxLen, Len)
             
        # If A[i] is not a
        # factor of K  
        else:
             
            # Reset Len
            Len = 0
             
    return MaxLen
 
# Driver Code
A = [ 2, 8, 3, 10, 6, 7, 4, 9 ]
N = len(A)
     
K = 60
  
print(find_longest_subarray(A, N, K))
  
# This code is contributed by susmitakundugoaldanga


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to find the length of the longest
// subarray in which all elements are a factor of K
static int find_longest_subarray(int[] A, int N,
                                 int K)
{
     
    // Stores length of the longest subarray
    // in which all elements are a factor of K
    int MaxLen = 0;
 
    // Stores length of
    // the current subarray
    int Len = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If A[i] is a
        // factor of K
        if (K % A[i] == 0)
        {
             
            // Update Len
            Len++;
 
            // Update MaxLen
            MaxLen = Math.Max(MaxLen, Len);
        }
 
        // If A[i] is not a
        // factor of K
        else
        {
             
            // Update Len
            Len = 0;
        }
    }
    return MaxLen;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] A = { 2, 8, 3, 10, 6, 7, 4, 9 };
    int N = A.Length;
    int K = 60;
     
    Console.WriteLine(find_longest_subarray(
        A, N, K));
}
}
 
// This code is contributed by chitranayal


输出:
3










时间复杂度: O(N)
辅助空间: O(1)