Equal Number Of Boys And Girls

Moderate
0/80
Average time to solve is 35m
profile
Contributed by
15 upvotes
Asked in company
Amazon

Problem statement

There are ‘N’ students standing in a row. Students are comprised of both girls and boys. Kevin is their teacher, who wants to pick a group of students that have an equal number of boys and girls. Kevin does not want to disturb the whole row and so, he only wants to pick students that are adjacent to each other. Formally, he can only pick a subarray, not a subsequence.

A complete row is given as an array of characters (as a string 'S'), you have to find the length of the longest such subarray which follows the above criteria.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain a single integer ‘N’ which represents the number of students in the row.

The second line of each test case contains a string ‘S’ of length ‘N’ which is comprised of two characters ‘G’ and ‘B’ where ‘B’ denotes Boy and ‘G’ denotes the Girl.
Output Format:
For each test case, print the length of the longest possible subarray that  Kevin pick so that students are adjacent to each other.

Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10000
S[ i ] =  ‘B’ or ‘G’

Where S[i] denotes the ‘i-th’ character of the given string ‘s’.

Time limit: 1 sec
Sample Input 1:
2
4
BGGB
5
BBBGB  
Sample Output 1:
4
2
Explanation For Sample Input 1:
In the first test case, Kevin can choose all students as there is exactly the same number of Boys and Girls in the row.

In the second test case, possible subarrays are “BG” and “GB”.
Sample Input 2:
2
1
B
2
GG
Sample Output 2:
0
0
Explanation For Sample Input 2:
In the first test case, there is no way to pick the required subarray.

In the second test case, there is no way to pick the required subarray.
Hint

Can you think of going through each subarray?

Approaches (3)
Brute Force

The basic idea of this approach is to loop through each subarray and find the length of the longest subarray which satisfies the property mentioned in the problem statement.

 

The steps are as follows:

 

  1. Create a variable to store the answer (say, “ans”) and Initialise it with 0.
  2. Iterate through ‘s’ (say, iterator = ‘i’)
    • Iterate again through ‘s’ from ‘i’ to end of ‘s’ (say, iterator = ‘j’)
      • Iterate through ‘s’ from ‘i’ to ‘j’ and check if it has an equal number of boys and girls.
      • If it has, then check if it is also longest or not by comparing its length with “ans”. If true then store its length into “ans”.
  3. Return “ans”.
Time Complexity

O(N^3), where ‘N’ is the number of students in the row.

 

Since we are using three loops one inside the other and, so the overall time complexity will be O(N^3).

Space Complexity

O(1).

 

Since we are not using any extra space, the overall space complexity will be O(1).

Code Solution
(100% EXP penalty)
Equal Number Of Boys And Girls
Full screen
Console