Find length of longest subarray with only even elements

Given an array which contains all integer elements, Our goal is to find longest subarray which contains all even numbers. For example

Input arr[] = [3 , 9 , 2 , 4 , 4 , 0 , 1 , 3 , 6 , 4 , 2 , 1 , 3 , 9]
Output =  4
Following subarray of even number 
[2 , 4 , 4 , 0] length 4 
[6 , 4 , 2] length 3

4 is maximum subarray of even numbers

Here given code implementation process.

// C program for
// Find length of longest subarray with only even elements
#include <stdio.h>

int longestEvenSubarray(int arr[], int n)
{
	int even = 0;
	int result = 0;
	for (int i = 0; i < n; ++i)
	{
		if (arr[i] % 2 == 0)
		{
			// When element is even
			even++;
		}
		else
		{
			// When element is odd
			even = 0;
		}
		if (even > result)
		{
			result = even;
		}
	}
	// Return the calculated result
	return result;
}
int main(int argc, char
	const *argv[])
{
	int arr[] = {
		3 , 9 , 2 , 4 , 4 , 0 , 1 , 3 , 6 , 4 , 2 , 1 , 3 , 9
	};
	int n = sizeof(arr) / sizeof(arr[0]);
	int result = longestEvenSubarray(arr, n);
	// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	// [2, 4, 4, 0]
	// Output : 4 
	printf("\n Result : %d", result);
	return 0;
}

Output

 Result : 4
// Java program for
// Longest subarray with even numbers
public class Subarray
{
	public int longestEvenSubarray(int[] arr, int n)
	{
		int even = 0;
		int result = 0;
		for (int i = 0; i < n; ++i)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even++;
			}
			else
			{
				// When element is odd
				even = 0;
			}
			if (even > result)
			{
				result = even;
			}
		}
		// Return the calculated result
		return result;
	}
	public static void main(String[] args)
	{
		Subarray task = new Subarray();
		int arr[] = {
			3 , 9 , 2 , 4 , 4 , 0 , 1 , 3 , 6 , 4 , 2 , 1 , 3 , 9
		};
		int n = arr.length;
		int result = task.longestEvenSubarray(arr, n);
		// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
		// [2, 4, 4, 0]
		// Output : 4 
		System.out.println("\n Result : " + result);
	}
}

Output

 Result : 4
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Longest subarray with even numbers
class Subarray
{
	public: int longestEvenSubarray(int arr[], int n)
	{
		int even = 0;
		int result = 0;
		for (int i = 0; i < n; ++i)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even++;
			}
			else
			{
				// When element is odd
				even = 0;
			}
			if (even > result)
			{
				result = even;
			}
		}
		// Return the calculated result
		return result;
	}
};
int main()
{
	Subarray *task = new Subarray();
	int arr[] = {
		3 , 9 , 2 , 4 , 4 , 0 , 1 , 3 , 6 , 4 , 2 , 1 , 3 , 9
	};
	int n = sizeof(arr) / sizeof(arr[0]);
	int result = task->longestEvenSubarray(arr, n);
	// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	// [2, 4, 4, 0]
	// Output : 4 
	cout << "\n Result : " << result << endl;
	return 0;
}

Output

 Result : 4
package main
import "fmt"
// Go program for
// Longest subarray with even numbers

func longestEvenSubarray(arr[] int, n int) int {
	var even int = 0
	var result int = 0
	for i := 0 ; i < n ; i++ {
		if arr[i] % 2 == 0 {
			// When element is even
			even++
		} else {
			// When element is odd
			even = 0
		}
		if even > result {
			result = even
		}
	}
	// Return the calculated result
	return result
}
func main() {

	var arr = [] int {
		3,
		9,
		2,
		4,
		4,
		0,
		1,
		3,
		6,
		4,
		2,
		1,
		3,
		9,
	}
	var n int = len(arr)
	var result int = longestEvenSubarray(arr, n)
	// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	// [2, 4, 4, 0]
	// Output : 4 
	fmt.Println("\n Result : ", result)
}

Output

 Result : 4
// Include namespace system
using System;
namespace KalkiCode
{
	// Csharp program for
	// Longest subarray with even numbers
	public class Subarray
	{
		public int longestEvenSubarray(int[] arr, int n)
		{
			var even = 0;
			var result = 0;
			for (var i = 0; i < n; ++i)
			{
				if (arr[i] % 2 == 0)
				{
					// When element is even
					even++;
				}
				else
				{
					// When element is odd
					even = 0;
				}
				if (even > result)
				{
					result = even;
				}
			}
			// Return the calculated result
			return result;
		}
		public static void Main(String[] args)
		{
			var task = new Subarray();
			int[] arr = {
				3 , 9 , 2 , 4 , 4 , 0 , 1 , 3 , 6 , 4 , 2 , 1 , 3 , 9
			};
			var n = arr.Length;
			var result = task.longestEvenSubarray(arr, n);
			// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
			// [2, 4, 4, 0]
			// Output : 4 
			Console.WriteLine("\n Result : " + result.ToString());
		}
	}
}

Output

 Result : 4
