Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:
After Image
These are examples of trees that are aesthetically pleasing:
After Image 2
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:
class Solution { public int solution(int[] 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 = [1, 2, 3, 4] 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.
3. Given A = [1, 3, 1, 2] your 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.

Step by Step Solution

3.33 Rating (153 Votes )

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probability & Statistics For Engineers & Scientists

Authors: Ronald E. Walpole, Raymond H. Myers, Sharon L. Myers, Keying

7th Edition

9789813131279, 130415294, 9813131276, 978-0130415295

More Books

Students also viewed these Accounting questions

Question

What is the formula to calculate the mth Fibonacci number?

Answered: 1 week ago

Question

Explain the term robust design. Give an example.

Answered: 1 week ago