Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5578 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3412 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 714 |

@Manikanta punnam | 579 |

@sriramgoparaju99 | 532 |

@yashkaril4 | 517 |

@praveenbgp6 | 517 |

@prankursharma31 | 489 |

@rdakka | 465 |

@sonamnigam1999 | 441 |

@purohitmn02 | 427 |

@sonamkumari63928 | 426 |

@ShubhankarPandey | 419 |

Complete Leaderboard |

You are given weights and values of **N** items, put these items in a knapsack of capacity **W** to get the maximum total value in the knapsack. Note that we have only **one quantity of each item**.

In other words, given two integer arrays **val[0..N-1]** and **wt[0..N-1]** which represent values and weights associated with **N** items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of **val[]** such that sum of the weights of this subset is smaller than or equal to **W.** You cannot break an item, **either pick the complete item, or don’t pick it (0-1 property)**.

**Example 1:**

Input:N = 3 W = 4 values[] = {1,2,3} weight[] = {4,5,1}Output:3

**Example 2:**

Input:N = 3 W = 3 values[] = {1,2,3} weight[] = {4,5,6}Output:0

**Your Task:**

Complete the function **knapSack()** which takes maximum capacity W, weight array wt[], value array val[] and number of items n as a parameter and returns the **maximum possible** value you can get.

**Expected Time Complexity:** O(N*W).

**Expected Auxiliary Space:** O(N*W)

**Constraints:**

1 ≤ N ≤ 1000

1 ≤ W ≤ 1000

1 ≤ wt[i] ≤ 1000

1 ≤ v[i] ≤ 1000

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
0 - 1 Knapsack Problem

...