- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

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

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

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

- Related Articles
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python
- Program to count number of fraction pairs whose sum is 1 in python
- Python program to Count Even and Odd numbers in a List
- Program to find sum of odd elements from list in Python
- Program to find two pairs of numbers where difference between sum of these pairs are minimized in python
- Program to count number of elements in a list that contains odd number of digits in Python
- Program to check whether list can be partitioned into pairs where sum is multiple of k in python
- Program to find largest distance pair from two list of numbers in Python
- Python program to print odd numbers in a list
- Program to find sum of first N odd numbers in Python
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Python Program for Find sum of odd factors of a number
- Program to find the sum of first n odd numbers in Python

Advertisements