Given an array arr[] of size N and an integer K, the task is to find the maximum number of even numbers present in any subarray of size K.
Examples:
Input: arr[] = {2, 3, 5, 4, 7, 6}, K = 3
Output: 2
Explanation:
Subarrays of size K(=3) with maximum count of even numbers are { arr[3], arr[4], arr[5] }
Therefore, the required output is 2
Input: arr[] = {4, 3, 2, 6}, K = 2
Output: 2
Naive Approach: The simplest approach to solve this problem is to generate all possible subarrays of size K and count the even numbers in the subarray. Finally, print the maximum count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxEvenIntegers( int arr[], int N, int M)
{
int ans = 0;
for ( int i = 0; i <= N - M; i++) {
int cnt = 0;
for ( int j = 0; j < M; j++) {
if (arr[i + j] % 2 == 0)
cnt++;
}
ans = max(ans, cnt);
}
return ans;
}
int main()
{
int arr[] = { 2, 3, 5, 4, 7, 6 };
int K = 3;
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxEvenIntegers(arr, N, K) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxEvenIntegers( int arr[], int N, int M)
{
int ans = 0 ;
for ( int i = 0 ; i <= N - M; i++)
{
int cnt = 0 ;
for ( int j = 0 ; j < M; j++)
{
if (arr[i + j] % 2 == 0 )
cnt++;
}
ans = Math.max(ans, cnt);
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 5 , 4 , 7 , 6 };
int K = 3 ;
int N = arr.length;
System.out.print(maxEvenIntegers(arr, N, K) + "\n" );
}
}
|
Python3
def maxEvenIntegers(arr, N, K):
ans = 0
for i in range (N - K + 1 ):
cnt = 0
for j in range ( 0 , K):
if arr[i + j] % 2 = = 0 :
cnt + = 1
ans = max (cnt, ans)
return ans
if __name__ = = '__main__' :
arr = [ 2 , 3 , 5 , 4 , 7 , 6 ]
K = 3
N = len (arr)
print (maxEvenIntegers(arr, N, K))
|
C#
using System;
class GFG
{
static int maxEvenIntegers( int []arr, int N, int M)
{
int ans = 0;
for ( int i = 0; i <= N - M; i++)
{
int cnt = 0;
for ( int j = 0; j < M; j++)
{
if (arr[i + j] % 2 == 0)
cnt++;
}
ans = Math.Max(ans, cnt);
}
return ans;
}
public static void Main( string [] args)
{
int []arr = { 2, 3, 5, 4, 7, 6 };
int K = 3;
int N = arr.Length;
Console.WriteLine(maxEvenIntegers(arr, N, K));
}
}
|
Javascript
<script>
function maxEvenIntegers(arr, N, M)
{
let ans = 0;
for (let i = 0; i <= N - M; i++)
{
let cnt = 0;
for (let j = 0; j < M; j++)
{
if (arr[i + j] % 2 == 0)
cnt++;
}
ans = Math.max(ans, cnt);
}
return ans;
}
let arr = [ 2, 3, 5, 4, 7, 6 ];
let K = 3;
let N = arr.length;
document.write(maxEvenIntegers(arr, N, K) + "<br>" );
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using the Sliding window technique. Follow the steps below to solve the problems:
- Initialize a variable, say cntMaxEven, to store the maximum count of even numbers in a subarray of size K.
- Calculate the count of even numbers in the subarray { arr[0], … arr[K – 1] } and store it into cntMaxEven.
- Traverse the remaining subarrays of size K by iterating over the range [K, N – 1]. For every ith iteration remove the first element of the subarray and insert the current ith element of the array into the current subarray.
- Count the even numbers in the current subarray and update cntMaxEven to the maximum count of even numbers in the current subarray and cntMaxEven.
- Finally, print the value of cntMaxEven.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxEvenIntegers( int arr[], int N, int M)
{
int curr = 0;
for ( int i = 0; i < M; i++) {
if (arr[i] % 2 == 0)
curr++;
}
int ans = curr;
for ( int i = M; i < N; i++) {
if (arr[i - M] % 2 == 0) {
curr--;
}
if (arr[i] % 2 == 0)
curr++;
ans = max(ans, curr);
}
return ans;
}
int main()
{
int arr[] = { 2, 3, 5, 4, 7, 6 };
int M = 3;
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxEvenIntegers(arr, N, M) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxEvenIntegers( int arr[], int N, int M)
{
int curr = 0 ;
for ( int i = 0 ; i < M; i++)
{
if (arr[i] % 2 == 0 )
curr++;
}
int ans = curr;
for ( int i = M; i < N; i++)
{
if (arr[i - M] % 2 == 0 )
{
curr--;
}
if (arr[i] % 2 == 0 )
curr++;
ans = Math.max(ans, curr);
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 5 , 4 , 7 , 6 };
int M = 3 ;
int N = arr.length;
System.out.print(maxEvenIntegers(arr, N, M) + "\n" );
}
}
|
Python3
def maxEvenIntegers(arr, N, M):
curr = 0
for i in range ( 0 , M):
if (arr[i] % 2 = = 0 ):
curr + = 1
ans = curr
for i in range (M, N):
if (arr[i - M] % 2 = = 0 ):
curr - = 1
if (arr[i] % 2 = = 0 ):
curr + = 1
ans = max (curr, ans)
return ans
if __name__ = = '__main__' :
arr = [ 2 , 3 , 5 , 4 , 7 , 6 ]
M = 3
N = len (arr)
print (maxEvenIntegers(arr, N, M))
|
C#
using System;
class GFG
{
static int maxEvenints( int []arr, int N, int M)
{
int curr = 0;
for ( int i = 0; i < M; i++)
{
if (arr[i] % 2 == 0)
curr++;
}
int ans = curr;
for ( int i = M; i < N; i++)
{
if (arr[i - M] % 2 == 0)
{
curr--;
}
if (arr[i] % 2 == 0)
curr++;
ans = Math.Max(ans, curr);
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 5, 4, 7, 6 };
int M = 3;
int N = arr.Length;
Console.Write(maxEvenints(arr, N, M) + "\n" );
}
}
|
Javascript
<script>
function maxEvenLetegers(arr, N, M)
{
let curr = 0;
for (let i = 0; i < M; i++)
{
if (arr[i] % 2 == 0)
curr++;
}
let ans = curr;
for (let i = M; i < N; i++)
{
if (arr[i - M] % 2 == 0)
{
curr--;
}
if (arr[i] % 2 == 0)
curr++;
ans = Math.max(ans, curr);
}
return ans;
}
let arr = [ 2, 3, 5, 4, 7, 6 ];
let M = 3;
let N = arr.length;
document.write(maxEvenLetegers(arr, N, M) + "\n" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)