Count pairs from a given range having even sum | Function to find the count of ordered pairs having even sum ; Stores count of even numbers in the range [ L , R ] ; If L is even ; Update count_even ; Update count_odd ; Stores count of odd numbers in the range [ L , R ] ; Update count_odd ; Update count_odd ; Stores count of pairs whose sum is even and both elements of the pairs are also even ; Stores count of pairs whose sum is even and both elements of the pairs are odd ; Print total ordered pairs whose sum is even ; Driver Code ; Given L & R ; Function Call
def countPairs ( L , R ) : NEW_LINE INDENT count_even = 0 NEW_LINE if ( L % 2 == 0 ) : NEW_LINE INDENT count_even = ( ( R // 2 ) - ( L // 2 ) + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT count_even = ( ( R // 2 ) - ( L // 2 ) ) NEW_LINE DEDENT count_odd = 0 NEW_LINE if ( L % 2 == 0 ) : NEW_LINE INDENT count_odd = ( ( ( R + 1 ) // 2 ) - ( ( L + 1 ) // 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT count_odd = ( ( ( R + 1 ) // 2 ) - ( ( L + 1 ) // 2 ) + 1 ) NEW_LINE DEDENT count_even *= count_even NEW_LINE count_odd *= count_odd NEW_LINE print ( count_even + count_odd ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 1 , 3 NEW_LINE countPairs ( L , R ) NEW_LINE DEDENT