Question

Please write code in C++ and comment out code Question: We have to paint n boards...

Please write code in C++ and comment out code

Question:

We have to paint n boards of length {A1, A2, .. An}. There are k painters available and each takes 1 unit time to paint 1 unit of board. The problem is to find the minimum time to get this job done under the constraints that any painter will only paint continuous sections of boards, say board {2, 3, 4} or only board {1} or nothing but not board {2, 4, 5}.

Examples :

Input : k = 2, A = {10, 10, 10, 10}

Output : 20.

Here we can divide the boards into 2

equal sized partitions, so each painter

gets 20 units of board and the total

time taken is 20.

Input : k = 2, A = {10, 20, 30, 40}

Output : 60.

Here we can divide first 3 boards for

one painter and the last board for

second painter.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

int sumBetween2Points(int i, int j, int arr[]){
int total =0;
for(int k=i;k<=j;k++){
total = total + arr[k];
}
return k;
}


int arrayDivision(int n, int arr[],k){
int best = INT_MAX;

if(k==1){
sumBetween2Points(0,n,arr);
}

if(n==1){
return arr[0];
}

for(int i=1;i<=n;i++){
best = min(best,max(arrayDivision(i,arr,k-1),sumBetween2Points(i,n-1,arr)));
}

return best;

}

int main(){
int arr[] = {10,20,30,40};
int n = sizeof(arr);
int k =2;
cout << arrayDivision(n,arr,k);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please write code in C++ and comment out code Question: We have to paint n boards...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the assembly code that will transform a squared image into a negative print. (HARD-CODED OUTPUT...

    Write the assembly code that will transform a squared image into a negative print. (HARD-CODED OUTPUT WILL RESULT IN ZERO.) That is, every pixel whose unsigned value is 0 should change to 255, 1 should change to 254, 2 should change to 253, ... 255 should change to 0. $a0 contains the address of the input buffer, $a1 contains the address of the output buffer address, and $a2 contains the image dimension. Look at the below image for your reference:...

  • Syntax checker. We will write C code to check to see if an input string can be derived from a gra...

    Syntax checker. We will write C code to check to see if an input string can be derived from a grammar. The grammar is: A ::= 0 B B ::= 1 A | 2 B | ; Your program "check.c" should output "yes" if the line of standard input can be derived from this grammar, and "no" otherwise. Here are some examples: $ ./check 0 ; yes $ ./check 0 2 1 ; no $ The input values will always...

  • Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231...

    Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231 Output: 0 This sequence also does not have a majority element (note that the element 1 appears twice and hence is not a majority element). Problem Introduction Majority rule is a decision rule that selects the alternative which has a majority, that is, more than half the votes. Given a sequence of elements (1.02....,On, you would like to check whether it contains an element...

  • please code in basic c++ program Write a program that uses the following formula: n (ax...

    please code in basic c++ program Write a program that uses the following formula: n (ax - ib) i=1 Your program will prompt the user for the number of iterations for the calculation. input values for n, x, a, b, and c. n must be a positive integer, but x, a, b, and c can be any real numbers. Then it will perform the calculation and output the result. Then, it will ask the user if they would like to...

  • C++ please use only easy code and stdio.h because im just a beginner Description Write the...

    C++ please use only easy code and stdio.h because im just a beginner Description Write the program that can manage the information of students as follows: S 1. This program has two phase; the input phase and the output phase. 2. In an input phase, a teacher inputs the information (name, score, and department) for each student. The total number of students who can be inputted should be defined as a NUM OF_STUDENT constant value. In this example, the value...

  • Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps....

    Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps. This is a basic course and the furthest we have gone is BASIC arrays. Write the code for the following problems using arrays. Write what would go in the “int main()” part of the program. For these problems, the entire program and program output is not needed. Just include what goes in the int main() part of the program. 1. Declare an integer array...

  • I need help writing this C code. Here is the description: Let's say we have a...

    I need help writing this C code. Here is the description: Let's say we have a program called smart typing which does two things: The first letter of a word is always input manually by a keystroke. When a sequence of n letters has been typed: c1c2...cn and the set of words in the dictionary that start with that sequence also have c1c2...cnc then the smart typing displays c automatically. we would like to know,  for each word in the dictionary,...

  • Please code in java There are N employees already working for a company and M new...

    Please code in java There are N employees already working for a company and M new candidates eligible for the work. At the end of the financial year, each of the N employees demand for an increment. You are provided with the data of current_salary and the salary he/she expects. Also, there are M candidates we can recruit. We have the data of salary demanded by each eligible candidate. The company can spend maximum of X units of money. Now...

  • the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++...

    the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...

  • Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc"....

    Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT