Longest subarray with all even or all odd elements

Given an array of integer elements which contain random elements of even and odd number. Our goal is to find the length which is longest length of even or odd number. For example.

Input arr: [9 , 2 , 4 , 1 , 3 , 6 , 4 , 8 , 0 , 2 , 1 , 3 , 9]
Output : 5
// Here [6, 4, 8, 0, 2] Longest length subarray which include Even number

Here given code implementation process.

// Java program for
// Longest subarray with all even or all odd elements
public class Subarray
{
	public int longestSubarray(int[] arr, int n)
	{
		int result = 0;
		int even = 0;
		int odd = 0;
		for (int i = 0; i < n; ++i)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even++;
				// Reset odd
				odd = 0;
			}
			else
			{
				// When element is odd
				odd++;
				// Reset even
				even = 0;
			}
			if (result < even)
			{
				// When new Even subarray larger
				result = even;
			}
			if (result < odd)
			{
				// When new Odd subarray larger
				result = odd;
			}
		}
		// Return the calculated result
		return result;
	}
	public static void main(String[] args)
	{
		Subarray task = new Subarray();
		int[] arr = {
			9 , 2 , 4 , 1 , 3 , 6 , 4 , 8 , 0 , 2 , 1 , 3 , 9
		};
		int n = arr.length;
		int result = task.longestSubarray(arr, n);
		// Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
		// [6, 4, 8, 0, 2] Max Length Of Even elements subarray
		// Output : 5 
		System.out.print("\n Result : " + result);
	}
}

Output

 Result : 5
// C program for
// Longest subarray with all even or all odd elements
#include <stdio.h>

int longestSubarray(int arr[], int n)
{
	int result = 0;
	int even = 0;
	int odd = 0;
	for (int i = 0; i < n; ++i)
	{
		if (arr[i] % 2 == 0)
		{
			// When element is even
			even++;
			// Reset odd
			odd = 0;
		}
		else
		{
			// When element is odd
			odd++;
			// Reset even
			even = 0;
		}
		if (result < even)
		{
			// When new Even subarray larger
			result = even;
		}
		if (result < odd)
		{
			// When new Odd subarray larger
			result = odd;
		}
	}
	// Return the calculated result
	return result;
}
int main(int argc, char const *argv[])
{
	int arr[] = {
		9 , 2 , 4 , 1 , 3 , 6 , 4 , 8 , 0 , 2 , 1 , 3 , 9
	};
	int n = sizeof(arr) / sizeof(arr[0]);
	int result = longestSubarray(arr, n);
	// Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	// [6, 4, 8, 0, 2] Max Length Of Even elements subarray
	// Output : 5 
	printf("\n Result : %d", result);
	return 0;
}

Output

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

Output

 Result : 5
package main
import "fmt"
// Go program for
// Longest subarray with all even or all odd elements

func longestSubarray(arr[] int, n int) int {
	var result int = 0
	var even int = 0
	var odd int = 0
	for i := 0 ; i < n ; i++ {
		if arr[i] % 2 == 0 {
			// When element is even
			even++
			// Reset odd
			odd = 0
		} else {
			// When element is odd
			odd++
			// Reset even
			even = 0
		}
		if result < even {
			// When new Even subarray larger
			result = even
		}
		if result < odd {
			// When new Odd subarray larger
			result = odd
		}
	}
	// Return the calculated result
	return result
}
func main() {
	var arr = [] int {
		9,
		2,
		4,
		1,
		3,
		6,
		4,
		8,
		0,
		2,
		1,
		3,
		9,
	}
	var n int = len(arr)
	var result int = longestSubarray(arr, n)
	// Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	// [6, 4, 8, 0, 2] Max Length Of Even elements subarray
	// Output : 5 
	fmt.Print("\n Result : ", result)
}

Output

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

Output

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

Output

 Result : 5
// Node JS program for
// Longest subarray with all even or all odd elements
class Subarray
{
	longestSubarray(arr, n)
	{
		var result = 0;
		var even = 0;
		var odd = 0;
		for (var i = 0; i < n; ++i)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even++;
				// Reset odd
				odd = 0;
			}
			else
			{
				// When element is odd
				odd++;
				// Reset even
				even = 0;
			}
			if (result < even)
			{
				// When new Even subarray larger
				result = even;
			}
			if (result < odd)
			{
				// When new Odd subarray larger
				result = odd;
			}
		}
		// Return the calculated result
		return result;
	}
}

function main()
{
	var task = new Subarray();
	var arr = [9, 2, 4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9];
	var n = arr.length;
	var result = task.longestSubarray(arr, n);
	// Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	// [6, 4, 8, 0, 2] Max Length Of Even elements subarray
	// Output : 5 
	process.stdout.write("\n Result : " + result);
}
// Start program execution
main();

