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`

Javascript

 ``

Output:

`2`

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next