# Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a list of positive numbers nums, we have to find the number of valid pairs of indices (i, j), where i < j, and nums[i] + nums[j] is an odd number.

So, if the input is like [5, 4, 6], then the output will be 2, as two pairs are [5,4] and [5,6], whose sum are odd.

To solve this, we will follow these steps −

• e := a list by taking only the even numbers in nums
• return (size of nums - size of e) * size of e

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, nums):
e=[i for i in nums if i%2==0]
return (len(nums)-len(e))*len(e)
nums = [5, 4, 6]
ob = Solution()
print(ob.solve(nums))

## Input

[5, 4, 6]

## Output

2