Number of sub arrays with odd sum | Python 3 proggram to find count of sub - arrays with odd sum ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there is a single odd element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ ▁ sum ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ ▁ modulo ▁ 2 ▁ and ▁ increments ▁ ▁ even / odd ▁ count ▁ according ▁ ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even / odd count ; An odd can be formed by even - odd pair ; Driver code
def countOddSum ( ar , n ) : NEW_LINE INDENT temp = [ 1 , 0 ] NEW_LINE result = 0 NEW_LINE val = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT val = ( ( val + ar [ i ] ) % 2 + 2 ) % 2 NEW_LINE temp [ val ] += 1 NEW_LINE DEDENT result = ( temp [ 0 ] * temp [ 1 ] ) NEW_LINE return ( result ) NEW_LINE DEDENT ar = [ 5 , 4 , 4 , 5 , 1 , 3 ] NEW_LINE print ( " The ▁ Number ▁ of ▁ Subarrays " " ▁ with ▁ odd ▁ sum ▁ is ▁ " + str ( countOddSum ( ar , 6 ) ) ) NEW_LINE