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
def countOddSum ( a , n ) : NEW_LINE INDENT c_odd = 0 ; NEW_LINE result = 0 ; NEW_LINE odd = False ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 1 ) : NEW_LINE INDENT if ( odd == True ) : NEW_LINE INDENT odd = False ; NEW_LINE DEDENT else : NEW_LINE INDENT odd = True ; NEW_LINE DEDENT DEDENT if ( odd ) : NEW_LINE INDENT c_odd += 1 ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT result += c_odd ; NEW_LINE if ( a [ i ] % 2 == 1 ) : NEW_LINE INDENT c_odd = ( n - i - c_odd ) ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ar = [ 5 , 4 , 4 , 5 , 1 , 3 ] ; NEW_LINE n = len ( ar ) ; NEW_LINE print ( " The ▁ Number ▁ of ▁ Subarrays " + " with ▁ odd ▁ sum ▁ is ▁ " , countOddSum ( ar , n ) ) ; NEW_LINE DEDENT