简体   繁体   English

总和可被 k 整除的子序列的数量

[英]number of subsequences whose sum is divisible by k

I just did a coding challenge for a company and was unable to solve this problem.我刚刚为一家公司做了一个编码挑战,但无法解决这个问题。 Problem statement goes like:问题陈述如下:

Given an array of integers, find the number of subsequences in the array whose sum is divisible by k, where k is some positive integer.给定一个整数数组,找出数组中总和可被 k 整除的子序列的数量,其中 k 是某个正整数。

For example, for [4, 1, 3, 2] and k = 3 , the solution is 5. [[3], [1, 2], [4,3,2], [4,2], [1,3,2]] are the subsequences whose sum is divisible by k , ie current_sum + nums[i] % k == 0 , where nums[i] is the current element in the array.例如,对于[4, 1, 3, 2]k = 3 ,解为 5。 [[3], [1, 2], [4,3,2], [4,2], [1,3,2]]是其总和可被k整除的子序列,即current_sum + nums[i] % k == 0 ,其中nums[i]是数组中的当前元素。

I tried to solve this recursively, however, I was unable to pass any test cases.我试图递归地解决这个问题,但是,我无法通过任何测试用例。 My recursive code followed something like this:我的递归代码如下:

def kSum(nums, k):
    def kSum(cur_sum, i):
        if i == len(nums): return 0
        sol = 1 if (cur_sum + nums[i]) % k == 0 else 0
        return sol + kSum(cur_sum, i+1) + kSum(cur_sum + nums[i], i+1)
    return kSum(0, 0)

What is wrong with this recursive approach, and how can I correct it?这种递归方法有什么问题,我该如何纠正? I'm not interested in an iterative solution, I just want to know why this recursive solution is wrong and how I can correct it.我对迭代解决方案不感兴趣,我只想知道为什么这个递归解决方案是错误的以及我如何纠正它。

Are you sure that is not the case test?你确定这不是案例测试吗? For example:例如:

[4, 1, 3, 2], k = 3

has

4+2 = 6, 1+2=3, 3, 1+2+3=6, 4+2+3 = 9

So, your function is right (it gives me 5) and I don't see a major problem with your function.所以,你的函数是正确的(它给了我 5),我认为你的函数没有重大问题。

Here is a javascript reproduction of what you wrote with some console logs to help explain its behavior.这是您使用一些控制台日志编写的内容的 javascript 复制,以帮助解释其行为。

 function kSum(nums, k) { let recursive_depth = 1; function _kSum(cur_sum, i) { recursive_depth++; if (i == nums.length) { recursive_depth--; return 0; } let sol = 0; if (((cur_sum + nums[i]) % k) === 0) { sol = 1; console.log(`Found valid sequence ending with ${nums[i]} with sum = ${cur_sum + nums[i]} with partial sum ${cur_sum} at depth ${recursive_depth}`); } const _kSum1 = _kSum(cur_sum, i+1); const _kSum2 = _kSum(cur_sum + nums[i], i+1); const res = sol + _kSum1 + _kSum2; recursive_depth--; return res; } return _kSum(0, 0); } let arr = [4, 1, 3, 2], k = 3; console.log(kSum(arr, k));

I think this code actually gets the right answer.我认为这段代码实际上得到了正确的答案。 I'm not fluent in Python, but I might have inadvertently fixed a bug in your code though by adding parenthesis around (cur_sum + nums[i]) % k我不精通 Python,但我可能无意中修复了代码中的错误,尽管在(cur_sum + nums[i]) % k周围添加了括号

It seems to me that your solution is correct.在我看来,您的解决方案是正确的。 It reaches the answer by trying all subsequences, which has 2^n complexity.它通过尝试所有复杂度为2^n序列得出答案。 We could formulate it recursively in an O(n*k) search space, although it could be more efficient to table.我们可以在O(n*k)搜索空间中递归地制定它,尽管它可能更有效。 Let f(A, k, i, r) represent how many subsequences leave remainder r when their sum is divided by k , using elements up to A[i] .f(A, k, i, r)表示当它们的总和除以k ,有多少子序列离开余数r ,使用最多为A[i]元素。 Then:然后:

 function f(A, k, i=A.length-1, r=0){ // A[i] leaves remainder r // when divided by k const c = A[i] % k == r ? 1 : 0; if (i == 0) return c; return c + // All previous subsequences // who's sum leaves remainder r // when divided by k f(A, k, i - 1, r) + // All previous subsequences who's // sum when combined with A[i] // leaves remainder r when // divided by k f(A, k, i - 1, (k + r - A[i]%k) % k); } console.log(f([1,2,1], 3)); console.log(f([2,3,5,8], 5)); console.log(f([4,1,3,2], 3)); console.log(f([3,3,3], 3));

找到长度为 3 的总组合,使得总和可被给定数字和 i 整除<j<k< div><div id="text_translate"><p> 所以,我一直在努力寻找问题的最佳解决方案,但我找不到小于 o(n <sup>3</sup> ) 的解决方案。</p><p> 问题陈述是:- 找到数组中三元组的总数,使得 a[i]、a[j]、a[k] 的总和可被给定数 d 整除且 i<j<k。</p><p> 我尝试了多种解决方案,但解决方案都达到了 o(n <sup>3</sup> )。 我需要一个可能小于 o(n <sup>3</sup> ) 的解决方案</p></div></j<k<> - Find total combinations of length 3 such that sum is divisible by a given number and i<j<k

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 求和可被 M 整除的大小为 K 的子数组的数量? - finding the count of number of sub arrays of size K whose sum is divisible by M? 找到长度为 3 的总组合,使得总和可被给定数字和 i 整除<j<k< div><div id="text_translate"><p> 所以,我一直在努力寻找问题的最佳解决方案,但我找不到小于 o(n <sup>3</sup> ) 的解决方案。</p><p> 问题陈述是:- 找到数组中三元组的总数,使得 a[i]、a[j]、a[k] 的总和可被给定数 d 整除且 i<j<k。</p><p> 我尝试了多种解决方案,但解决方案都达到了 o(n <sup>3</sup> )。 我需要一个可能小于 o(n <sup>3</sup> ) 的解决方案</p></div></j<k<> - Find total combinations of length 3 such that sum is divisible by a given number and i<j<k 查找所有总和等于零的不间断子序列 - Find all uninterrupted subsequences whose sum is equal to zero 给定一个由 N 个整数组成的数组和一个整数 K,找出该数组中总和等于 K 的元素对的数量 - Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K 生成具有被n整除的随机数列表 - Generate list of random number with the sum divisible by n 前 n 位可被 n 整除的 10 位数字 - 10 digit number whose first n digits are divisible by n 计算总和可被k整除的所有子数组 - Count all sub-arrays having sum divisible by k 找到一个字符串的子串数,该字符串可以被数字k整除 - Find the number of substrings of a string which can be divisible by a number k 数组中连续子序列的总和 - Sum of contigous subsequences in an array Python中子序列递归的总和 - Sum of subsequences recursion in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM