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...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot use for or while loops to solve the problems as well as not using global variables. 1. Create a function that takes a positive integer and returns it with neighboring digits removed. Do not convert the integer to a list. Ex. Input = [5555537777721] Output = [53721]
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...
Please use the Scheme programming language with Dr. Racket to solve a and b below. (Use...
Please use the Scheme programming language with Dr. Racket to solve a and b below. (Use Scheme and not Python) Write before–in–list?, which takes a list and two elements of the list. It should return #t if the second argument appears in the list argument before the third argument: > (before–in–list? '(back in the ussr) 'in 'ussr) #T > (before–in–list? '(back in the ussr) 'the 'back) #F The procedure should also return #f if either of the supposed elements doesn't...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT