Longest subarray with odd product | Java 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
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 ) ) ; } }