Maxium subset of elements in array within capacity
An unsorted array is filled with random, positive integers. What is the
maximum subset of elements where the total does not exceed the capacity C?
ie: Array [1, 5, 3, 6, 3, 2] where C = 9 | Answer: {1, 2, 3, 3}
Constraint: O(n) time complexity
Hint: Prune & Search and QuickSelect
I've spent quite awhile thinking of how to solve this problem, but the constraint is making this extremely difficult. My assumption is that the array doesn't need to be fully sorted as the hint recommends a combination of prune & search and QuickSelect. QuickSelect is fairly straightforward to find in constant time (using the median method), but I'm not sure how the combination of the two will help to find the answer.
algorithm
add a comment |
An unsorted array is filled with random, positive integers. What is the
maximum subset of elements where the total does not exceed the capacity C?
ie: Array [1, 5, 3, 6, 3, 2] where C = 9 | Answer: {1, 2, 3, 3}
Constraint: O(n) time complexity
Hint: Prune & Search and QuickSelect
I've spent quite awhile thinking of how to solve this problem, but the constraint is making this extremely difficult. My assumption is that the array doesn't need to be fully sorted as the hint recommends a combination of prune & search and QuickSelect. QuickSelect is fairly straightforward to find in constant time (using the median method), but I'm not sure how the combination of the two will help to find the answer.
algorithm
In your given example, Array [1, 5, 3, 6, 3, 2] where C = 8 | Answer: {1, 2, 3, 3}, here (1+2+3+3) > C. Is your example is correct? or by total you mean something else?
– nightfury1204
Nov 14 '18 at 4:59
Whoops, small typo. Fixed!
– G.C
Nov 14 '18 at 5:09
add a comment |
An unsorted array is filled with random, positive integers. What is the
maximum subset of elements where the total does not exceed the capacity C?
ie: Array [1, 5, 3, 6, 3, 2] where C = 9 | Answer: {1, 2, 3, 3}
Constraint: O(n) time complexity
Hint: Prune & Search and QuickSelect
I've spent quite awhile thinking of how to solve this problem, but the constraint is making this extremely difficult. My assumption is that the array doesn't need to be fully sorted as the hint recommends a combination of prune & search and QuickSelect. QuickSelect is fairly straightforward to find in constant time (using the median method), but I'm not sure how the combination of the two will help to find the answer.
algorithm
An unsorted array is filled with random, positive integers. What is the
maximum subset of elements where the total does not exceed the capacity C?
ie: Array [1, 5, 3, 6, 3, 2] where C = 9 | Answer: {1, 2, 3, 3}
Constraint: O(n) time complexity
Hint: Prune & Search and QuickSelect
I've spent quite awhile thinking of how to solve this problem, but the constraint is making this extremely difficult. My assumption is that the array doesn't need to be fully sorted as the hint recommends a combination of prune & search and QuickSelect. QuickSelect is fairly straightforward to find in constant time (using the median method), but I'm not sure how the combination of the two will help to find the answer.
algorithm
algorithm
edited Nov 14 '18 at 5:06
G.C
asked Nov 14 '18 at 4:38
G.CG.C
32
32
In your given example, Array [1, 5, 3, 6, 3, 2] where C = 8 | Answer: {1, 2, 3, 3}, here (1+2+3+3) > C. Is your example is correct? or by total you mean something else?
– nightfury1204
Nov 14 '18 at 4:59
Whoops, small typo. Fixed!
– G.C
Nov 14 '18 at 5:09
add a comment |
In your given example, Array [1, 5, 3, 6, 3, 2] where C = 8 | Answer: {1, 2, 3, 3}, here (1+2+3+3) > C. Is your example is correct? or by total you mean something else?
– nightfury1204
Nov 14 '18 at 4:59
Whoops, small typo. Fixed!
– G.C
Nov 14 '18 at 5:09
In your given example, Array [1, 5, 3, 6, 3, 2] where C = 8 | Answer: {1, 2, 3, 3}, here (1+2+3+3) > C. Is your example is correct? or by total you mean something else?
– nightfury1204
Nov 14 '18 at 4:59
In your given example, Array [1, 5, 3, 6, 3, 2] where C = 8 | Answer: {1, 2, 3, 3}, here (1+2+3+3) > C. Is your example is correct? or by total you mean something else?
– nightfury1204
Nov 14 '18 at 4:59
Whoops, small typo. Fixed!
– G.C
Nov 14 '18 at 5:09
Whoops, small typo. Fixed!
– G.C
Nov 14 '18 at 5:09
add a comment |
1 Answer
1
active
oldest
votes
Question assumes that you need to separate smaller elements until their sum reaches C.
Choose pivot (any method).
Perform partition relative to pivot.
Calculate sum of the left part.
If it is too small (Sum < C), solve the same problem for the right part and value C'=C-Sum recursively
If Sum is too large Sum > C, solve the same problem for the left part.
This is modification of QuickSelect
For good partition scheme you'll have about n + n/2 + n/4 + n/8... +1 = 2*n = O(n) operations of summation (plus partition cost, inherent for Quickselect)
Wouldn't there be an issue if the sum of the left/right side = the capacity C as the question is asking for maximum number of elements that doesn't exceed C (rather than find any combination of elements where the sum = C)?
– G.C
Nov 14 '18 at 5:15
I believe thatsum=Cfor the left part is the perfect solution - left part contains smaller elements, so their number is the largest possible for given sum
– MBo
Nov 14 '18 at 5:32
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293279%2fmaxium-subset-of-elements-in-array-within-capacity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Question assumes that you need to separate smaller elements until their sum reaches C.
Choose pivot (any method).
Perform partition relative to pivot.
Calculate sum of the left part.
If it is too small (Sum < C), solve the same problem for the right part and value C'=C-Sum recursively
If Sum is too large Sum > C, solve the same problem for the left part.
This is modification of QuickSelect
For good partition scheme you'll have about n + n/2 + n/4 + n/8... +1 = 2*n = O(n) operations of summation (plus partition cost, inherent for Quickselect)
Wouldn't there be an issue if the sum of the left/right side = the capacity C as the question is asking for maximum number of elements that doesn't exceed C (rather than find any combination of elements where the sum = C)?
– G.C
Nov 14 '18 at 5:15
I believe thatsum=Cfor the left part is the perfect solution - left part contains smaller elements, so their number is the largest possible for given sum
– MBo
Nov 14 '18 at 5:32
add a comment |
Question assumes that you need to separate smaller elements until their sum reaches C.
Choose pivot (any method).
Perform partition relative to pivot.
Calculate sum of the left part.
If it is too small (Sum < C), solve the same problem for the right part and value C'=C-Sum recursively
If Sum is too large Sum > C, solve the same problem for the left part.
This is modification of QuickSelect
For good partition scheme you'll have about n + n/2 + n/4 + n/8... +1 = 2*n = O(n) operations of summation (plus partition cost, inherent for Quickselect)
Wouldn't there be an issue if the sum of the left/right side = the capacity C as the question is asking for maximum number of elements that doesn't exceed C (rather than find any combination of elements where the sum = C)?
– G.C
Nov 14 '18 at 5:15
I believe thatsum=Cfor the left part is the perfect solution - left part contains smaller elements, so their number is the largest possible for given sum
– MBo
Nov 14 '18 at 5:32
add a comment |
Question assumes that you need to separate smaller elements until their sum reaches C.
Choose pivot (any method).
Perform partition relative to pivot.
Calculate sum of the left part.
If it is too small (Sum < C), solve the same problem for the right part and value C'=C-Sum recursively
If Sum is too large Sum > C, solve the same problem for the left part.
This is modification of QuickSelect
For good partition scheme you'll have about n + n/2 + n/4 + n/8... +1 = 2*n = O(n) operations of summation (plus partition cost, inherent for Quickselect)
Question assumes that you need to separate smaller elements until their sum reaches C.
Choose pivot (any method).
Perform partition relative to pivot.
Calculate sum of the left part.
If it is too small (Sum < C), solve the same problem for the right part and value C'=C-Sum recursively
If Sum is too large Sum > C, solve the same problem for the left part.
This is modification of QuickSelect
For good partition scheme you'll have about n + n/2 + n/4 + n/8... +1 = 2*n = O(n) operations of summation (plus partition cost, inherent for Quickselect)
edited Nov 14 '18 at 5:11
answered Nov 14 '18 at 5:05
MBoMBo
47.8k23050
47.8k23050
Wouldn't there be an issue if the sum of the left/right side = the capacity C as the question is asking for maximum number of elements that doesn't exceed C (rather than find any combination of elements where the sum = C)?
– G.C
Nov 14 '18 at 5:15
I believe thatsum=Cfor the left part is the perfect solution - left part contains smaller elements, so their number is the largest possible for given sum
– MBo
Nov 14 '18 at 5:32
add a comment |
Wouldn't there be an issue if the sum of the left/right side = the capacity C as the question is asking for maximum number of elements that doesn't exceed C (rather than find any combination of elements where the sum = C)?
– G.C
Nov 14 '18 at 5:15
I believe thatsum=Cfor the left part is the perfect solution - left part contains smaller elements, so their number is the largest possible for given sum
– MBo
Nov 14 '18 at 5:32
Wouldn't there be an issue if the sum of the left/right side = the capacity C as the question is asking for maximum number of elements that doesn't exceed C (rather than find any combination of elements where the sum = C)?
– G.C
Nov 14 '18 at 5:15
Wouldn't there be an issue if the sum of the left/right side = the capacity C as the question is asking for maximum number of elements that doesn't exceed C (rather than find any combination of elements where the sum = C)?
– G.C
Nov 14 '18 at 5:15
I believe that
sum=C for the left part is the perfect solution - left part contains smaller elements, so their number is the largest possible for given sum– MBo
Nov 14 '18 at 5:32
I believe that
sum=C for the left part is the perfect solution - left part contains smaller elements, so their number is the largest possible for given sum– MBo
Nov 14 '18 at 5:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293279%2fmaxium-subset-of-elements-in-array-within-capacity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
In your given example, Array [1, 5, 3, 6, 3, 2] where C = 8 | Answer: {1, 2, 3, 3}, here (1+2+3+3) > C. Is your example is correct? or by total you mean something else?
– nightfury1204
Nov 14 '18 at 4:59
Whoops, small typo. Fixed!
– G.C
Nov 14 '18 at 5:09