Question: Was a challenge problem in our Java class and solution was not given. Farmer John has N cows (2N105). Each cow has a breed that

Was a challenge problem in our Java class and solution was not given.

Was a challenge problem in our Java class and solution was not

Farmer John has N cows (2N105). Each cow has a breed that is either Guernsey or Holstein. As is often the case, the cows are standing in a line, numbered 1N in this order. Over the course of the day, each cow writes down a list of cows. Specifically, cow i 's list contains the range of cows starting with herself (cow i) up to and including cowEi(iEiN). FJ has recently discovered that each breed of cow has exactly one distinct leader. FJ does not know who the leaders are, but he knows that each leader must have a list that includes all the cows of their breed, or the other breed's leader (or both). Help FJ count the number of pairs of cows that could be leaders. It is guaranteed that there is at least one possible pair. INPUT FORMAT (input arrives from the terminal / stdin): The first line contains N. The second line contains a string of length N, with the i th character denoting the breed of the i th cow (G meaning Guernsey and H meaning Holstein). It is guaranteed that there is at least one Guernsey and one Holstein. The third line contains E1EN. OUTPUT FORMAT (print output to the terminal / stdout): Output the number of possible pairs of leaders. SAMPLE INPUT: 4 GHHG 2434 SAMPLE OUTPUT: 1 The only valid leader pair is (1,2). Cow 1's list contains the other breed's leader (cow 2). Cow 2's list contains all cows of her breed (Holstein). No other pairs are valid. For example, (2,4) is invalid since cow 4 's list does not contain the other breed's leader, and it also does not contain all cows of her breed

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!