
- 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 Find Out the Largest K-Divisible Subsequence Sum in Python
Suppose we are given a list of non−negative numbers, and a positive value k. We have to find the maximum sum subsequence of numbers such that the sum is divisible by k.
So, if the input is like, nums = [4, 6, 8, 2], k = 2, then the output will be 20.
The sum of the whole array is 20, which is divisible by 2.
To solve this, we will follow these steps −
numsSum := sum of the values in input list nums
remainder := numsSum mod k
if remainder is same as 0, then
return numsSum
sort the list nums
for each number combination tpl in nums. do
subSeqSum := sum(tpl)
if subSeqSum mod k is same as remainder, then
return numsSum − subSeqSum
return 0
Let us see the following implementation to get better understanding −
Example
from itertools import chain, combinations class Solution: def solve(self, nums, k): numsSum = sum(nums) remainder = numsSum % k if remainder == 0: return numsSum nums.sort() for tpl in chain.from_iterable(combinations(nums, r) for r in range(1, len(nums) + 1)): subSeqSum = sum(tpl) if subSeqSum % k == remainder: return numsSum − subSeqSum return 0 ob1 = Solution() print(ob1.solve([4, 6, 8, 2], 2))
Input
[4, 6, 8, 2], 2
Output
20
- Related Articles
- Program to Find K-Largest Sum Pairs in Python
- Program to find the sum of largest K sublist in Python
- Program to find closest subsequence sum in Python
- Program to find out the k-th largest product of elements of two arrays in Python
- Program to find minimum largest sum of k sublists in C++
- Program to find lexicographically smallest subsequence of size k in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Program to find out the length of longest palindromic subsequence using Python
- Program to find out the largest sum value of a BST in a given binary tree in Python
- C++ Program to find the Largest Divisible Subset in Array
- C++ Program to find the Largest Divisible Pairs Subset
- C++ Program for the Largest K digit number divisible by X?
- Program to find k-th largest XOR coordinate value in Python
- C++ Program for Largest K digit number divisible by X?
- Java Program for Largest K digit number divisible by X

Advertisements