-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLongestSubarrayWithAtMostKEvenElements.java
48 lines (46 loc) · 1.77 KB
/
LongestSubarrayWithAtMostKEvenElements.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*https://practice.geeksforgeeks.org/problems/longest-subarray-with-atmost-k-even-elements/0*/
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
String[] tokens;
int n, k, i, j, currEvenCount, maxLen;
int[] arr;
for (int ii = 0; ii < cases; ++ii)
{
tokens = br.readLine().trim().split(" +");
n = Integer.parseInt(tokens[0]);
k = Integer.parseInt(tokens[1]);
arr = new int[n];
tokens = br.readLine().trim().split(" +");
for (i = 0; i < n; ++i)
arr[i] = Integer.parseInt(tokens[i]);
i = 0;
j = -1;
currEvenCount = 0;
maxLen = 0;
while (j < n) //till we have more elements
{
++j; //increment j
if (j >= n) break; //if crossed bounds, break
if (arr[j]%2 == 0 && currEvenCount < k) //if jth element is even and we can still add more even numbers
{
++currEvenCount; //increment the even count
}
else if (arr[j]%2 == 0) //if jth element is even but we cannot add more even numbers, that means we cannot increase the window size anymore
{
while (arr[i]%2 != 0) ++i; //find out the earliest even number
++i; //remove it from the current window
}
/*This if else block makes sure that the number of even elements is bounded by k*/
if (currEvenCount <= k && j-i+1 > maxLen) maxLen = j-i+1; //if even count is bounded by k, update the maximum window length
}
System.out.println(maxLen);
}
}
}