Fruits and Baskets
There are ‘n’ fruit trees that are planted along a road. The trees are numbered from 0 to n-1. The type of fruit each tree bears is represented by an uppercase character of the English alphabet. A Ninja is walking along that road. He has two baskets and wants to put the maximum number of fruits in them. The restriction is that each basket can have only one type of fruit.
Ninja can start with any tree and end at any tree, but once he has started he cannot skip a tree i.e if he picks fruit from the tree ‘i’, then he has to pick fruit from tree ‘i+1’ before going to the tree ‘i+2’. He will pick one fruit from each tree until he cannot, i.e, he will stop when he has to pick a fruit of the third type because both the baskets can be filled by only two different fruits.
You are given a string ‘str’ consisting of the ‘n’ uppercase character of the English alphabet. The ‘i’th character in this string represents the type of fruit tree ‘i’ bears. Return the maximum number of fruits ninja can put in both the baskets after satisfying all the given conditions.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.
The first line of each test case contains an integer ‘n’ representing the number of trees.
The second line of the test case contains a string consisting of the ‘n’ uppercase character of the English alphabet that represents the type of fruit in each tree.
Output format :
For each test case, output in a separate line, the maximum number of fruits Ninja can put in both the baskets after satisfying all the given conditions.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= n <= 10^4
Where ‘T’ is the total number of test cases, ‘N’ represents the number of trees.
Time limit: 1 sec
AnswerBot
1y
The problem is to find the maximum number of fruits the Ninja can put in both baskets after satisfying given conditions.
The Ninja can start at any tree and end at any tree, but cannot skip a tree.
He c...read more
Help your peers!
Add answer anonymously...
Top Quantiphi Analytics Solutions Private Limited Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Quantiphi Analytics Solutions Private Limited Software Developer
>
Quantiphi Analytics Solutions Private Limited Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app