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: 2
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: 3
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++
#include <bits/stdc++.h>
using namespace std;
int Maxlen( int arr[], int n)
{
int ans = 0;
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
count = 0;
else
count++;
ans = max(ans, count);
}
return ans;
}
int main()
{
int arr[] = { 1, 7, 2 };
int n = sizeof (arr) / sizeof ( int );
cout << Maxlen(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int Maxlen( int arr[], int n)
{
int ans = 0 ;
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % 2 == 0 )
count = 0 ;
else
count++;
ans = Math.max(ans, count);
}
return ans;
}
public static void main(String s[])
{
int arr[] = { 1 , 7 , 2 };
int n = arr.length;
System.out.println(Maxlen(arr, n));
}
}
|
Python3
def Maxlen(a, n):
ans = 0
count = 0
for i in range (n):
if a[i] % 2 = = 0 :
count = 0
else :
count + = 1
ans = max (ans, count)
return ans
arr = [ 1 , 7 , 2 ]
n = len (arr)
print (Maxlen(arr, n))
|
C#
using System;
class GFG{
static int Maxlen( int []arr, int n)
{
int ans = 0;
int count = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
count = 0;
else
count++;
ans = Math.Max(ans, count);
}
return ans;
}
public static void Main()
{
int []arr = { 1, 7, 2 };
int n = arr.Length;
Console.WriteLine(Maxlen(arr, n));
}
}
|
Javascript
<script>
function Maxlen(arr, n)
{
let ans = 0;
let count = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
count = 0;
else
count++;
ans = Math.max(ans, count);
}
return ans;
}
let arr = [ 1, 7, 2 ];
let n = arr.length;
document.write(Maxlen(arr, n));
</script>
|
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!
Last Updated :
12 May, 2021
Like Article
Save Article