Output

 Result : 5
#  Python 3 program for
#  Longest subarray with all even or all odd elements
class Subarray :
	def longestSubarray(self, arr, n) :
		result = 0
		even = 0
		odd = 0
		i = 0
		while (i < n) :
			if (arr[i] % 2 == 0) :
				#  When element is even
				even += 1
				#  Reset odd
				odd = 0
			else :
				#  When element is odd
				odd += 1
				#  Reset even
				even = 0
			
			if (result < even) :
				#  When new Even sublist larger
				result = even
			
			if (result < odd) :
				#  When new Odd sublist larger
				result = odd
			
			i += 1
		
		#  Return the calculated result
		return result
	

def main() :
	task = Subarray()
	arr = [9, 2, 4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	n = len(arr)
	result = task.longestSubarray(arr, n)
	#  Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	#  [6, 4, 8, 0, 2] Max Length Of Even elements sublist
	#  Output : 5 
	print("\n Result : ", result, end = "")

if __name__ == "__main__": main()

Output

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

			if (result < even) 
				#  When new Even subarray larger
				result = even
			end

			if (result < odd) 
				#  When new Odd subarray larger
				result = odd
			end

			i += 1
		end

		#  Return the calculated result
		return result
	end

end

def main() 
	task = Subarray.new()
	arr = [9, 2, 4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	n = arr.length
	result = task.longestSubarray(arr, n)
	#  Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	#  [6, 4, 8, 0, 2] Max Length Of Even elements subarray
	#  Output : 5 
	print("\n Result : ", result)
end

main()

Output

 Result : 5
// Scala program for
// Longest subarray with all even or all odd elements
class Subarray()
{
	def longestSubarray(arr: Array[Int], n: Int): Int = {
		var result: Int = 0;
		var even: Int = 0;
		var odd: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (arr(i) % 2 == 0)
			{
				// When element is even
				even += 1;
				// Reset odd
				odd = 0;
			}
			else
			{
				// When element is odd
				odd += 1;
				// Reset even
				even = 0;
			}
			if (result < even)
			{
				// When new Even subarray larger
				result = even;
			}
			if (result < odd)
			{
				// When new Odd subarray larger
				result = odd;
			}
			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(9, 2, 4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9);
		var n: Int = arr.length;
		var result: Int = task.longestSubarray(arr, n);
		// Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
		// [6, 4, 8, 0, 2] Max Length Of Even elements subarray
		// Output : 5 
		print("\n Result : " + result);
	}
}

Output

 Result : 5
import Foundation;
// Swift 4 program for
// Longest subarray with all even or all odd elements
class Subarray
{
	func longestSubarray(_ arr: [Int], _ n: Int) -> Int
	{
		var result: Int = 0;
		var even: Int = 0;
		var odd: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even += 1;
				// Reset odd
				odd = 0;
			}
			else
			{
				// When element is odd
				odd += 1;
				// Reset even
				even = 0;
			}
			if (result < even)
			{
				// When new Even subarray larger
				result = even;
			}
			if (result < odd)
			{
				// When new Odd subarray larger
				result = odd;
			}
			i += 1;
		}
		// Return the calculated result
		return result;
	}
}
func main()
{
	let task: Subarray = Subarray();
	let arr: [Int] = [9, 2, 4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9];
	let n: Int = arr.count;
	let result: Int = task.longestSubarray(arr, n);
	// Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	// [6, 4, 8, 0, 2] Max Length Of Even elements subarray
	// Output : 5 
	print("\n Result : ", result, terminator: "");
}
main();

Output

 Result :  5
// Kotlin program for
// Longest subarray with all even or all odd elements
class Subarray
{
	fun longestSubarray(arr: Array < Int > , n: Int): Int
	{
		var result: Int = 0;
		var even: Int = 0;
		var odd: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (arr[i] % 2 == 0)
			{
				// When element is even
				even += 1;
				// Reset odd
				odd = 0;
			}
			else
			{
				// When element is odd
				odd += 1;
				// Reset even
				even = 0;
			}
			if (result < even)
			{
				// When new Even subarray larger
				result = even;
			}
			if (result < odd)
			{
				// When new Odd subarray larger
				result = odd;
			}
			i += 1;
		}
		// Return the calculated result
		return result;
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Subarray = Subarray();
	val arr: Array < Int > = arrayOf(9, 2, 4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9);
	val n: Int = arr.count();
	val result: Int = task.longestSubarray(arr, n);
	// Input : [9,2,4, 1, 3, 6, 4, 8, 0, 2, 1, 3, 9]
	// [6, 4, 8, 0, 2] Max Length Of Even elements subarray
	// Output : 5 
	print("\n Result : " + result);
}

Output

 Result : 5


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