# Longest subarray with elements divisible by k

Suppose you a given an array. You have to find the length of the longest subarray such that each and every element of it is divisible by k.

Examples:

```Input : arr[] = { 1, 7, 2, 6, 8, 100, 3, 6, 16}, k=2
Output : 4

Input : arr[] = { 3, 11, 22, 32, 55, 100, 1, 5}, k=5
Output : 2
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Initialize two variables current_count and max_count with value 0;
• Iterate the array from left to right and check the divisibility of each element by k.
• If the element is divisible, then increment current_count, otherwise make current_count equal to 0
• Compare current_count with max_count at each element, if current_count is greater than max_count, assign the value of current_count to max_count
• Finally, output value of max_count

Below is the implementation of above approach:

## C++

 `// C++ program of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// function to find longest subarray ` `int` `longestsubarray(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `current_count = 0; ` `    ``// this will contain length of longest subarray found ` `    ``int` `max_count = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(arr[i] % k == 0) ` `            ``current_count++; ` `        ``else` `            ``current_count = 0; ` `        ``max_count = max(current_count, max_count); ` `    ``} ` `    ``return` `max_count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 5, 11, 32, 64, 88 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `k = 8; ` `    ``cout << longestsubarray(arr, n, k); ` `    ``return` `0; ` `} `

## Java

 `//Java program of above approach ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `    ``// function to find longest subarray ` `static` `int` `longestsubarray(``int` `arr[],  ` `                        ``int` `n, ``int` `k) ` `{ ` `    ``int` `current_count = ``0``; ` `     `  `    ``// this will contain length of  ` `    ``// longest subarray found ` `    ``int` `max_count = ``0``; ` ` `  `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` `        ``if` `(arr[i] % k == ``0``) ` `            ``current_count++; ` `        ``else` `            ``current_count = ``0``; ` `        ``max_count = Math.max(current_count,  ` `                            ``max_count); ` `    ``} ` `    ``return` `max_count; ` `} ` ` `  `// Driver code ` `    ``public` `static` `void` `main (String[] args) { ` `int` `arr[] = { ``2``, ``5``, ``11``, ``32``, ``64``, ``88` `}; ` `    ``int` `n = arr.length; ` `    ``int` `k = ``8``; ` `    ``System.out.println(longestsubarray(arr, n, k)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by ajit `

## Python3

 `# Python 3 program of above approach ` ` `  `# function to find longest subarray ` `def` `longestsubarray(arr, n, k): ` `    ``current_count ``=` `0` `     `  `    ``# this will contain length of  ` `    ``# longest subarray found ` `    ``max_count ``=` `0` ` `  `    ``for` `i ``in` `range``(``0``, n, ``1``): ` `        ``if` `(arr[i] ``%` `k ``=``=` `0``): ` `            ``current_count ``+``=` `1` `        ``else``: ` `            ``current_count ``=` `0` `        ``max_count ``=` `max``(current_count,  ` `                            ``max_count) ` `     `  `    ``return` `max_count ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``2``, ``5``, ``11``, ``32``, ``64``, ``88``]      ` `    ``n ``=` `len``(arr) ` `    ``k ``=` `8` `    ``print``(longestsubarray(arr, n, k)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program of above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `// function to find longest subarray ` `static` `int` `longestsubarray(``int``[] arr,  ` `                           ``int` `n, ``int` `k) ` `{ ` `    ``int` `current_count = 0; ` `     `  `    ``// this will contain length of  ` `    ``// longest subarray found ` `    ``int` `max_count = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``if` `(arr[i] % k == 0) ` `            ``current_count++; ` `        ``else` `            ``current_count = 0; ` `        ``max_count = Math.Max(current_count,  ` `                             ``max_count); ` `    ``} ` `    ``return` `max_count; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int``[] arr = { 2, 5, 11, 32, 64, 88 }; ` `    ``int` `n = arr.Length; ` `    ``int` `k = 8; ` `    ``Console.Write(longestsubarray(arr, n, k)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Akanksha Rai `

## PHP

 ` `

Output:

```3
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.