1. Member
Join Date
Aug 2014
Posts
27
Rep Power
0

## maximum toys

plz any one help me out i have an assignment wil u plz help me tommorow is its last submission date . so plz.....
here it is....jst tell me the logic how can it b solved,,,

mark and jane are very happy having their first kid.their son is fond of toys so mark wants to buy some their are N different toys in front of him ,having tagged their price.but he has only \$k he wants to maximize the toys he buys with this money now you are marks best friend and have to help him buy as many as passible.

solve the above problem with following constraint.

buy maximum toys with maximum money left

note. a toy cannt b bought multiple times but multiple toys can hav sam price.

example
input.
mark has money:50
available toys:{1,12,5,111,1000,200,10,9,6,7,4}

output
buy maximum no. of toys with maximum money left:
output- {7,8}
explanation:
maximum no. of toys =7 {1,5,10,9,6,7,4}
saving =8

2. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: maximum toys

How would you do this by hand, on paper?

3. ## Re: maximum toys

kind regards,

Jos

4. Member
Join Date
Aug 2014
Posts
27
Rep Power
0

## Re: maximum toys

here is a code of above problem can u plz solve it error is main class not found
candidatecode.candidatecode class wasn't found in candidatecode project...
Java Code:
```import java.util.Scanner;
import java.util.Arrays;
public class Candidatecode {

public static int[] getmaxtoysithmaxsaving(int money,int[] toys)
{
Scanner input=new Scanner(System.in);
int n;
n=input.nextInt();
money=input.nextInt();
toys=new int[n];
for(int count=1;count<=n;++count)
toys[count-1]=input.nextInt();
Arrays.sort(toys);
for(int count=1;count<=n;++count)
{
money-=toys[count];
if(money<0)
break;
}
}
}```
Last edited by simar; 11-09-2014 at 01:33 PM. Reason: added [code] ... [/code] tags

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: maximum toys

I must be missing something here because this problem seems trivial as stated. The term "knapsack problem" seems to imply a more complicated solution (akin to bin stacking).

Regards,
Jim

6. ## Re: maximum toys

Originally Posted by jim829
I must be missing something here because this problem seems trivial as stated. The term "knapsack problem" seems to imply a more complicated solution (akin to bin stacking).
You're not missing anything; the OP is missing the point: his/her algorithm simply collects as many of the cheapest toys as possible (which is not a knapsack solution to the problem).

kind regards,

Jos

edit: I just noticed: there is one toy for each type and the aim of the game is to collect as many toys as possible; this reduces the problem to the solution given by the OP.
Last edited by JosAH; 11-08-2014 at 06:01 PM.

7. Member
Join Date
Aug 2014
Posts
27
Rep Power
0

## Re: maximum toys

can u plz help me to solve it error is main class not found
candidatecode.candidatecode class wasn't found in candidatecode project

8. ## Re: maximum toys

Java is case sensitive. Check that the class name's spelling matches the class name used with the java command.

9. ## Re: maximum toys

Originally Posted by simar
can u plz help me to solve it error is main class not found
candidatecode.candidatecode class wasn't found in candidatecode project
You named your class Candidatecode, not candidatecode; the name of the source file should be the same.

kind regards,

Jos

10. ## Re: maximum toys

Originally Posted by simar
can u plz ...
Can you please not write like a semi-educated twit?

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•