Find the length of the longest subarray with atmost K occurrences of the integer X
Last Updated :
19 May, 2021
Given two numbers K, X and an array arr[] containing N integers, the task is to find the length of the longest subarray such that it contains atmost ‘K’ occurrences of integer’X’.
Examples:
Input: K = 2, X = 2, arr[] = {1, 2, 2, 3, 4}
Output: 5
Explanation:
The longest sub-array is {1, 2, 2, 3, 4} which is the complete array as it contains at-most ‘2’ occurrences of the element ‘2’.
Input: K = 1, X = 2, arr[] = {1, 2, 2, 3, 4},
Output: 3
Explanation:
The longest sub-array is {2, 3, 4} as it contains at-most ‘1’ occurrence of the element ‘2’.
Naive Approach: The naive approach for this problem is to generate all possible subarrays for the given subarray. Then, for every subarray, find the largest subarray that contains at-most K occurrence of the element X. The time complexity for this approach is O(N2) where N is the number of elements in the array.
Efficient Approach: The idea is to solve this problem is to use the two pointer technique.
- Initialize two pointers ‘i’ and ‘j’ to -1 and 0 respectively.
- Keep incrementing ‘i’. If an element X is found, increase the count of that element by keeping a counter.
- If the count of X becomes greater than K, then decrease the count and also decrement the value of ‘j’.
- If the count of X becomes less than or equal to K, increment ‘i’ and make no changes to ‘j’.
- The indices ‘i’ and ‘j’ here represents the starting point and ending point of the subarray which is being considered.
- Therefore, at every step, find the value of |i – j + 1|. The maximum possible value for this is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longest( int a[], int n, int k, int x)
{
int max = 0;
int i = -1;
int j = 0;
int m1 = 0;
while (i < n) {
if (m1 <= k) {
i++;
if (a[i] == x) {
m1++;
}
}
else {
if (a[j] == x) {
m1--;
}
j++;
}
if (m1 <= k && i < n) {
if ( abs (i - j + 1) > max) {
max = abs (i - j + 1);
}
}
}
return max;
}
int main()
{
int arr[] = { 1, 2, 2, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 2;
int x = 2;
cout << longest(arr, n, k, x);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int longest( int a[], int n,
int k, int x)
{
int max = 0 ;
int i = - 1 ;
int j = 0 ;
int m1 = 0 ;
while (i < n)
{
if (m1 <= k)
{
i++;
if (i < a.length && a[i] == x)
{
m1++;
}
}
else
{
if (j < a.length && a[j] == x)
{
m1--;
}
j++;
}
if (m1 <= k && i < n)
{
if (Math.abs(i - j + 1 ) > max)
{
max = Math.abs(i - j + 1 );
}
}
}
return max;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 , 3 , 4 };
int n = arr.length;
int k = 2 ;
int x = 2 ;
System.out.print(longest(arr, n, k, x));
}
}
|
Python3
def longest(a, n, k, x):
max = 0 ;
i = - 1 ;
j = 0 ;
m1 = 0 ;
while (i < n):
if (m1 < = k):
if (a[i] = = x):
m1 + = 1 ;
i + = 1 ;
else :
if (a[j] = = x):
m1 - = 1 ;
j + = 1 ;
if (m1 < = k and i < n):
if ( abs (i - j + 1 ) > max ):
max = abs (i - j + 1 );
return max ;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 , 3 , 4 ];
n = len (arr);
k = 2 ;
x = 2 ;
print (longest(arr, n, k, x));
|
C#
using System;
class GFG{
static int longest( int []a, int n,
int k, int x)
{
int max = 0;
int i = -1;
int j = 0;
int m1 = 0;
while (i < n)
{
if (m1 <= k)
{
i++;
if (i < a.Length && a[i] == x)
{
m1++;
}
}
else
{
if (j < a.Length && a[j] == x)
{
m1--;
}
j++;
}
if (m1 <= k && i < n)
{
if (Math.Abs(i - j + 1) > max)
{
max = Math.Abs(i - j + 1);
}
}
}
return max;
}
public static void Main( string [] args)
{
int []arr = { 1, 2, 2, 3, 4 };
int n = arr.Length;
int k = 2;
int x = 2;
Console.WriteLine(longest(arr, n, k, x));
}
}
|
Javascript
<script>
function longest( a , n , k , x) {
var max = 0;
var i = -1;
var j = 0;
var m1 = 0;
while (i < n) {
if (m1 <= k) {
i++;
if (i < a.length && a[i] == x) {
m1++;
}
}
else {
if (j < a.length && a[j] == x) {
m1--;
}
j++;
}
if (m1 <= k && i < n) {
if (Math.abs(i - j + 1) > max) {
max = Math.abs(i - j + 1);
}
}
}
return max;
}
var arr = [ 1, 2, 2, 3, 4 ];
var n = arr.length;
var k = 2;
var x = 2;
document.write(longest(arr, n, k, x));
</script>
|
Time Complexity: O(N), where N is the length of the array.
Share your thoughts in the comments
Please Login to comment...