# Longest subarray with elements having equal modulo K

Given an integer K and an array arr of integer elements, the task is to print the length of the longest sub-array such that each element of this sub-array yields same remainder upon division by K.

Examples:

Input: arr[] = {2, 1, 5, 8, 1}, K = 3
Output: 2
{2, 1, 5, 8, 1} gives remainders {2, 1, 2, 2, 1} on division with 3
Hence, longest sub-array length is 2.

Input: arr[] = {1, 100, 2, 9, 4, 32, 6, 3}, K = 2
Output: 3

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

Simple Approach:

• Traverse the array from left to right and store modulo of each element with K in second array.
• Now the task is reduced to find the longest sub-array with same elements.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// function to find longest sub-array ` `// whose elements gives same remainder ` `// when divided with K ` `int` `LongestSubarray(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``// second array contains modulo ` `    ``// results of each element with K ` `    ``int` `arr2[n]; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``arr2[i] = arr[i] % k; ` ` `  `    ``int` `current_length, max_length = 0; ` `    ``int` `j; ` ` `  `    ``// loop for finding longest sub-array ` `    ``// with equal elements ` `    ``for` `(``int` `i = 0; i < n;) { ` `        ``current_length = 1; ` `        ``for` `(j = i + 1; j < n; j++) { ` `            ``if` `(arr2[j] == arr2[i]) ` `                ``current_length++; ` `            ``else` `                ``break``; ` `        ``} ` `        ``max_length = max(max_length, current_length); ` `        ``i = j; ` `    ``} ` `    ``return` `max_length; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 9, 7, 18, 29, 11 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``int` `k = 11; ` `    ``cout << LongestSubarray(arr, n, k); ` `    ``return` `0; ` `} `

## Java

 `//  Java implementation of above approach ` `import` `java .io.*; ` ` `  `class` `GFG ` `{ ` `// function to find longest sub-array ` `// whose elements gives same remainder ` `// when divided with K ` `static` `int` `LongestSubarray(``int``[] arr,  ` `                        ``int` `n, ``int` `k) ` `{ ` `    ``// second array contains modulo ` `    ``// results of each element with K ` `    ``int``[] arr2 = ``new` `int``[n]; ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``arr2[i] = arr[i] % k; ` ` `  `    ``int` `current_length, max_length = ``0``; ` `    ``int` `j; ` ` `  `    ``// loop for finding longest  ` `    ``// sub-array with equal elements ` `    ``for` `(``int` `i = ``0``; i < n;)  ` `    ``{ ` `        ``current_length = ``1``; ` `        ``for` `(j = i + ``1``; j < n; j++)  ` `        ``{ ` `            ``if` `(arr2[j] == arr2[i]) ` `                ``current_length++; ` `            ``else` `                ``break``; ` `        ``} ` `        ``max_length = Math.max(max_length,  ` `                            ``current_length); ` `        ``i = j; ` `    ``} ` `    ``return` `max_length; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int``[] arr = { ``4``, ``9``, ``7``, ``18``, ``29``, ``11` `}; ` `    ``int` `n = arr.length; ` `    ``int` `k = ``11``; ` `    ``System.out.println(LongestSubarray(arr, n, k)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by shs `

## Python 3

 `# Python 3 implementation of above approach ` ` `  `# function to find longest sub-array ` `# whose elements gives same remainder ` `# when divided with K ` `def` `LongestSubarray(arr, n, k): ` ` `  `    ``# second array contains modulo ` `    ``# results of each element with K ` `    ``arr2 ``=` `[``0``] ``*` `n ` `    ``for` `i ``in` `range``( n): ` `        ``arr2[i] ``=` `arr[i] ``%` `k ` `         `  `    ``max_length ``=` `0` ` `  `    ``# loop for finding longest sub-array ` `    ``# with equal elements ` `    ``i ``=` `0` `    ``while` `i < n : ` `        ``current_length ``=` `1` `        ``for` `j ``in` `range``(i ``+` `1``, n): ` `            ``if` `(arr2[j] ``=``=` `arr2[i]): ` `                ``current_length ``+``=` `1` `            ``else``: ` `                ``break` `         `  `        ``max_length ``=` `max``(max_length,  ` `                         ``current_length) ` `        ``i ``=` `j ` `        ``i ``+``=` `1` ` `  `    ``return` `max_length ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``arr ``=` `[ ``4``, ``9``, ``7``, ``18``, ``29``, ``11` `] ` `    ``n ``=` `len``(arr) ` `    ``k ``=` `11` `    ``print``(LongestSubarray(arr, n, k)) ` ` `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `// C# implementation of above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `// function to find longest sub-array ` `// whose elements gives same remainder ` `// when divided with K ` `static` `int` `LongestSubarray(``int``[] arr,  ` `                           ``int` `n, ``int` `k) ` `{ ` `    ``// second array contains modulo ` `    ``// results of each element with K ` `    ``int``[] arr2 = ``new` `int``[n]; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``arr2[i] = arr[i] % k; ` ` `  `    ``int` `current_length, max_length = 0; ` `    ``int` `j; ` ` `  `    ``// loop for finding longest  ` `    ``// sub-array with equal elements ` `    ``for` `(``int` `i = 0; i < n;)  ` `    ``{ ` `        ``current_length = 1; ` `        ``for` `(j = i + 1; j < n; j++)  ` `        ``{ ` `            ``if` `(arr2[j] == arr2[i]) ` `                ``current_length++; ` `            ``else` `                ``break``; ` `        ``} ` `        ``max_length = Math.Max(max_length,    ` `                              ``current_length); ` `        ``i = j; ` `    ``} ` `    ``return` `max_length; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int``[] arr = { 4, 9, 7, 18, 29, 11 }; ` `    ``int` `n = arr.Length; ` `    ``int` `k = 11; ` `    ``Console.Write(LongestSubarray(arr, n, k)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Akanksha Rai `

