[Solved] Balls for Challenge with Java, C++, Python

Balls for Challenge: The Circoloco Children Carnival is the City’s largest and successful event dedicated to children and families. The main focus at the carnival is the workshop arena where kids can participate in educational activities.

Charlie, a little boy accompanied by his Mom visited the fair, where he participated at the “Balls for Challenge” activity.  He was given many balls of white and black colors. During the play, he arranged the balls into two rows both consisting of N number of balls. These two rows of balls are given to you in the form of strings X, Y. Both these string consist of ‘W‘ and ‘B‘, where ‘W‘ denotes a white-colored ball and ‘B‘ a black colored.

Other than these two rows of balls, Charlie has an infinite supply of extra balls of each color. He wants to create another row of N balls, Z in such a way that the sum of Hamming distance between X and Z, and hamming distance between Y and Z is maximized.

Hamming Distance between two strings X and Y is defined as the number of positions where the color of balls in row X differs from the row Y ball at that position. e.g. Hamming distance between “WBB”, “BWB” is 2, as, at positions 1 and 2, corresponding colors in the two strings differ. As there can be multiple such arrangements of row Z, Charlie wants you to find the lexicographically smallest arrangement which will maximize the above value.

Input Format:
The first line of the input will contain a string X denoting the arrangement of balls in the first row.
The second line of the input will contain the string Y denoting the arrangement of balls in second row.

Output Format:
Output a single line containing the string of length N denoting the arrangement of colors of the balls belonging to row Z.
Refer to sample input and output for formatting specifications.

Sample Input 1:
WBWB
WBBB
Sample Output 1:
BWBW

Sample Input 2:
BBBW
BWBB
Sample Output 2:
WBWB

Solution

import java.util.Scanner;
import static java.lang.Math.*;
public
class Main
{
public
    static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String x = in.next();
        String y = in.next();
        int n = x.length();
        String z = "";
        for (int i = 0; i < n; i++)
        {
            //sauravhathi
            char it;
            if (x.charAt(i) == y.charAt(i))
            {
                //sauravhathi
                if (x.charAt(i) == 'W')
                    z += 'B';
                else if (x.charAt(i) == 'B')
                    z += 'W';
            }
            else
            {
                z += 'B';
            }
        }
        System.out.println(z);
    }
}
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int main()
{
    string x, y;
    cin >> x >> y;
    long long int n = x.size();
    string z = "";
    // sauravhathi
    for (long long int i = 0; i < n; i++)
    {
        char it;
        if (x[i] == y[i])
        {
            if (x[i] == 'W')
                z += 'B';
            // sauravhathi
            else if (x[i] == 'B')
                z += 'W';
        }
        else
        {
            z += 'B';
        }
    }
    cout << z << "\n";

    return 0;
}
x = input()
y = input()
n = len(x)
z = ""
for i in range(n):
    if x[i] == y[i]:
        if x[i] == 'W':
            z += 'B'
        else:
            z += 'W'
    else:
        z += 'B'
print(z)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *