Given an array, **arr[] **of size **N** and an integer **K**, the task is to find the length of the longest subsequence from the given array such that the absolute difference of each pair in the subsequence is divisible by **K**.

**Examples:**

Input:arr[] = {10, 12, 16, 20, 32, 15}, K = 4Output:4Explanation:

The Longest subsequence in which the absolute difference of each pair divisible by K (= 4) are {12, 26, 20, 32}.

Therefore, the required output is 4

Input:arr[] = {12, 3, 13, 5, 21, 11}, K = 3Output:3

**Naive Approach**: The simplest approach to solve this problem is to generate all possible subsequence of the given array and print the length of the longest subsequence having an absolute difference of each pair divisible by **K**.

**Time Complexity:** O(2^{N})**Auxiliary Space:** O(N)

**Efficient Approach**: To optimize the above approach the idea is to use Hashing based on the following observation:

Absolute difference of all possible pairs of a subset having the equal value of arr[i] % K must be divisible by K.

Mathematical Proof:

If arr[i] % K = arr[j] % K

=> abs(arr[i] – arr[j]) % K must be 0.

Follow the steps below to solve the problem:

- Initialize an array, say
**hash[K]**to store the frequency of**arr[i] % K**. -
Traverse the
**hash[]**array and find the maximum element in**hash[]**array. - Finally, print the maximum element of the
**hash[]**array.

Below is the implementation of the above approach :

`// C++14 program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;`
`// Function to find the length` `// of subsequence that satisfy` `// the given condition` `int` `maxLenSub(` `int` `arr[],`
` ` `int` `N, ` `int` `K)`
`{` ` ` `// Store the frequencies`
` ` `// of arr[i] % K`
` ` `int` `hash[K];`
` ` `// Initilize hash[] array`
` ` `memset` `(hash, 0, ` `sizeof` `(hash));`
` ` `// Traverse the given array`
` ` `for` `(` `int` `i = 0; i < N; i++) {`
` ` `// Update frequency of`
` ` `// arr[i] % K`
` ` `hash[arr[i] % K]++;`
` ` `}`
` ` `// Stores the length of`
` ` `// the longest subsequence that`
` ` `// satisfy the given condition`
` ` `int` `LenSub = 0;`
` ` `// Find the maximum element`
` ` `// in hash[] array`
` ` `for` `(` `int` `i = 0; i < K; i++) {`
` ` `LenSub = max(LenSub, hash[i]);`
` ` `}`
`}` `// Driver Code` `int` `main()`
`{` ` ` `int` `arr[] = { 12, 3, 13, 5, 21, 11 };`
` ` `int` `K = 3;`
` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);`
` ` `cout << maxLenSub(arr, N, K);`
`}` |

*chevron_right*

*filter_none*

