Longest subarray with odd product | C ++ Program to find the longest subarray with odd product ; Function to return length of longest subarray with odd product ; If even element is encountered ; Update maximum ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }