Implementation: Algorithm for a special distribution Problem











up vote
5
down vote

favorite












We are given a number x, and a set of n coins with denominations v1, v2, …, vn.



The coins are to be divided between Alice and Bob, with the restriction that each person's coins must add up to at least x.



For example, if x = 1, n = 2, and v1 = v2 = 2, then there are two possible distributions: one where Alice gets coin #1 and Bob gets coin #2, and one with the reverse. (These distributions are considered distinct even though both coins have the same denomination.)



I'm interested in counting the possible distributions. I'm pretty sure this can be done in O(nx) time and O(n+x) space using dynamic programming; but I don't see how.










share|improve this question









New contributor




faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • when are 2 distributions different? By values or by coin indexes?
    – juvian
    yesterday










  • @juvian Coin indexes.
    – faegra
    yesterday















up vote
5
down vote

favorite












We are given a number x, and a set of n coins with denominations v1, v2, …, vn.



The coins are to be divided between Alice and Bob, with the restriction that each person's coins must add up to at least x.



For example, if x = 1, n = 2, and v1 = v2 = 2, then there are two possible distributions: one where Alice gets coin #1 and Bob gets coin #2, and one with the reverse. (These distributions are considered distinct even though both coins have the same denomination.)



I'm interested in counting the possible distributions. I'm pretty sure this can be done in O(nx) time and O(n+x) space using dynamic programming; but I don't see how.










share|improve this question









New contributor




faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • when are 2 distributions different? By values or by coin indexes?
    – juvian
    yesterday










  • @juvian Coin indexes.
    – faegra
    yesterday













up vote
5
down vote

favorite









up vote
5
down vote

favorite











We are given a number x, and a set of n coins with denominations v1, v2, …, vn.



The coins are to be divided between Alice and Bob, with the restriction that each person's coins must add up to at least x.



For example, if x = 1, n = 2, and v1 = v2 = 2, then there are two possible distributions: one where Alice gets coin #1 and Bob gets coin #2, and one with the reverse. (These distributions are considered distinct even though both coins have the same denomination.)



I'm interested in counting the possible distributions. I'm pretty sure this can be done in O(nx) time and O(n+x) space using dynamic programming; but I don't see how.










share|improve this question









New contributor




faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











We are given a number x, and a set of n coins with denominations v1, v2, …, vn.



The coins are to be divided between Alice and Bob, with the restriction that each person's coins must add up to at least x.



For example, if x = 1, n = 2, and v1 = v2 = 2, then there are two possible distributions: one where Alice gets coin #1 and Bob gets coin #2, and one with the reverse. (These distributions are considered distinct even though both coins have the same denomination.)



I'm interested in counting the possible distributions. I'm pretty sure this can be done in O(nx) time and O(n+x) space using dynamic programming; but I don't see how.







algorithm






share|improve this question









New contributor




faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 4 hours ago





















New contributor




faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









faegra

284




284




New contributor




faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






faegra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • when are 2 distributions different? By values or by coin indexes?
    – juvian
    yesterday










  • @juvian Coin indexes.
    – faegra
    yesterday


















  • when are 2 distributions different? By values or by coin indexes?
    – juvian
    yesterday










  • @juvian Coin indexes.
    – faegra
    yesterday
















when are 2 distributions different? By values or by coin indexes?
– juvian
yesterday




when are 2 distributions different? By values or by coin indexes?
– juvian
yesterday












@juvian Coin indexes.
– faegra
yesterday




@juvian Coin indexes.
– faegra
yesterday












3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










Count the ways for one person to get just less than x, double it and subtract from the doubled total number of ways to divide the collection in two, (Stirling number of the second kind {n, 2}).



For example,



{2, 3, 3, 5}, x = 5

i matrix
0 2: 1
1 3: 1 (adding to 2 is too much)
2 3: 2
3 N/A (≥ x)

3 ways for one person to get
less than 5.

Total ways to partition a set
of 4 items in 2 is {4, 2} = 7

2 * 7 - 2 * 3 = 8


The Python code below uses MBo's routine. If you like this answer, please consider up-voting that answer.



# Stirling Algorithm
# Cod3d by EXTR3ME
# https://extr3metech.wordpress.com
def stirling(n,k):
n1=n
k1=k
if n<=0:
return 1
elif k<=0:
return 0
elif (n==0 and k==0):
return -1
elif n!=0 and n==k:
return 1
elif n<k:
return 0
else:
temp1=stirling(n1-1,k1)
temp1=k1*temp1
return (k1*(stirling(n1-1,k1)))+stirling(n1-1,k1-1)

def f(coins, x):
a = [1] + (x-1) * [0]

# Code by MBo
# https://stackoverflow.com/a/53418438/2034787
for c in coins:
for i in xrange(x - 1, c - 1, -1):
if a[i - c] > 0:
a[i] = a[i] + a[i - c]

return 2 * (stirling(len(coins), 2) - sum(a) + 1)

print f([2,3,3,5], 5) # 8
print f([1,2,3,4,4], 5) # 16





share|improve this answer























  • Nice catch concerning non-significant nominals!
    – MBo
    14 hours ago












  • How exactly do you count count the ways for one person to get just less than x ?
    – faegra
    13 hours ago










  • @faegra MBo described such a method. Can you think of how to modify it to achieve this?
    – גלעד ברקן
    12 hours ago












  • @faegra added code.
    – גלעד ברקן
    12 hours ago






  • 1




    great answer. I think the uncommon case of sum of elements being < 2x should be handled separately
    – juvian
    3 hours ago


















up vote
3
down vote













If sum of all coins is S, then the first person can get x..S-x of money.



Make array A of length S-x+1 and fill it with numbers of variants of changing A[i] with given coins (like kind of Coin Change problem).



To provide uniqueness (don't count C1+C2 and C2+C1 as two variants), fill array in reverse direction



A[0] = 1
for C in Coins:
for i = S-x downto C:
if A[i - C] > 0:
A[i] = A[i] + A[i - C]
//we can compose value i as i-C and C


then sum A entries in range x..S-x



Example for coins 2, 3, 3, 5 and x=5.
S = 13, S-x = 8



Array state after using coins in order:



0 1 2 3 4 5 6 7 8  //idx
1 1
1 1 1 1
1 1 2 2 1 1
1 1 2 3 1 1 3


So there are 8 variants to distribute these coins. Quick check (3' denotes the second coin 3):



2 3      3' 5
2 3' 3 5
2 3 3' 5
2 5 3 3'
3 3' 2 5
3 5 2 3'
3' 5 2 3
5 2 3 3'





share|improve this answer























  • But that needs O(S-x+1) space instead of O(n+x). Is there a way to improve this? Also, the runtime is bigger than O(nx),
    – faegra
    14 hours ago










  • This approach cannot be improved as-is, but @גלעד ברקן proposed inversion - instead of calculation for 0..S-x interval make calculation for complementary 0..x interval
    – MBo
    13 hours ago


















up vote
3
down vote













You can also solve it in O(A * x^2) time and memory adding memoization to this dp:



solve(A, pos, sum1, sum2):
if (pos == A.length) return sum1 == x && sum2 == x
return solve(A, pos + 1, min(sum1 + A[pos], x), sum2) +
solve(A, pos + 1, sum1, min(sum2 + A[pos], x))

print(solve(A, 0, 0, 0))


So depending if x^2 < sum or not you could use this or the answer provided by @Mbo (in terms of time complexity). If you care more about space, this is better only when A * x^2 < sum - x






share|improve this answer



















  • 1




    UV for alternative. Seems the last argument is missed in the first recursive call. And is ==x correct here?
    – MBo
    yesterday












  • It's ==x because we never have values over x. Code was wrong using max instead of min, fixed ^^ @MBo
    – juvian
    yesterday










  • @juvian What are the dimensions fo A?
    – faegra
    yesterday










  • @faegra A in the code is the array of coins, in the complexity is the amount of coins, a bit of notation abuse ^^
    – juvian
    yesterday










  • @juvian I am a bit confused. Where exactly is the solution displayed? At the end sum1 == x && sum2 == x will output but this is a boolean and not the solution?
    – faegra
    yesterday











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






faegra is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416924%2fimplementation-algorithm-for-a-special-distribution-problem%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










Count the ways for one person to get just less than x, double it and subtract from the doubled total number of ways to divide the collection in two, (Stirling number of the second kind {n, 2}).



For example,



{2, 3, 3, 5}, x = 5

i matrix
0 2: 1
1 3: 1 (adding to 2 is too much)
2 3: 2
3 N/A (≥ x)

3 ways for one person to get
less than 5.

Total ways to partition a set
of 4 items in 2 is {4, 2} = 7

2 * 7 - 2 * 3 = 8


The Python code below uses MBo's routine. If you like this answer, please consider up-voting that answer.



# Stirling Algorithm
# Cod3d by EXTR3ME
# https://extr3metech.wordpress.com
def stirling(n,k):
n1=n
k1=k
if n<=0:
return 1
elif k<=0:
return 0
elif (n==0 and k==0):
return -1
elif n!=0 and n==k:
return 1
elif n<k:
return 0
else:
temp1=stirling(n1-1,k1)
temp1=k1*temp1
return (k1*(stirling(n1-1,k1)))+stirling(n1-1,k1-1)

def f(coins, x):
a = [1] + (x-1) * [0]

# Code by MBo
# https://stackoverflow.com/a/53418438/2034787
for c in coins:
for i in xrange(x - 1, c - 1, -1):
if a[i - c] > 0:
a[i] = a[i] + a[i - c]

return 2 * (stirling(len(coins), 2) - sum(a) + 1)

print f([2,3,3,5], 5) # 8
print f([1,2,3,4,4], 5) # 16





share|improve this answer























  • Nice catch concerning non-significant nominals!
    – MBo
    14 hours ago












  • How exactly do you count count the ways for one person to get just less than x ?
    – faegra
    13 hours ago










  • @faegra MBo described such a method. Can you think of how to modify it to achieve this?
    – גלעד ברקן
    12 hours ago












  • @faegra added code.
    – גלעד ברקן
    12 hours ago






  • 1




    great answer. I think the uncommon case of sum of elements being < 2x should be handled separately
    – juvian
    3 hours ago















up vote
4
down vote



accepted










Count the ways for one person to get just less than x, double it and subtract from the doubled total number of ways to divide the collection in two, (Stirling number of the second kind {n, 2}).



For example,



{2, 3, 3, 5}, x = 5

i matrix
0 2: 1
1 3: 1 (adding to 2 is too much)
2 3: 2
3 N/A (≥ x)

3 ways for one person to get
less than 5.

Total ways to partition a set
of 4 items in 2 is {4, 2} = 7

2 * 7 - 2 * 3 = 8


The Python code below uses MBo's routine. If you like this answer, please consider up-voting that answer.



# Stirling Algorithm
# Cod3d by EXTR3ME
# https://extr3metech.wordpress.com
def stirling(n,k):
n1=n
k1=k
if n<=0:
return 1
elif k<=0:
return 0
elif (n==0 and k==0):
return -1
elif n!=0 and n==k:
return 1
elif n<k:
return 0
else:
temp1=stirling(n1-1,k1)
temp1=k1*temp1
return (k1*(stirling(n1-1,k1)))+stirling(n1-1,k1-1)

def f(coins, x):
a = [1] + (x-1) * [0]

# Code by MBo
# https://stackoverflow.com/a/53418438/2034787
for c in coins:
for i in xrange(x - 1, c - 1, -1):
if a[i - c] > 0:
a[i] = a[i] + a[i - c]

return 2 * (stirling(len(coins), 2) - sum(a) + 1)

print f([2,3,3,5], 5) # 8
print f([1,2,3,4,4], 5) # 16





share|improve this answer























  • Nice catch concerning non-significant nominals!
    – MBo
    14 hours ago












  • How exactly do you count count the ways for one person to get just less than x ?
    – faegra
    13 hours ago










  • @faegra MBo described such a method. Can you think of how to modify it to achieve this?
    – גלעד ברקן
    12 hours ago












  • @faegra added code.
    – גלעד ברקן
    12 hours ago






  • 1




    great answer. I think the uncommon case of sum of elements being < 2x should be handled separately
    – juvian
    3 hours ago













up vote
4
down vote



accepted







up vote
4
down vote



accepted






Count the ways for one person to get just less than x, double it and subtract from the doubled total number of ways to divide the collection in two, (Stirling number of the second kind {n, 2}).



For example,



{2, 3, 3, 5}, x = 5

i matrix
0 2: 1
1 3: 1 (adding to 2 is too much)
2 3: 2
3 N/A (≥ x)

3 ways for one person to get
less than 5.

Total ways to partition a set
of 4 items in 2 is {4, 2} = 7

2 * 7 - 2 * 3 = 8


The Python code below uses MBo's routine. If you like this answer, please consider up-voting that answer.



# Stirling Algorithm
# Cod3d by EXTR3ME
# https://extr3metech.wordpress.com
def stirling(n,k):
n1=n
k1=k
if n<=0:
return 1
elif k<=0:
return 0
elif (n==0 and k==0):
return -1
elif n!=0 and n==k:
return 1
elif n<k:
return 0
else:
temp1=stirling(n1-1,k1)
temp1=k1*temp1
return (k1*(stirling(n1-1,k1)))+stirling(n1-1,k1-1)

def f(coins, x):
a = [1] + (x-1) * [0]

# Code by MBo
# https://stackoverflow.com/a/53418438/2034787
for c in coins:
for i in xrange(x - 1, c - 1, -1):
if a[i - c] > 0:
a[i] = a[i] + a[i - c]

return 2 * (stirling(len(coins), 2) - sum(a) + 1)

print f([2,3,3,5], 5) # 8
print f([1,2,3,4,4], 5) # 16





share|improve this answer














Count the ways for one person to get just less than x, double it and subtract from the doubled total number of ways to divide the collection in two, (Stirling number of the second kind {n, 2}).



For example,



{2, 3, 3, 5}, x = 5

i matrix
0 2: 1
1 3: 1 (adding to 2 is too much)
2 3: 2
3 N/A (≥ x)

3 ways for one person to get
less than 5.

Total ways to partition a set
of 4 items in 2 is {4, 2} = 7

2 * 7 - 2 * 3 = 8


The Python code below uses MBo's routine. If you like this answer, please consider up-voting that answer.



# Stirling Algorithm
# Cod3d by EXTR3ME
# https://extr3metech.wordpress.com
def stirling(n,k):
n1=n
k1=k
if n<=0:
return 1
elif k<=0:
return 0
elif (n==0 and k==0):
return -1
elif n!=0 and n==k:
return 1
elif n<k:
return 0
else:
temp1=stirling(n1-1,k1)
temp1=k1*temp1
return (k1*(stirling(n1-1,k1)))+stirling(n1-1,k1-1)

def f(coins, x):
a = [1] + (x-1) * [0]

# Code by MBo
# https://stackoverflow.com/a/53418438/2034787
for c in coins:
for i in xrange(x - 1, c - 1, -1):
if a[i - c] > 0:
a[i] = a[i] + a[i - c]

return 2 * (stirling(len(coins), 2) - sum(a) + 1)

print f([2,3,3,5], 5) # 8
print f([1,2,3,4,4], 5) # 16






share|improve this answer














share|improve this answer



share|improve this answer








edited 12 hours ago

























answered 15 hours ago









גלעד ברקן

11.9k21439




11.9k21439












  • Nice catch concerning non-significant nominals!
    – MBo
    14 hours ago












  • How exactly do you count count the ways for one person to get just less than x ?
    – faegra
    13 hours ago










  • @faegra MBo described such a method. Can you think of how to modify it to achieve this?
    – גלעד ברקן
    12 hours ago












  • @faegra added code.
    – גלעד ברקן
    12 hours ago






  • 1




    great answer. I think the uncommon case of sum of elements being < 2x should be handled separately
    – juvian
    3 hours ago


















  • Nice catch concerning non-significant nominals!
    – MBo
    14 hours ago












  • How exactly do you count count the ways for one person to get just less than x ?
    – faegra
    13 hours ago










  • @faegra MBo described such a method. Can you think of how to modify it to achieve this?
    – גלעד ברקן
    12 hours ago












  • @faegra added code.
    – גלעד ברקן
    12 hours ago






  • 1




    great answer. I think the uncommon case of sum of elements being < 2x should be handled separately
    – juvian
    3 hours ago
















Nice catch concerning non-significant nominals!
– MBo
14 hours ago






Nice catch concerning non-significant nominals!
– MBo
14 hours ago














How exactly do you count count the ways for one person to get just less than x ?
– faegra
13 hours ago




How exactly do you count count the ways for one person to get just less than x ?
– faegra
13 hours ago












@faegra MBo described such a method. Can you think of how to modify it to achieve this?
– גלעד ברקן
12 hours ago






@faegra MBo described such a method. Can you think of how to modify it to achieve this?
– גלעד ברקן
12 hours ago














@faegra added code.
– גלעד ברקן
12 hours ago




@faegra added code.
– גלעד ברקן
12 hours ago




1




1




great answer. I think the uncommon case of sum of elements being < 2x should be handled separately
– juvian
3 hours ago




great answer. I think the uncommon case of sum of elements being < 2x should be handled separately
– juvian
3 hours ago












up vote
3
down vote













If sum of all coins is S, then the first person can get x..S-x of money.



Make array A of length S-x+1 and fill it with numbers of variants of changing A[i] with given coins (like kind of Coin Change problem).



To provide uniqueness (don't count C1+C2 and C2+C1 as two variants), fill array in reverse direction



A[0] = 1
for C in Coins:
for i = S-x downto C:
if A[i - C] > 0:
A[i] = A[i] + A[i - C]
//we can compose value i as i-C and C


then sum A entries in range x..S-x



Example for coins 2, 3, 3, 5 and x=5.
S = 13, S-x = 8



Array state after using coins in order:



0 1 2 3 4 5 6 7 8  //idx
1 1
1 1 1 1
1 1 2 2 1 1
1 1 2 3 1 1 3


So there are 8 variants to distribute these coins. Quick check (3' denotes the second coin 3):



2 3      3' 5
2 3' 3 5
2 3 3' 5
2 5 3 3'
3 3' 2 5
3 5 2 3'
3' 5 2 3
5 2 3 3'





share|improve this answer























  • But that needs O(S-x+1) space instead of O(n+x). Is there a way to improve this? Also, the runtime is bigger than O(nx),
    – faegra
    14 hours ago










  • This approach cannot be improved as-is, but @גלעד ברקן proposed inversion - instead of calculation for 0..S-x interval make calculation for complementary 0..x interval
    – MBo
    13 hours ago















up vote
3
down vote













If sum of all coins is S, then the first person can get x..S-x of money.



Make array A of length S-x+1 and fill it with numbers of variants of changing A[i] with given coins (like kind of Coin Change problem).



To provide uniqueness (don't count C1+C2 and C2+C1 as two variants), fill array in reverse direction



A[0] = 1
for C in Coins:
for i = S-x downto C:
if A[i - C] > 0:
A[i] = A[i] + A[i - C]
//we can compose value i as i-C and C


then sum A entries in range x..S-x



Example for coins 2, 3, 3, 5 and x=5.
S = 13, S-x = 8



Array state after using coins in order:



0 1 2 3 4 5 6 7 8  //idx
1 1
1 1 1 1
1 1 2 2 1 1
1 1 2 3 1 1 3


So there are 8 variants to distribute these coins. Quick check (3' denotes the second coin 3):



2 3      3' 5
2 3' 3 5
2 3 3' 5
2 5 3 3'
3 3' 2 5
3 5 2 3'
3' 5 2 3
5 2 3 3'





share|improve this answer























  • But that needs O(S-x+1) space instead of O(n+x). Is there a way to improve this? Also, the runtime is bigger than O(nx),
    – faegra
    14 hours ago










  • This approach cannot be improved as-is, but @גלעד ברקן proposed inversion - instead of calculation for 0..S-x interval make calculation for complementary 0..x interval
    – MBo
    13 hours ago













up vote
3
down vote










up vote
3
down vote









If sum of all coins is S, then the first person can get x..S-x of money.



Make array A of length S-x+1 and fill it with numbers of variants of changing A[i] with given coins (like kind of Coin Change problem).



To provide uniqueness (don't count C1+C2 and C2+C1 as two variants), fill array in reverse direction



A[0] = 1
for C in Coins:
for i = S-x downto C:
if A[i - C] > 0:
A[i] = A[i] + A[i - C]
//we can compose value i as i-C and C


then sum A entries in range x..S-x



Example for coins 2, 3, 3, 5 and x=5.
S = 13, S-x = 8



Array state after using coins in order:



0 1 2 3 4 5 6 7 8  //idx
1 1
1 1 1 1
1 1 2 2 1 1
1 1 2 3 1 1 3


So there are 8 variants to distribute these coins. Quick check (3' denotes the second coin 3):



2 3      3' 5
2 3' 3 5
2 3 3' 5
2 5 3 3'
3 3' 2 5
3 5 2 3'
3' 5 2 3
5 2 3 3'





share|improve this answer














If sum of all coins is S, then the first person can get x..S-x of money.



Make array A of length S-x+1 and fill it with numbers of variants of changing A[i] with given coins (like kind of Coin Change problem).



To provide uniqueness (don't count C1+C2 and C2+C1 as two variants), fill array in reverse direction



A[0] = 1
for C in Coins:
for i = S-x downto C:
if A[i - C] > 0:
A[i] = A[i] + A[i - C]
//we can compose value i as i-C and C


then sum A entries in range x..S-x



Example for coins 2, 3, 3, 5 and x=5.
S = 13, S-x = 8



Array state after using coins in order:



0 1 2 3 4 5 6 7 8  //idx
1 1
1 1 1 1
1 1 2 2 1 1
1 1 2 3 1 1 3


So there are 8 variants to distribute these coins. Quick check (3' denotes the second coin 3):



2 3      3' 5
2 3' 3 5
2 3 3' 5
2 5 3 3'
3 3' 2 5
3 5 2 3'
3' 5 2 3
5 2 3 3'






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









MBo

45.4k22847




45.4k22847












  • But that needs O(S-x+1) space instead of O(n+x). Is there a way to improve this? Also, the runtime is bigger than O(nx),
    – faegra
    14 hours ago










  • This approach cannot be improved as-is, but @גלעד ברקן proposed inversion - instead of calculation for 0..S-x interval make calculation for complementary 0..x interval
    – MBo
    13 hours ago


















  • But that needs O(S-x+1) space instead of O(n+x). Is there a way to improve this? Also, the runtime is bigger than O(nx),
    – faegra
    14 hours ago










  • This approach cannot be improved as-is, but @גלעד ברקן proposed inversion - instead of calculation for 0..S-x interval make calculation for complementary 0..x interval
    – MBo
    13 hours ago
















But that needs O(S-x+1) space instead of O(n+x). Is there a way to improve this? Also, the runtime is bigger than O(nx),
– faegra
14 hours ago




But that needs O(S-x+1) space instead of O(n+x). Is there a way to improve this? Also, the runtime is bigger than O(nx),
– faegra
14 hours ago












This approach cannot be improved as-is, but @גלעד ברקן proposed inversion - instead of calculation for 0..S-x interval make calculation for complementary 0..x interval
– MBo
13 hours ago




This approach cannot be improved as-is, but @גלעד ברקן proposed inversion - instead of calculation for 0..S-x interval make calculation for complementary 0..x interval
– MBo
13 hours ago










up vote
3
down vote













You can also solve it in O(A * x^2) time and memory adding memoization to this dp:



solve(A, pos, sum1, sum2):
if (pos == A.length) return sum1 == x && sum2 == x
return solve(A, pos + 1, min(sum1 + A[pos], x), sum2) +
solve(A, pos + 1, sum1, min(sum2 + A[pos], x))

print(solve(A, 0, 0, 0))


So depending if x^2 < sum or not you could use this or the answer provided by @Mbo (in terms of time complexity). If you care more about space, this is better only when A * x^2 < sum - x






share|improve this answer



















  • 1




    UV for alternative. Seems the last argument is missed in the first recursive call. And is ==x correct here?
    – MBo
    yesterday












  • It's ==x because we never have values over x. Code was wrong using max instead of min, fixed ^^ @MBo
    – juvian
    yesterday










  • @juvian What are the dimensions fo A?
    – faegra
    yesterday










  • @faegra A in the code is the array of coins, in the complexity is the amount of coins, a bit of notation abuse ^^
    – juvian
    yesterday










  • @juvian I am a bit confused. Where exactly is the solution displayed? At the end sum1 == x && sum2 == x will output but this is a boolean and not the solution?
    – faegra
    yesterday















up vote
3
down vote













You can also solve it in O(A * x^2) time and memory adding memoization to this dp:



solve(A, pos, sum1, sum2):
if (pos == A.length) return sum1 == x && sum2 == x
return solve(A, pos + 1, min(sum1 + A[pos], x), sum2) +
solve(A, pos + 1, sum1, min(sum2 + A[pos], x))

print(solve(A, 0, 0, 0))


So depending if x^2 < sum or not you could use this or the answer provided by @Mbo (in terms of time complexity). If you care more about space, this is better only when A * x^2 < sum - x






share|improve this answer



















  • 1




    UV for alternative. Seems the last argument is missed in the first recursive call. And is ==x correct here?
    – MBo
    yesterday












  • It's ==x because we never have values over x. Code was wrong using max instead of min, fixed ^^ @MBo
    – juvian
    yesterday










  • @juvian What are the dimensions fo A?
    – faegra
    yesterday










  • @faegra A in the code is the array of coins, in the complexity is the amount of coins, a bit of notation abuse ^^
    – juvian
    yesterday










  • @juvian I am a bit confused. Where exactly is the solution displayed? At the end sum1 == x && sum2 == x will output but this is a boolean and not the solution?
    – faegra
    yesterday













up vote
3
down vote










up vote
3
down vote









You can also solve it in O(A * x^2) time and memory adding memoization to this dp:



solve(A, pos, sum1, sum2):
if (pos == A.length) return sum1 == x && sum2 == x
return solve(A, pos + 1, min(sum1 + A[pos], x), sum2) +
solve(A, pos + 1, sum1, min(sum2 + A[pos], x))

print(solve(A, 0, 0, 0))


So depending if x^2 < sum or not you could use this or the answer provided by @Mbo (in terms of time complexity). If you care more about space, this is better only when A * x^2 < sum - x






share|improve this answer














You can also solve it in O(A * x^2) time and memory adding memoization to this dp:



solve(A, pos, sum1, sum2):
if (pos == A.length) return sum1 == x && sum2 == x
return solve(A, pos + 1, min(sum1 + A[pos], x), sum2) +
solve(A, pos + 1, sum1, min(sum2 + A[pos], x))

print(solve(A, 0, 0, 0))


So depending if x^2 < sum or not you could use this or the answer provided by @Mbo (in terms of time complexity). If you care more about space, this is better only when A * x^2 < sum - x







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









juvian

13k21926




13k21926








  • 1




    UV for alternative. Seems the last argument is missed in the first recursive call. And is ==x correct here?
    – MBo
    yesterday












  • It's ==x because we never have values over x. Code was wrong using max instead of min, fixed ^^ @MBo
    – juvian
    yesterday










  • @juvian What are the dimensions fo A?
    – faegra
    yesterday










  • @faegra A in the code is the array of coins, in the complexity is the amount of coins, a bit of notation abuse ^^
    – juvian
    yesterday










  • @juvian I am a bit confused. Where exactly is the solution displayed? At the end sum1 == x && sum2 == x will output but this is a boolean and not the solution?
    – faegra
    yesterday














  • 1




    UV for alternative. Seems the last argument is missed in the first recursive call. And is ==x correct here?
    – MBo
    yesterday












  • It's ==x because we never have values over x. Code was wrong using max instead of min, fixed ^^ @MBo
    – juvian
    yesterday










  • @juvian What are the dimensions fo A?
    – faegra
    yesterday










  • @faegra A in the code is the array of coins, in the complexity is the amount of coins, a bit of notation abuse ^^
    – juvian
    yesterday










  • @juvian I am a bit confused. Where exactly is the solution displayed? At the end sum1 == x && sum2 == x will output but this is a boolean and not the solution?
    – faegra
    yesterday








1




1




UV for alternative. Seems the last argument is missed in the first recursive call. And is ==x correct here?
– MBo
yesterday






UV for alternative. Seems the last argument is missed in the first recursive call. And is ==x correct here?
– MBo
yesterday














It's ==x because we never have values over x. Code was wrong using max instead of min, fixed ^^ @MBo
– juvian
yesterday




It's ==x because we never have values over x. Code was wrong using max instead of min, fixed ^^ @MBo
– juvian
yesterday












@juvian What are the dimensions fo A?
– faegra
yesterday




@juvian What are the dimensions fo A?
– faegra
yesterday












@faegra A in the code is the array of coins, in the complexity is the amount of coins, a bit of notation abuse ^^
– juvian
yesterday




@faegra A in the code is the array of coins, in the complexity is the amount of coins, a bit of notation abuse ^^
– juvian
yesterday












@juvian I am a bit confused. Where exactly is the solution displayed? At the end sum1 == x && sum2 == x will output but this is a boolean and not the solution?
– faegra
yesterday




@juvian I am a bit confused. Where exactly is the solution displayed? At the end sum1 == x && sum2 == x will output but this is a boolean and not the solution?
– faegra
yesterday










faegra is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















faegra is a new contributor. Be nice, and check out our Code of Conduct.













faegra is a new contributor. Be nice, and check out our Code of Conduct.












faegra is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416924%2fimplementation-algorithm-for-a-special-distribution-problem%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)