`// Java program to implement` `// the above approach` `import` `java.util.*;`
`class` `GFG{`
`// Function to find the length` `// of subsequence that satisfy` `// the given condition` `static` `int` `maxLenSub(` `int` `arr[],`
` ` `int` `N, ` `int` `K)`
`{` ` ` `// Store the frequencies`
` ` `// of arr[i] % K`
` ` `int` `[]hash = ` `new` `int` `[K];`
` ` `// Traverse the given array`
` ` `for` `(` `int` `i = ` `0` `; i < N; i++) `
` ` `{`
` ` `// Update frequency of`
` ` `// arr[i] % K`
` ` `hash[arr[i] % K]++;`
` ` `}`
` ` `// Stores the length of`
` ` `// the longest subsequence that`
` ` `// satisfy the given condition`
` ` `int` `LenSub = ` `0` `;`
` ` `// Find the maximum element`
` ` `// in hash[] array`
` ` `for` `(` `int` `i = ` `0` `; i < K; i++) `
` ` `{`
` ` `LenSub = Math.max(LenSub, `
` ` `hash[i]);`
` ` `}`
` ` ` ` `return` `LenSub;`
`}` `// Driver Code` `public` `static` `void` `main(String[] args)`
`{` ` ` `int` `arr[] = {` `12` `, ` `3` `, ` `13` `, ` `5` `, ` `21` `, ` `11` `};`
` ` `int` `K = ` `3` `;`
` ` `int` `N = arr.length;`
` ` `System.out.print(maxLenSub(arr, N, K));`
`}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

`# Python3 program to implement` `# the above approach` `# Function to find the length` `# of subsequence that satisfy` `# the given condition` `def` `maxLenSub(arr, N, K):`
` ` ` ` `# Store the frequencies`
` ` `# of arr[i] % K`
` ` `hash` `=` `[` `0` `] ` `*` `K`
` ` `# Traverse the given array`
` ` `for` `i ` `in` `range` `(N):`
` ` `# Update frequency of`
` ` `# arr[i] % K`
` ` `hash` `[arr[i] ` `%` `K] ` `+` `=` `1`
` ` `# Stores the length of the`
` ` `# longest subsequence that`
` ` `# satisfy the given condition`
` ` `LenSub ` `=` `0`
` ` `# Find the maximum element`
` ` `# in hash[] array`
` ` `for` `i ` `in` `range` `(K):`
` ` `LenSub ` `=` `max` `(LenSub, ` `hash` `[i])`
` ` ` ` `return` `LenSub `
`# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:`
` ` ` ` `arr ` `=` `[ ` `12` `, ` `3` `, ` `13` `, ` `5` `, ` `21` `, ` `11` `]`
` ` `K ` `=` `3`
` ` `N ` `=` `len` `(arr)`
` ` ` ` `print` `(maxLenSub(arr, N, K))`
`# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

`// C# program to implement` `// the above approach` `using` `System;`
`class` `GFG{`
`// Function to find the length` `// of subsequence that satisfy` `// the given condition` `static` `int` `maxLenSub(` `int` `[]arr,`
` ` `int` `N, ` `int` `K)`
`{` ` ` `// Store the frequencies`
` ` `// of arr[i] % K`
` ` `int` `[]hash = ` `new` `int` `[K];`
` ` `// Traverse the given array`
` ` `for` `(` `int` `i = 0; i < N; i++) `
` ` `{`
` ` `// Update frequency of`
` ` `// arr[i] % K`
` ` `hash[arr[i] % K]++;`
` ` `}`
` ` `// Stores the length of`
` ` `// the longest subsequence that`
` ` `// satisfy the given condition`
` ` `int` `LenSub = 0;`
` ` `// Find the maximum element`
` ` `// in hash[] array`
` ` `for` `(` `int` `i = 0; i < K; i++) `
` ` `{`
` ` `LenSub = Math.Max(LenSub, `
` ` `hash[i]);`
` ` `}`
` ` `return` `LenSub;`
`}` `// Driver Code` `public` `static` `void` `Main(String[] args)`
`{` ` ` `int` `[]arr = {12, 3, 13, `
` ` `5, 21, 11};`
` ` `int` `K = 3;`
` ` `int` `N = arr.Length;`
` ` `Console.Write(maxLenSub(arr, N, K));`
`}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output**

3

**Time Complexity:** O(N)**Auxiliary Space:** O(K)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of longest increasing absolute even subsequence
- Length of longest subsequence in an Array having all elements as Nude Numbers
- Count all disjoint pairs having absolute difference at least K from a given array
- Longest subsequence such that absolute difference between every pair is atmost 1
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Find the length of the longest subsequence with first K alphabets having same frequency
- Length of longest subsequence having sum of digits of each element as a Composite Number
- Count pairs in an array whose absolute difference is divisible by K
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Longest subsequence having difference atmost K
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Sum of absolute difference of all pairs raised to power K
- Count of all pairs in an Array with minimum absolute difference
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Find the equal pairs of subsequence of S and subsequence of T
- Longest subsequence whose sum is divisible by a given number
- Longest Subsequence from a numeric String divisible by K

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.