This is a verified interview question from Deutsche-bank. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Buckest Arrangment - Deutsche Bank Online Assessment MNNIT Allahabad" covers key patterns like Arrays.
"There are **N buckets** arranged in a row. Each bucket either is empty or contains a ball. The buckets are specified as a string `buckets` consisting of characters `"."` (empty bucket) and `"B"` (bucket with a ball). For example, for `buckets = "B.BB.B..B"`, the row of buckets appears as follows: In one move you can take the ball out of any bucket and place it in another (empty) bucket. Your goal is to arrange the balls to create an alternating sequence of full and empty buckets, in other words, the distance between two consecutive balls should be equal to 2. Note that the sequence may start at any bucket. For example, in the figure below, the balls are placed correctly. Write a function: ```cpp int solution(string &buckets); ``` that, given a string `buckets` of length `N`, returns the minimum number of moves required to create the described sequence. If it is impossible to create a correct sequence, return `-1`. ### Examples **1.** Given `buckets = "..B....B.BB"`, the function should return `2`. In the beginning, the row of buckets appears as follows: First, you can move the ball from bucket **10** to bucket **5**. Second, you can move the ball from bucket **2** to bucket **3**. Now, the balls are in buckets **3, 5, 7 and 9**, creating a correct sequence. --- **2.** Given `buckets = "BB.B.BBB..."`, the function should return `4`. You can move the balls from buckets **1, 3, 5 and 7** to buckets **2, 4, 8 and 10**. --- **3.** Given `buckets = "..BBB.BB"`, the function should return `-1` because it is impossible to create a correct sequence."
Join thousands of developers practicing for Deutsche-bank.