查找O(N * M)中的子集总数

时间:2016-01-28 13:59:08

标签: algorithm

我最近遇到了一个算法问题,如下所示。

有两个大小为'N'的数组。 找到选择元素子集的算法,使得Array1之和与Array2之和之间的绝对差值应为< ='M'(其中M为常数)。

Eg:

    Array1 -> 1,2
    Array2 -> 4,5

    M =2

Total number of subsets are '6'

The subsets are like below

( sum of array1, sum of array2 )

(0,0) -> no numbers selected 
(1,0) -> selecting 1 from array1, selecting none from array2
(2,0) -> selecting 2 from array1, selecting none from array2
(2,4) -> selecting 2 from array1, selecting 4 from array2
(3,4) -> selecting 1,2 from array1, selecting 4 from array2
(3,5) -> selecting 1,2 from array1, selecting 5 from array2

算法的复杂性应为O(N * M)

提前致谢

0 个答案:

没有答案