## PHP

 ` `

Output:

```3
```

Time Complexity: O(n * n)
Auxiliary Space: O(n)

An efficient approach is to keep track of current count in single traversal. Whenever we find an element whose modulo is not same, we reset count as 0.

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// function to find longest sub-array ` `// whose elements gives same remainder ` `// when divided with K ` `int` `LongestSubarray(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `count = 1; ` `    ``int` `max_length = 1;  ` `    ``int` `prev_mod = arr[0] % k; ` `   `  `    ``// Iterate in the array  ` `    ``for` `(``int` `i = 1; i < n; i++) {  ` ` `  `        ``int` `curr_mod = arr[i] % k; ` `   `  `        ``// check if array element  ` `        ``// greater then X or not  ` `        ``if` `(curr_mod == prev_mod) {  ` `            ``count++; ` `        ``}  ` `        ``else` `{  ` `   `  `            ``max_length = max(max_length, count);    ` `            ``count = 1;  ` `            ``prev_mod = curr_mod; ` `        ``}  ` `    ``}  ` `     `  `    ``return` `max_length;  ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 9, 7, 18, 29, 11 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``int` `k = 11; ` `    ``cout << LongestSubarray(arr, n, k); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach  ` ` `  `class` `GFG { ` ` `  `// function to find longest sub-array  ` `// whose elements gives same remainder  ` `// when divided with K  ` `    ``static` `public` `int` `LongestSubarray(``int` `arr[], ``int` `n, ``int` `k) { ` `        ``int` `count = ``1``; ` `        ``int` `max_length = ``1``; ` `        ``int` `prev_mod = arr[``0``] % k; ` ` `  `        ``// Iterate in the array  ` `        ``for` `(``int` `i = ``1``; i < n; i++) { ` ` `  `            ``int` `curr_mod = arr[i] % k; ` ` `  `            ``// check if array element  ` `            ``// greater then X or not  ` `            ``if` `(curr_mod == prev_mod) { ` `                ``count++; ` `            ``} ``else` `{ ` ` `  `                ``max_length = Math.max(max_length, count); ` `                ``count = ``1``; ` `                ``prev_mod = curr_mod; ` `            ``} ` `        ``} ` ` `  `        ``return` `max_length; ` `    ``} ` ` `  `// Driver code  ` `    ``public` `static` `void` `main(String[] args) { ` `        ``int` `arr[] = {``4``, ``9``, ``7``, ``18``, ``29``, ``11``}; ` `        ``int` `n = arr.length; ` `        ``int` `k = ``11``; ` `        ``System.out.print(LongestSubarray(arr, n, k)); ` `    ``} ` `} ` `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of above approach ` ` `  `# function to find longest sub-array ` `# whose elements gives same remainder ` `#  when divided with K ` ` `  `def` `LongestSubarray(arr,n,k): ` `    ``count ``=` `1` `    ``max_lenght ``=` `1` `    ``prev_mod ``=` `arr[``0``]``%``k ` ` `  `    ``# Iterate in the array ` `    ``for` `i ``in` `range``(``1``,n): ` `        ``curr_mod ``=` `arr[i]``%``k ` ` `  `       ``#  check if array element  ` `       ``# greater then X or not  ` `        ``if` `curr_mod``=``=``prev_mod: ` `            ``count``+``=``1` `        ``else``: ` `            ``max_lenght ``=` `max``(max_lenght,count) ` `            ``count``=``1` `            ``prev_mod ``=` `curr_mod ` ` `  ` `  `    ``return` `max_lenght ` ` `  `# Driver code ` `arr ``=` `[``4``, ``9``, ``7``, ``18``, ``29``, ``11``] ` `n ``=` `len``(arr) ` `k ``=``11` `print``(LongestSubarray(arr,n,k)) ` ` `  ` `  ` `  `# This code is contributed by Shrikant13 `

## C#

 `// C# implementation of above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// function to find longest sub-array ` `// whose elements gives same remainder ` `// when divided with K ` `static` `int` `LongestSubarray(``int` `[]arr, ``int` `n, ``int` `k) ` `{ ` `    ``int` `count = 1; ` `    ``int` `max_length = 1;  ` `    ``int` `prev_mod = arr[0] % k; ` ` `  `    ``// Iterate in the array  ` `    ``for` `(``int` `i = 1; i < n; i++) ` `    ``{  ` ` `  `        ``int` `curr_mod = arr[i] % k; ` ` `  `        ``// check if array element  ` `        ``// greater then X or not  ` `        ``if` `(curr_mod == prev_mod) ` `        ``{  ` `            ``count++; ` `        ``}  ` `        ``else`  `        ``{  ` `            ``max_length = Math.Max(max_length, count);  ` `            ``count = 1;  ` `            ``prev_mod = curr_mod; ` `        ``}  ` `    ``}  ` `    ``return` `max_length;  ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int``[] arr = { 4, 9, 7, 18, 29, 11 }; ` `    ``int` `n = arr.Length; ` `    ``int` `k = 11; ` `    ``Console.Write(LongestSubarray(arr, n, k)); ` `} ` `} ` ` `  `// This code is cntributed by Shivi_Aggarwal `

## PHP

 ` `

Output:

```3
```

Time Complexity : O(n)
Auxiliary Space : O(1)

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 :

2

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