Longest Subsequence with difference between max and min at most K
Given an array arr[] of size N and a non-negative integer K, the task is to find the length of the longest subsequence such that the difference between the maximum and the minimum of the subsequence is at most K.
Examples:
Input: arr[] = {1, 3, 5, 4, 2}, N = 5, K = 3
Output: 4
Explanation: If we consider, the sequence {1, 3, 4, 2}. The subsequence maximum element is 4 and minimum element is 1 and their absolute difference is 3. So maximum length among all subsequence is 4.
Input: arr[] = {5, 5, 5, 5, 5}, N = 5, K = 1
Output: 5
Naive Approach: The simplest way is to generate all possible subsequence and find the longest among them having the difference between the maximum and the minimum at most K.
Time Complexity: O(N * 2N)
Auxiliary Space: O(N)
Efficient Approach: The problem can be solved efficiently using sorting based on the following idea:
If we sort the array we can get the elements in sorted order. Now the task reduces to finding the longest window such that the difference between the last and first element of the window is at most K. This can be solved using two pointer technique.
Follow the steps mentioned below to implement the idea:
- Sort the array arr[] in ascending order.
- Initialize, i = 0, j = 0 to implement the two pointer approach and MaxSize = 0 to store the length of the longest subsequence.
- Run a loop till j < N:
- If, (arr[j] – arr[i]) ? K [Because the array is sorted so arr[j] is maximum and arr[i] is minimum], update, the MaxSize to have the maximum length and increment j by 1.
- Else increment i by 1.
- Return MaxSize as the required maximum length.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxSize( int * arr, int n, int k)
{
sort(arr, arr + n);
int i = 0, j = 0, MaxSize = 0;
while (j < n) {
if ((arr[j] - arr[i]) <= k) {
MaxSize = max(MaxSize, j - i + 1);
j++;
}
else
i++;
}
return MaxSize;
}
int main()
{
int N, K = 3;
int arr[] = { 1, 3, 5, 4, 2 };
N = sizeof (arr) / sizeof (arr[0]);
cout << findMaxSize(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int findMaxSize( int [] arr, int n, int k)
{
Arrays.sort(arr);
int i = 0 , j = 0 , MaxSize = 0 ;
while (j < n) {
if ((arr[j] - arr[i]) <= k) {
MaxSize = Math.max(MaxSize, j - i + 1 );
j++;
}
else {
i++;
}
}
return MaxSize;
}
public static void main(String[] args)
{
int N, K = 3 ;
int [] arr = { 1 , 3 , 5 , 4 , 2 };
N = arr.length;
System.out.print(findMaxSize(arr, N, K));
}
}
|
Python3
def findMaxSize(arr, n, k):
arr.sort();
i = 0
j = 0
MaxSize = 0 ;
while (j < n):
if ((arr[j] - arr[i]) < = k):
MaxSize = max (MaxSize, j - i + 1 );
j + = 1
else :
i + = 1
return MaxSize;
N = 3
K = 3
arr = [ 1 , 3 , 5 , 4 , 2 ];
N = len (arr)
print (findMaxSize(arr, N, K));
|
C#
using System;
public class GFG {
static int findMaxSize( int [] arr, int n, int k)
{
Array.Sort(arr);
int i = 0, j = 0, MaxSize = 0;
while (j < n) {
if ((arr[j] - arr[i]) <= k) {
MaxSize = Math.Max(MaxSize, j - i + 1);
j++;
}
else {
i++;
}
}
return MaxSize;
}
static public void Main()
{
int N, K = 3;
int [] arr = { 1, 3, 5, 4, 2 };
N = arr.Length;
Console.Write(findMaxSize(arr, N, K));
}
}
|
Javascript
function findMaxSize(arr, n, k) {
arr.sort( function (a, b) { return a - b });
let i = 0, j = 0, MaxSize = 0;
while (j < n) {
if ((arr[j] - arr[i]) <= k) {
MaxSize = Math.max(MaxSize, j - i + 1);
j++;
}
else
i++;
}
return MaxSize;
}
let N, K = 3;
let arr = [1, 3, 5, 4, 2];
N = arr.length;
console.log(findMaxSize(arr, N, K));
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Last Updated :
28 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...