# Longest subarray with odd product

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:
Explanation:
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:
Explanation:
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 ` `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; ` `} `

## 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 `

## 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 `

## 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 `

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : rutvik_56, amreshkumar3