Longest subarray in which all elements are a factor of K | Java program to implement the above approach ; Function to find the length of the longest subarray in which all elements are a factor of K ; Stores length of the longest subarray in which all elements are a factor of K ; Stores length of the current subarray ; Traverse the array arr [ ] ; If A [ i ] is a factor of K ; Update Len ; Update MaxLen ; If A [ i ] is not a factor of K ; Update Len ; Driver Code
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 ) ) ; } }