Python program to get all subsets of given size of a Set We are given a set, and our task is to get all subsets of a given size. For example, if we have s = {1, 2, 3, 4} and need to find subsets of size k = 2, the output should be [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)].Using itertools.combinationsWe can use itertools.combinations(iterable, r) to
3 min read
Python Program to Filter Rows with a specific Pair Sum Given Matrix, the following program shows how to extract all rows which have a pair such that their sum is equal to a specific number, here denoted as K. Input : test_list = [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]], k = 8 Output : [[1, 5, 3, 6], [6, 9, 3, 2]] Explanation : 5 + 3 = 8
6 min read
Python program to find all possible pairs with given sum Given a list of integers and an integer variable K, write a Python program to find all pairs in the list with given sum K. Examples: Input : lst =[1, 5, 3, 7, 9] K = 12 Output : [(5, 7), (3, 9)] Input : lst = [2, 1, 5, 7, -1, 4] K = 6 Output : [(2, 4), (1, 5), (7, -1)] Method #1: Pythonic Naive This
7 min read
Python program to find sum of elements in list Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently.Using sum()sum() function is th
3 min read
Python3 Program to Find if there is a subarray with 0 sum Given an array of positive and negative numbers, find if there is a subarray (of size at-least one) with 0 sum.Examples : Input: {4, 2, -3, 1, 6}Output: true Explanation:There is a subarray with zero sum from index 1 to 3.Input: {4, 2, 0, 1, 6}Output: true Explanation:There is a subarray with zero s
3 min read
Python Program for Count pairs with given sum Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5}, sum = 6 Output : 3 Pairs with sum 6 are
3 min read
Python Program to get K length groups with given summation Given a list, our task is to write a Python program to extract all K length sublists with lead to given summation. Input : test_list = [6, 3, 12, 7, 4, 11], N = 21, K = 4 Output : [(6, 6, 6, 3), (6, 6, 3, 6), (6, 3, 6, 6), (6, 7, 4, 4), (6, 4, 7, 4), (6, 4, 4, 7), (3, 6, 6, 6), (3, 3, 3, 12), (3, 3,
6 min read
Python Program to get all possible differences between set elements Given a set, the task is to write a Python program to get all possible differences between its elements. Input : test_set = {1, 5, 2, 7, 3, 4, 10, 14} Output : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} Explanation : All possible differences are computed. Input : test_set = {1, 5, 2, 7} Output : {1
4 min read