- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to check whether list can be partitioned into pairs where sum is multiple of k in python

Suppose we have a list of numbers called nums and another value k, we have to check whether the list can be partitioned into pairs such that the sum of each pair is divisible by k.

So, if the input is like nums = [4, 7, 2, 5] k = 6, then the output will be True, as we can partition the given list into pairs like (4, 2) and (8, 1) and their sums are divisible by 3.

To solve this, we will follow these steps:

- if nums has even number of elements, then
- return False

- count := a list of size k and fill with 0
- for each n in nums, do
- count[n mod k] := count[n mod k] + 1

- if count[0] is even, then
- return False

- for i in range 1 to quotient of (k / 2), do
- if count[i] is not same as count[k - i], then
- return False

- if count[i] is not same as count[k - i], then
- return True

Let us see the following implementation to get better understanding:

## Example

class Solution: def solve(self, nums, k): if len(nums) % 2: return False count = [0] * k for n in nums: count[n % k] += 1 if count[0] % 2: return False for i in range(1, k // 2 + 1): if count[i] != count[k - i]: return False return True ob = Solution() nums = [4, 7, 2, 5] k = 6 print(ob.solve(nums, k))

## Input

[4, 7, 2, 5], 6

## Output

True

- Related Questions & Answers
- Program to check whether list can be split into sublists of k increasing elements in C++
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Program to check whether we can partition a list with k-partitions of equal sum in C++
- Program to check n can be shown as sum of k or not in Python
- Program to check n can be represented as sum of k primes or not in Python
- Program to Find K-Largest Sum Pairs in Python
- Program to find max number of K-sum pairs in Python
- Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python
- Program to check whether one string can be 1-to-1 mapped into another string in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check a string can be broken into given list of words or not in python
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

Advertisements