Find length of longest subarray with only odd elements

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

``````Input arr[] =
[3 , 9 , 2 , 1 , 3 , 9 , 7 , -3 , 4 , 4 , 0 , 1 , 3 , 5 , 6 , 4]
Output =  5
Following subarray of odd number
[3] length 1
[ 1 , 3 , 9 , 7 , -3] length 5
[1 , 3 , 5] length 3
5 is maximum subarray of odd numbers``````

Here given code implementation process.

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

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

Output

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

Output

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

Output

`` Result : 5``
``````package main
import "fmt"
// Go program for
// Longest subarray with all odd numbers

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

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

Output

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

Output

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

Output

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

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

Output

`` Result : 5``
``````#  Python 3 program for
#  Longest subarray with all odd numbers
class Subarray :
def longestOddSubarray(self, arr, n) :
odd = 0
result = 0
i = 0
while (i < n) :
if (arr[i] % 2 != 0) :
#  When element is odd
odd += 1
else :
#  When element is even
odd = 0

if (odd > result) :
result = odd

i += 1

#  Return the calculated result
return result

def main() :
arr = [3, 9, 2, 1, 3, 9, 7, -3, 4, 4, 0, 1, 3, 5, 6, 4]
n = len(arr)
#  Input : [3 , 9 , 2 , 1 , 3 , 9 , 7 , -3 ,
#           4 , 4 , 0 , 1 , 3 , 5 , 6 , 4]
#  [1, 3, 9, 7, -3]
#  Output : 5
print("\n Result : ", result)

if __name__ == "__main__": main()``````

Output

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

#  When element is even
odd = 0
end

if (odd > result)
result = odd
end

i += 1
end

#  Return the calculated result
return result
end

end

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

main()``````

Output

`````` Result : 5
``````
``````// Scala program for
// Longest subarray with all odd numbers
class Subarray()
{
def longestOddSubarray(arr: Array[Int], n: Int): Int = {
var odd: Int = 0;
var result: Int = 0;
var i: Int = 0;
while (i < n)
{
if (arr(i) % 2 != 0)
{
// When element is odd
odd += 1;
}
else
{
// When element is even
odd = 0;
}
if (odd > result)
{
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(3, 9, 2, 1, 3, 9, 7,
-3, 4, 4, 0, 1, 3, 5, 6, 4);
var n: Int = arr.length;
var result: Int = task.longestOddSubarray(arr, n);
// Input : [3 , 9 , 2 , 1 , 3 , 9 , 7 , -3 ,
//          4 , 4 , 0 , 1 , 3 , 5 , 6 , 4]
// [1, 3, 9, 7, -3]
// Output : 5
println("\n Result : " + result);
}
}``````

Output

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

Output

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

Output

`` Result : 5``

Comment

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.