Question: Java In a garden there are N fruit trees growing in a row. Each tree produces one fruit of some type. Little Amy goes to

Java

Java In a garden there are N fruit trees growing in a

In a garden there are N fruit trees growing in a row. Each tree produces one fruit of some type. Little Amy goes to the garden to collect some fruit. She has two baskets that can hold any number of fruit, but she only wants to put one type of fruit in each basket. When Amy enters the garden, she goes to some tree, collects the fruit from it (every tree produces exactly one fruit) and then goes to the next tree to the right. Amy continues her walk until she encounters a tree with a fruit that she cannot put in any of her baskets (each of them already has a fruit ofa different type), or when the garden ends. Amy never throws any fruit away from her basket. What is the maximal number of fruit she can collect? Write a function: class Solution public int solution (intl] A): > that, given an array A of N integers, representing consecutive types of fruit in the garden, returns the maximal number of fruit Amy can collect. For example, given A [1, 2,1,3, 4, 3,5, 1,2] the function should return 3. Amy can collect fruit of types 1 and 2 from the beginning of the garden or all fruits of types 3 and 4. Given A [1, 2, 1,2,1,2, 1] the function should return 7. Amy can collect all the fruit from the garden. Write an efficient algorithm for the following assumptions: is an integer within the range [1..100,000]; each element of array A is an integer within the range [0.1,000,000,000]. N

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!