Longest subarray in which all elements are a factor of K
Last Updated :
21 Apr, 2021
Given an array A[] of size N and a positive integer K, the task is to find the length of the longest subarray such that all elements of the subarray is a factor of K.
Examples:
Input: A[] = {2, 8, 3, 10, 6, 7, 4, 9}, K = 60
Output: 3
Explanation: The longest subarray in which all elements are a factor of K (= 60) is {3, 10, 6}. Therefore, the required output is 3.
Input: A[] = {7, 20, 8, 10, 5}, K = 20
Output: 2
Explanation: The longest subarray in which all elements are a factor of K (= 20) is {10, 5}. Therefore, the required output is 2.
Naive Approach: This simplest approach to solve this problem is to traverse the array and generate all possible subarrays of the given array. For each subarray, check if all its elements are a factor of K or not. If found to be true, then store the length of the subarray if it the maximum obtained till then. Finally, print the maximum length obtained as the required answer.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to traverse the array and check if arr[i] is a factor of K or not. If found to be true, then increment the length of the subarray. Otherwise, store the length of the subarray if it is the maximum obtained so far and reset to 0. Follow the steps below to solve the problem:
- Initialize a variable, say MaxLen, to store the length of longest subarray such that every element in the subarray is a factor of K.
- Initialize a variable, say Len, to store the length of the current subarray.
- Traverse the array and check if K % arr[i] = 0 or not. If found to be true, then increment the value of Len and update the value of MaxLen = max(Len, MaxLen).
- Finally, print the value of MaxLen.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_longest_subarray( int A[], int N, int K)
{
int MaxLen = 0;
int Len = 0;
for ( int i = 0; i < N; i++) {
if (K % A[i] == 0) {
Len++;
MaxLen = max(MaxLen, Len);
}
else {
Len = 0;
}
}
return MaxLen;
}
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
import java.util.*;
class GFG {
static int find_longest_subarray( int [] A, int N,
int K)
{
int MaxLen = 0 ;
int Len = 0 ;
for ( int i = 0 ; i < N; i++) {
if (K % A[i] == 0 ) {
Len++;
MaxLen = Math.max(
MaxLen, Len);
}
else {
Len = 0 ;
}
}
return MaxLen;
}
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
def find_longest_subarray(A, N, K):
MaxLen = 0
Len = 0
for i in range (N):
if (K % A[i] = = 0 ):
Len + = 1
MaxLen = max (MaxLen, Len )
else :
Len = 0
return MaxLen
A = [ 2 , 8 , 3 , 10 , 6 , 7 , 4 , 9 ]
N = len (A)
K = 60
print (find_longest_subarray(A, N, K))
|
C#
using System;
class GFG{
static int find_longest_subarray( int [] A, int N,
int K)
{
int MaxLen = 0;
int Len = 0;
for ( int i = 0; i < N; i++)
{
if (K % A[i] == 0)
{
Len++;
MaxLen = Math.Max(MaxLen, Len);
}
else
{
Len = 0;
}
}
return MaxLen;
}
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));
}
}
|
Javascript
<script>
function find_longest_subarray(A, N, K)
{
let MaxLen = 0;
let Len = 0;
for (let i = 0; i < N; i++)
{
if (K % A[i] == 0)
{
Len++;
MaxLen = Math.max(MaxLen, Len);
}
else
{
Len = 0;
}
}
return MaxLen;
}
let A = [ 2, 8, 3, 10, 6, 7, 4, 9 ];
let N = A.length;
let K = 60;
document.write(find_longest_subarray(A, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...