Do you have a solution for this problem usingJavascript? Please Javascript not Java. Thank you. Jimmy owns
Fantastic news! We've Found the answer you've been seeking!
Question:
Do you have a solution for this problem usingJavascript?
Please Javascript not Java.
Thank you.
Transcribed Image Text:
Jimmy owns a garden in which he has planted N trees in a row. After a few years, the trees have grown up and now they have different heights. Jimmy pays much attention to the aesthetics of his garden. He finds his trees aesthetically pleasing if they alternately increase and decrease in height (..., shorter, taller, shorter, taller, ...). These are examples of aesthetically pleasing trees: MMM... These are examples of trees that are not aesthetically pleasing: Note that two adjacent trees cannot have equal heights. It may turn out that some trees have to be cut out, in order to keep the remaining trees aesthetically pleasing. However, there is a legal restriction that allows a gardener to cut out at most one tree in his possession. In how many ways can Jimmy cut out exactly one tree, so that the remaining ones are aesthetically pleasing? Write a function: function solution(A); that, given an array A consisting of N integers, where A[K] denotes the height of the K-th tree, returns the number of ways of cutting out one tree, so that the remaining trees are aesthetically pleasing. If it is not possible to achieve the desired result, your function should return -1. If the trees are already aesthetically pleasing without any removal, your function should return 0. Examples: 1. Given A = [3, 4, 5, 3, 7], your function should return 3: • You can remove A[0] so the sequence becomes [4, 5, 3, 7]; • You can remove A[1] so the sequence becomes [3, 5, 3, 7]; • You can remove A[2] so the sequence becomes [3, 4, 3, 7]. 2. Given A = 11. 2. 3. 41. your function should return -1, since there is no single tree that Jimmy can cut out that would leave the rest of the trees looking aesthetically pleasing. Enable accessibility mode ,,,,,our function should return 0, since the trees are already aesthetically pleasing and no removal is needed. Assume that: • N is an integer within the range [4..200]; • each element of array A is an integer within the range [1..1,000]. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment. Jimmy owns a garden in which he has planted N trees in a row. After a few years, the trees have grown up and now they have different heights. Jimmy pays much attention to the aesthetics of his garden. He finds his trees aesthetically pleasing if they alternately increase and decrease in height (..., shorter, taller, shorter, taller, ...). These are examples of aesthetically pleasing trees: MMM... These are examples of trees that are not aesthetically pleasing: Note that two adjacent trees cannot have equal heights. It may turn out that some trees have to be cut out, in order to keep the remaining trees aesthetically pleasing. However, there is a legal restriction that allows a gardener to cut out at most one tree in his possession. In how many ways can Jimmy cut out exactly one tree, so that the remaining ones are aesthetically pleasing? Write a function: function solution(A); that, given an array A consisting of N integers, where A[K] denotes the height of the K-th tree, returns the number of ways of cutting out one tree, so that the remaining trees are aesthetically pleasing. If it is not possible to achieve the desired result, your function should return -1. If the trees are already aesthetically pleasing without any removal, your function should return 0. Examples: 1. Given A = [3, 4, 5, 3, 7], your function should return 3: • You can remove A[0] so the sequence becomes [4, 5, 3, 7]; • You can remove A[1] so the sequence becomes [3, 5, 3, 7]; • You can remove A[2] so the sequence becomes [3, 4, 3, 7]. 2. Given A = 11. 2. 3. 41. your function should return -1, since there is no single tree that Jimmy can cut out that would leave the rest of the trees looking aesthetically pleasing. Enable accessibility mode ,,,,,our function should return 0, since the trees are already aesthetically pleasing and no removal is needed. Assume that: • N is an integer within the range [4..200]; • each element of array A is an integer within the range [1..1,000]. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
Do you have a local mobile search app? If so, what is your evaluation of it? If no, why not?
-
You have a solution that contains Cl - ions and another that contains NO 3 - ions. How would adding a solution that contains Ag + ions to these solutions enable you to tell which is which?
-
You have a solution that contains Ag + ions and another that contains Na + ions. How would adding a solution that contains Cl - ions to these solutions enable you to tell which is which?
-
Use a histogram to graph the cumulative frequencies. Use the following information to answer question. Suppose a class of high school seniors had the following distribution of SAT scores in English....
-
How is a firms changing P/E ratio reflected in the stock price? Give examples.
-
Leapin Larrys Pre-Owned Cars has two divisions, Operations and Financing. Operations is responsible for selling Larrys inventory as quickly as possible and purchasing cars for future sale. Financing...
-
Meghann Patrick is a former employee of Altria Group Distribution Company. After her employment was terminated, Patrick sued Altria and a supervisor at Altria, alleging employment-related claims...
-
Parry Company has accounts receivable of $95,400 at March 31, 2012. An analysis of the accounts shows these amounts. Credit terms are 2/10, n/30. At March 31, 2012, there is a $2,100 credit balance...
-
What is the net electric charge of this object? Note for advanced students: you can assume charge is measured in any convenient units, e.g. coulombs or units of e. Write down only the number part of...
-
The production manager at a factory manufacturing four types of light fittings (A, B, C and D) on an automated machine is fixing the schedule for the next week on this machine. HEIJUNKA SCHEDULING OF...
-
Time tickets record use of the following labor for the month. These wages were paid in cash. Job 136 Job 137 $ 12,200 Job 138 Job 139 Job 140 Total direct labor Indirect labor Total labor cost 10,700...
-
What advantage does a hybrid system have over purely functional and hierarchical systems?
-
Which accounting branches are part of the systematic arrangement of management accounting?
-
Describe how decisions on membership fees of associations are made?
-
What characterizes the goals and financing of the four basic types?
-
What are the principle of maximum result and the principle of minimum means?
-
Your friend Jenny says a Roth IRA has the best tax advantages, because you pay taxes now and won't have to pay them when you take the money out in retirement. Why might she be right? 2. Your friend...
-
In Exercises, find the equation of the tangent line at the given point on each curve. 2y 2 - x = 4; (16, 2)
-
You can obtain the pH of a 0.100 M HCl solution by assuming that all of the H3O+ ion comes from the HCl, in which case the pH equals log 0.100 = 1.00. But if you want the pH of a solution that is...
-
List the following compounds in order of increasing acid strength: HBr, H2Se, H2S.
-
Which of the following reactions occur spontaneously as written, with the production of a measurable electric current? a. I 2 + NaBr Br 2 + 2NaI b. Li + NaCl LiCl + Na c. Li + + Na + NaLi d. Ag +...
-
Try drawing up a statement of financial position for Jerry and Company as at 4 March.
-
Assume a business owns a 50 percent stake in a gold mine. As this ownership stake will not give control over the whole of the gold mine, can this resource be regarded as an asset of the business?
-
Does the fact that the level of inventories (and, perhaps, of other factors) may not be typical of their levels at other times in the reporting period pose a problem for external users seeking to...
Study smarter with the SolutionInn App