An array of size N is given and a value K. You have to find the minimum subset size so that subset sum is exactly equal to K, if not print -1. 0 < K, a[i], N < 10^6.

Before contest

Codeforces Round 940 (Div. 2) and CodeCraft-23

2 days

Register now »

Codeforces Round 940 (Div. 2) and CodeCraft-23

2 days

Register now »

*has extra registration

# | User | Rating |
---|---|---|

1 | ecnerwala | 3648 |

2 | Benq | 3580 |

3 | orzdevinwang | 3570 |

4 | cnnfls_csy | 3569 |

5 | Geothermal | 3568 |

6 | tourist | 3565 |

7 | maroonrk | 3530 |

8 | Radewoosh | 3520 |

9 | Um_nik | 3481 |

10 | jiangly | 3467 |

# | User | Contrib. |
---|---|---|

1 | maomao90 | 174 |

2 | awoo | 164 |

2 | adamant | 164 |

4 | TheScrasse | 159 |

4 | nor | 159 |

6 | maroonrk | 156 |

7 | -is-this-fft- | 150 |

8 | SecondThread | 147 |

9 | orz | 146 |

10 | pajenegod | 145 |

An array of size N is given and a value K. You have to find the minimum subset size so that subset sum is exactly equal to K, if not print -1. 0 < K, a[i], N < 10^6.

↑

↓

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Apr/19/2024 15:14:55 (g1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Isn't this the famous coin change problem? You can easily find the solution online.

Sorry, can you explain how?

In problem which mentioned in post constraints for $$$N, K, a_i$$$ to high for stupid knapsack.

Other solution with sqrt opt works $$$O(n * log(max(a_i)) * sqrt(K))$$$, thats too much.

This problem came in Colortoken placement exam. Can you please tell me how can it be solved?

Can you say the time limit, and you sure that constraint is 10^6?

![ ]()

The sample output were: 2 and -1

Are you sure that they ask about finding subset?You tried submit solution for subsegment?

I think so too. Since the output for the second case is -1 they are probably asking for a subsegment (even if that is not clear in the statement). This could be solved in O(n) by using two pointers as well, which explains the constraints.

nice statement awesome task

Could you elaborate on this sqrt optmization (or provide links)? Depending on what it is maybe the solution is using it with bitset

Or can't we do fft here? Binary search on no of boxes. Complexity will be $$$O(N*logN*log(max a_i))$$$

How do you check that you can collect this amount or not by FFT with fixed amount of items?

I mean Like we do "Fast subset transform" in a similar way if possible lets say $$$s$$$ will be the smallest size of some subset such that it's sum is $$$K$$$. then polynomial raise to power $$$s$$$ will contains non-zero coefficient for $$$x^K$$$ term right.

I encountered a similar problem recently, can someone suggest some solution:

Given an array A of size N, find the number of subsets of size exactly K that add up to S.

e.g:

explanation (0 based indexing)

I don't remember the constraints, but let's say 1 <= N,S <= 1000

and would it really be possible to solve under normal time-limits i.e. 1 or 2 sec if N,S were 10^5?

I dont know anything better than $$$O(NS)$$$. You can submit it here https://www.geeksforgeeks.org/count-of-subsets-with-sum-equal-to-x/

you missed out on the point

Subsets of length exactly Kwith sum S. The link only talks about subsets with sum S which is quite a standard problem.So you got the answer? if yes can you please share.