Number of sub arrays with odd sum | Function to find number of subarrays with odd sum ; ' odd ' stores number of odd numbers upto ith index ' c _ odd ' stores number of odd sum subarrays starting at ith index ' Result ' stores the number of odd sum subarrays ; First find number of odd sum subarrays starting at 0 th index ; Find number of odd sum subarrays starting at ith index add to result ; Driver code
function countOddSum ( a , n ) { let c_odd = 0 , result = 0 ; let odd = false ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { odd = ! odd ; } if ( odd ) { c_odd ++ ; } } for ( let i = 0 ; i < n ; i ++ ) { result += c_odd ; if ( a [ i ] % 2 == 1 ) { c_odd = ( n - i - c_odd ) ; } } return result ; } let ar = [ 5 , 4 , 4 , 5 , 1 , 3 ] ; let n = ar . length ; document . write ( " " + " " + countOddSum ( ar , n ) ) ;