<?php
// Php program for
// Longest subarray with even numbers
class Subarray
{
	public	function longestEvenSubarray($arr, $n)
	{
		$even = 0;
		$result = 0;
		for ($i = 0; $i < $n; ++$i)
		{
			if ($arr[$i] % 2 == 0)
			{
				// When element is even
				$even++;
			}
			else
			{
				// When element is odd
				$even = 0;
			}
			if ($even > $result)
			{
				$result = $even;
			}
		}
		// Return the calculated result
		return $result;
	}
	public static
	function main($args)
	{
		$task = new Subarray();
		$arr = array(3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9);
		$n = count($arr);
		$result = $task->longestEvenSubarray($arr, $n);
		// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
		// [2, 4, 4, 0]
		// Output : 4 
		printf("%s\n", "\n Result : ".strval($result));
	}
}
Subarray::main(array());

Output

 Result : 4
// Node JS program for
// Longest subarray with even numbers
class Subarray
{
	longestEvenSubarray(arr, n)
	{
		var even = 0;
		var result = 0;
		for (var i = 0; i < n; ++i)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even++;
			}
			else
			{
				// When element is odd
				even = 0;
			}
			if (even > result)
			{
				result = even;
			}
		}
		// Return the calculated result
		return result;
	}
}

function main()
{
	var task = new Subarray();
	var arr = [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9];
	var n = arr.length;
	var result = task.longestEvenSubarray(arr, n);
	// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	// [2, 4, 4, 0]
	// Output : 4 
	console.log("\n Result : " + result);
}
// Start program execution
main();

Output

 Result : 4
#  Python 3 program for
#  Longest subarray with even numbers
class Subarray :
	def longestEvenSubarray(self, arr, n) :
		even = 0
		result = 0
		i = 0
		while (i < n) :
			if (arr[i] % 2 == 0) :
				#  When element is even
				even += 1
			else :
				#  When element is odd
				even = 0
			
			if (even > result) :
				result = even
			
			i += 1
		
		#  Return the calculated result
		return result
	

def main() :
	task = Subarray()
	arr = [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	n = len(arr)
	result = task.longestEvenSubarray(arr, n)
	#  Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	#  [2, 4, 4, 0]
	#  Output : 4 
	print("\n Result : ", result)

if __name__ == "__main__": main()

Output

 Result :  4
#  Ruby program for
#  Longest subarray with even numbers
class Subarray 
	def longestEvenSubarray(arr, n) 
		even = 0
		result = 0
		i = 0
		while (i < n) 
			if (arr[i] % 2 == 0) 
				#  When element is even
				even += 1
			else
				#  When element is odd
				even = 0
			end

			if (even > result) 
				result = even
			end

			i += 1
		end

		#  Return the calculated result
		return result
	end

end

def main() 
	task = Subarray.new()
	arr = [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	n = arr.length
	result = task.longestEvenSubarray(arr, n)
	#  Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	#  [2, 4, 4, 0]
	#  Output : 4 
	print("\n Result : ", result, "\n")
end

main()

Output

 Result : 4
// Scala program for
// Longest subarray with even numbers
class Subarray()
{
	def longestEvenSubarray(arr: Array[Int], n: Int): Int = {
		var even: Int = 0;
		var result: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (arr(i) % 2 == 0)
			{
				// When element is even
				even += 1;
			}
			else
			{
				// When element is odd
				even = 0;
			}
			if (even > result)
			{
				result = even;
			}
			i += 1;
		}
		// Return the calculated result
		return result;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Subarray = new Subarray();
		var arr: Array[Int] = Array(3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9);
		var n: Int = arr.length;
		var result: Int = task.longestEvenSubarray(arr, n);
		// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
		// [2, 4, 4, 0]
		// Output : 4 
		println("\n Result : " + result);
	}
}

Output

 Result : 4
import Foundation;
// Swift 4 program for
// Longest subarray with even numbers
class Subarray
{
	func longestEvenSubarray(_ arr: [Int], _ n: Int) -> Int
	{
		var even: Int = 0;
		var result: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even += 1;
			}
			else
			{
				// When element is odd
				even = 0;
			}
			if (even > result)
			{
				result = even;
			}
			i += 1;
		}
		// Return the calculated result
		return result;
	}
}
func main()
{
	let task: Subarray = Subarray();
	let arr: [Int] = [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9];
	let n: Int = arr.count;
	let result: Int = task.longestEvenSubarray(arr, n);
	// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	// [2, 4, 4, 0]
	// Output : 4 
	print("\n Result : ", result);
}
main();

Output

 Result :  4
// Kotlin program for
// Longest subarray with even numbers
class Subarray
{
	fun longestEvenSubarray(arr: Array < Int > , n: Int): Int
	{
		var even: Int = 0;
		var result: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even += 1;
			}
			else
			{
				// When element is odd
				even = 0;
			}
			if (even > result)
			{
				result = even;
			}
			i += 1;
		}
		// Return the calculated result
		return result;
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Subarray = Subarray();
	val arr: Array < Int > = arrayOf(3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9);
	val n: Int = arr.count();
	val result: Int = task.longestEvenSubarray(arr, n);
	// Input : [3, 9, 2, 4, 4, 0, 1, 3, 6, 4, 2, 1, 3, 9]
	// [2, 4, 4, 0]
	// Output : 4 
	println("\n Result : " + result);
}

Output

 Result : 4


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved