Length of longest subsequence having absolute difference of all pairs divisible by K
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 = 4
Output: 4
Explanation:
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 = 3
Output: 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(2N)
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:
Below is the implementation of the above approach :
C++14
#include <bits/stdc++.h>
using namespace std;
int maxLenSub( int arr[],
int N, int K)
{
int hash[K];
memset (hash, 0, sizeof (hash));
for ( int i = 0; i < N; i++) {
hash[arr[i] % K]++;
}
int LenSub = 0;
for ( int i = 0; i < K; i++) {
LenSub = max(LenSub, hash[i]);
}
return LenSub;
}
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);
}
|
Java
import java.util.*;
class GFG{
static int maxLenSub( int arr[],
int N, int K)
{
int []hash = new int [K];
for ( int i = 0 ; i < N; i++)
{
hash[arr[i] % K]++;
}
int LenSub = 0 ;
for ( int i = 0 ; i < K; i++)
{
LenSub = Math.max(LenSub,
hash[i]);
}
return LenSub;
}
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));
}
}
|
Python3
def maxLenSub(arr, N, K):
hash = [ 0 ] * K
for i in range (N):
hash [arr[i] % K] + = 1
LenSub = 0
for i in range (K):
LenSub = max (LenSub, hash [i])
return LenSub
if __name__ = = '__main__' :
arr = [ 12 , 3 , 13 , 5 , 21 , 11 ]
K = 3
N = len (arr)
print (maxLenSub(arr, N, K))
|
C#
using System;
class GFG{
static int maxLenSub( int []arr,
int N, int K)
{
int []hash = new int [K];
for ( int i = 0; i < N; i++)
{
hash[arr[i] % K]++;
}
int LenSub = 0;
for ( int i = 0; i < K; i++)
{
LenSub = Math.Max(LenSub,
hash[i]);
}
return LenSub;
}
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));
}
}
|
Javascript
<script>
function maxLenSub(arr, N, K)
{
let hash = Array.from({length: K}, (_, i) => 0);
for (let i = 0; i < N; i++)
{
hash[arr[i] % K]++;
}
let LenSub = 0;
for (let i = 0; i < K; i++)
{
LenSub = Math.max(LenSub,
hash[i]);
}
return LenSub;
}
let arr = [ 12, 3, 13, 5, 21, 11 ];
let K = 3;
let N = arr.length;
document.write(maxLenSub(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(K)
Last Updated :
01 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...