Check if K Consecutive even numbers present or not
Given an array, arr[] of integers and an integer K, the task is to print true if there are K consecutive even numbers present else return false.
Input: arr[] = {1, 2, 4, 6, 7, 8}, K = 3
Output: true
Explanation: There are K = 3 consecutive even elements (2, 4, 6) present in the array.
Input: arr[] = {1, 2, 3, 4, 5}, K = 2
Output: false
Approach: This can be solved with the following idea:
Get the index of all even elements in arr and check if there are increasing consecutive elements (increasing by 1) present at least K times.
Below is the implementation of the above approach:
- Create a vector index[], and get all indexes of even numbers in it.
- Then, check if consecutive increasing elements are present in the index[] at least K times.
- If yes, print “true”, else “false”.
Below is the implementation for the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void checkConsecutive( int arr[], int n, int k)
{
vector< int > index;
int i = 0;
while (i < n) {
if (arr[i] % 2 == 0) {
index.push_back(i);
}
i++;
}
int count = 1;
i = 1;
while (i < index.size()) {
if (index[i] == index[i - 1] + 1) {
count++;
}
else {
count = 1;
}
if (count >= k) {
cout << "true" ;
return ;
}
i++;
}
cout << "false" ;
return ;
}
int main()
{
int N = 6;
int arr[] = { 1, 2, 4, 6, 7, 8 };
int K = 3;
checkConsecutive(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void checkConsecutive( int arr[], int n,
int k)
{
List<Integer> index = new ArrayList<Integer>();
int i = 0 ;
while (i < n) {
if (arr[i] % 2 == 0 ) {
index.add(i);
}
i++;
}
int count = 1 ;
i = 1 ;
while (i < index.size()) {
if (index.get(i) == index.get(i - 1 ) + 1 ) {
count++;
}
else {
count = 1 ;
}
if (count >= k) {
System.out.println( "true" );
return ;
}
i++;
}
System.out.println( "false" );
return ;
}
public static void main(String[] args)
{
int N = 6 ;
int arr[] = { 1 , 2 , 4 , 6 , 7 , 8 };
int K = 3 ;
checkConsecutive(arr, N, K);
}
}
|
Python3
def checkConsecutive(arr, n, k):
index = []
i = 0
while i < n:
if arr[i] % 2 = = 0 :
index.append(i)
i + = 1
count = 1
i = 1
while i < len (index):
if index[i] = = index[i - 1 ] + 1 :
count + = 1
else :
count = 1
if count > = k:
print ( "true" )
return
i + = 1
print ( "false" )
return
if __name__ = = '__main__' :
N = 6
arr = [ 1 , 2 , 4 , 6 , 7 , 8 ]
K = 3
checkConsecutive(arr, N, K)
|
Javascript
function checkConsecutive(arr, n, k)
{
let index=[];
let i = 0;
while (i < n) {
if (arr[i] % 2 == 0) {
index.push(i);
}
i++;
}
let count = 1;
i = 1;
while (i < index.length) {
if (index[i] == index[i - 1] + 1) {
count++;
}
else {
count = 1;
}
if (count >= k) {
document.write( "true" );
return ;
}
i++;
}
document.write( "false" );
return ;
}
let N = 6;
let arr = [ 1, 2, 4, 6, 7, 8 ];
let K = 3;
checkConsecutive(arr, N, K);
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void checkConsecutive( int [] arr, int n,
int k)
{
List< int > index = new List< int >();
int i = 0;
while (i < n) {
if (arr[i] % 2 == 0) {
index.Add(i);
}
i++;
}
int count = 1;
i = 1;
while (i < index.Count) {
if (index[i] == index[i - 1] + 1) {
count++;
}
else {
count = 1;
}
if (count >= k) {
Console.WriteLine( "true" );
return ;
}
i++;
}
Console.WriteLine( "false" );
return ;
}
static public void Main()
{
int N = 6;
int [] arr = { 1, 2, 4, 6, 7, 8 };
int K = 3;
checkConsecutive(arr, N, K);
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach:
We can avoid use of extra auxiliary space in the above approach. We can just have a counter of consecutive evens as we are traversing the array. Once there is break in continuity of even numbers, we can reset the counter again to 0 and proceed to next element.
The algorithm will be involving the following steps:
- Traverse the array from left to right.
- Initialize a counter variable to 0.
- For each even element, increment the counter variable.
- If an odd element is encountered, reset the counter variable to 0.
- Check if the counter variable is equal to k. If it is, return true.
- If the end of the array is reached and the counter variable is not equal to k, return false.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool checkConsecutiveEven( int arr[], int n, int k) {
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
count++;
if (count >= k)
return true ;
}
else {
count = 0;
}
}
return false ;
}
int main() {
int arr[] = { 1, 2, 4, 6, 7, 8 };
int n = sizeof (arr)/ sizeof (arr[0]);
int k = 3;
if (checkConsecutiveEven(arr, n, k))
cout << "true\n" ;
else
cout << "false\n" ;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static boolean checkConsecutiveEven( int arr[],
int n, int k)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 0 ) {
count++;
if (count >= k)
return true ;
}
else {
count = 0 ;
}
}
return false ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 6 , 7 , 8 };
int n = arr.length;
int k = 3 ;
if (checkConsecutiveEven(arr, n, k))
System.out.println( "true" );
else
System.out.println( "false" );
}
}
|
Python3
def checkConsecutiveEven(arr, n, k):
count = 0
for i in range (n):
if arr[i] % 2 = = 0 :
count + = 1
if count > = k:
return True
else :
count = 0
return False
arr = [ 1 , 2 , 4 , 6 , 7 , 8 ]
n = len (arr)
k = 3
if checkConsecutiveEven(arr, n, k):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
class GFG {
static bool CheckConsecutiveEven( int [] arr, int n,
int k) {
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
count++;
if (count >= k)
return true ;
}
else {
count = 0;
}
}
return false ;
}
static void Main( string [] args) {
int [] arr = { 1, 2, 4, 6, 7, 8 };
int n = arr.Length;
int k = 3;
if (CheckConsecutiveEven(arr, n, k))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
}
}
|
Javascript
function checkConsecutiveEven(arr, n, k) {
let count = 0;
for (let i = 0; i < n; i++) {
if (arr[i] % 2 === 0) {
count++;
if (count >= k) {
return true ;
}
} else {
count = 0;
}
}
return false ;
}
let arr = [1, 2, 4, 6, 7, 8];
let n = arr.length;
let k = 3;
if (checkConsecutiveEven(arr, n, k)) {
console.log( "true" );
} else {
console.log( "false" );
}
|
Time Complexity: O(N) where N is the size of array.
Auxiliary Space: O(1) as no extra space has been used.
Last Updated :
20 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...