Question

In: Computer Science

Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...

Can you please solve this using recursion/ dynamic programming? Any programming language is fine.

Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg.

In case there exist two such numbers which are equally close to 1000 (e.g. 998 and 1002), Wallace will pick the greater one (in this case 1002).

Help Wallace the Weightlifting Walrus and tell it which weight it will have to lift.

Input

The first line of the input contains the number of plates n (1≤n≤1000). Each of the following n lines contains one positive integer less than or equal to 1000, denoting the weight of each plate.

Output

Output one integer, the combined weight closest to 1000.

Sample Input 1 Sample Output 1
4
900
500
498
4
1002
Sample Input 2 Sample Output 2
1
1
1

Solutions

Expert Solution

Solution :

Following is the dynamic programming approach to the problem.

Language is C++. I have written comments to explain it.

#include <bits/stdc++.h>
using namespace std;

//Weight Wallace has to lift
const int V = 1000;
//boolean array to check if Wallace can lift certain sum of weight
bool can[2*V+1];

int main() {
    int n, x;
    cin>>n;
    can[0] = 1;
    int dist = V, ans = 0;
    for (int i = 0; i < n; ++i) {
        cin>>x;
        for (int j = 2 * V - x; j >= 0; --j) {
            if (can[j]) {
                can[j + x] = 1;
                //if new weight is closer to V, update the ans
                if (abs(j + x - V) < dist) {
                    dist = abs(j + x - V);
                    ans = j + x;
                }
                //if value is equally closer, take the larger one
                else if (abs(j + x - V) == dist) {
                    ans = max(ans, j + x);
                }
            }
        }
    }
    cout<<ans;
    return 0;
}

Code demo :

Input 1 :

Output 1 :

Input 2 :

Output 2 :


Related Solutions

Please solve this question in C++ language using recursion. Q (1) Write functions for each of...
Please solve this question in C++ language using recursion. Q (1) Write functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. Write a function that uses recursion to raise a number to a power. The function should take two arguments, the number to be raised to the power (floating point) and the power (a non-negative int).                                                                                       (10 Points) Write a boolean...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Discrete math 1) Using MatLab (any language is fine just say what language is used). function...
Discrete math 1) Using MatLab (any language is fine just say what language is used). function d=lemma_gcd(a,b) %This program will return the greatest common divisor for input variables a %and b where a and b are integers such that they are not both 0. It uses %Lemma 4.8.3 on page 225. %Note: gcd(a,b)=gcd(-a,b)=gcd(a,-b)=gcd(-a,-b), so a=abs(a); b=abs(b); %This following section sets up the arrays we will use to store the %changing values of our variables as a sequence. "c" will be...
3. Solve using probabilistic dynamic programming: I would like to sell my computer to the highest...
3. Solve using probabilistic dynamic programming: I would like to sell my computer to the highest bidder. I have studied the market, and concluded that I am likely to receive three types of offers: an offer of $200 with probability 2/7, and offer of $300 with probability 4/7, and an offer of $400 with probability 1/7. I will advertise my computer for up to three consecutive days. At the end of each of the three days, I will decide whether...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Purpose For this programming project, you will create an application that draws fractal-like shapes using recursion....
Purpose For this programming project, you will create an application that draws fractal-like shapes using recursion. The class that creates the graphics window for the display of the shapes is given to you (see below). What you have to do is create a set of classes for the three shapes that are displayed in the graphics window. Two of the shapes must be a Sierpinski triangle and an H-shape. The third shape is your choice, though it must be a...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
The following two questions can be solved by dynamic programming. For each question, please describe optimal...
The following two questions can be solved by dynamic programming. For each question, please describe optimal substructure and express recurrence relation. Give pseudo-code and analyze time and space complexity of your algorithm. 1. Longest palindrome subsequence A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, “civic”, “racecar”, and “aibohphobia” (fear of palindromes). Design a dynamic programming algorithm to find the longest palindrome that is...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Recursion Discussions Question: Why would you want to use recursion? Concerning programming, when would you want...
Recursion Discussions Question: Why would you want to use recursion? Concerning programming, when would you want to use iterative vs. recursive programming? Where does coding fit in concerning the software process (i.e. its use in design, in coding, etc.)? Describe recursion problems, and why does it seem to fit with recursion? (e.g. nature, math, music, sports game, etc.)  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT