Given an array **arr[]** consisting of **N** elements, the task is to find the length of longest subarray with **odd** product.

**Examples:**

Input:arr[] = {3, 5, 2, 1}Output:2Explanation:

Subarrays with consecutive odd elements are {3, 5}, and {1}.

Since, {3, 5} is the longer, the answer is 2.

Input:arr[] = {8, 5, 3, 1, 0}Output:3Explanation:

Longest subarray with odd product is {5, 3, 1}.

**Approach:**

Following observations are required to solve the problem:

The Product of two odd numbers generates an odd number.

The Product of one odd and one even number generates an even number.

The Product of two even numbers generates an even number.

From the above observations, we can conclude that the longest subarray of consecutive odd elements in the array is the required answer.

Follow the steps below to solve the problem:

- Traverse the array and check if the current element is even or odd.
- If the current element is odd, set
**count**to**1**and keep increasing count until an even element is encountered in the array. - Once an even element is encountered, compare count with
**ans**and update**ans**storing maximum of the two. - Repeat the above steps for the remaining array.
- Finally, print the value stored in
**ans**.

Below is the implementation of the above approach:

## C++

`// C++ Program to find the longest ` `// subarray with odd product ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return length of ` `// longest subarray with odd product ` `int` `Maxlen(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `ans = 0; ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If even element ` ` ` `// is encountered ` ` ` `if` `(arr[i] % 2 == 0) ` ` ` `count = 0; ` ` ` `else` ` ` `count++; ` ` ` ` ` `// Update maximum ` ` ` `ans = max(ans, count); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// int arr[] = { 6, 3, 5, 1 }; ` ` ` `int` `arr[] = { 1, 7, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << Maxlen(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the longest ` `// subarray with odd product ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to return length of ` `// longest subarray with odd product ` `static` `int` `Maxlen(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `ans = ` `0` `; ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If even element ` ` ` `// is encountered ` ` ` `if` `(arr[i] % ` `2` `== ` `0` `) ` ` ` `count = ` `0` `; ` ` ` `else` ` ` `count++; ` ` ` ` ` `// Update maximum ` ` ` `ans = Math.max(ans, count); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String s[]) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `7` `, ` `2` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(Maxlen(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the longest ` `# subarray with odd product ` ` ` `# Function to return length of ` `# longest subarray with odd product ` `def` `Maxlen(a, n): ` ` ` ` ` `ans ` `=` `0` ` ` `count ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# If even element ` ` ` `# is encountered ` ` ` `if` `a[i] ` `%` `2` `=` `=` `0` `: ` ` ` `count ` `=` `0` ` ` `else` `: ` ` ` `count ` `+` `=` `1` ` ` ` ` `# Update maximum ` ` ` `ans ` `=` `max` `(ans, count) ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `arr ` `=` `[ ` `1` `, ` `7` `, ` `2` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(Maxlen(arr, n)) ` ` ` `# This code is contributed by amreshkumar3 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the longest ` `// subarray with odd product ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return length of ` `// longest subarray with odd product ` `static` `int` `Maxlen(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `int` `ans = 0; ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If even element ` ` ` `// is encountered ` ` ` `if` `(arr[i] % 2 == 0) ` ` ` `count = 0; ` ` ` `else` ` ` `count++; ` ` ` ` ` `// Update maximum ` ` ` `ans = Math.Max(ans, count); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = { 1, 7, 2 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(Maxlen(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by amreshkumar3 ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of longest Subarray with equal number of odd and even elements
- Length of the longest alternating even odd subarray
- Length of longest subarray with product greater than or equal to 0
- Length of longest subarray with negative product
- Length of longest subarray with positive product
- Maximum Product Subarray | Added negative product case
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum length of subarray such that sum of the subarray is even
- Count of subarray that does not contain any subarray with sum 0
- Maximum length of subarray such that all elements are equal in the subarray
- First subarray having sum at least half the maximum sum of any subarray of size K
- Check in binary array the number represented by a subarray is odd or even
- Queries to check whether bitwise AND of a subarray is even or odd
- Split array into K disjoint subarrays such that sum of each subarray is odd.
- Count of odd and even parity elements in subarray using MO's algorithm
- Query to count odd and even parity elements in subarray after XOR with K
- Longest Subarray of non-negative Integers
- Longest Prime Subarray after removing one element
- Longest Subarray having sum of elements atmost 'k'
- Longest increasing subarray

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.