[Solved] Nella’s Birthday and Colorfull Balloons with Java, C++, Python

Nella’s Birthday and Colorfull Balloons: Nella is an eight-year-old princess who will inherit the kingdom of Castlehaven. It is her birthday today and her Dad, the King of Castlehaven has arranged for a grand party. Nella loves colorful balloons and so her Dad planned to decorate the entire palace with balloons of the colors that Nella loved. So he asked her about her color preferences. The sophisticated princess, that is Nella, likes only two colors — amber and brass. Her Dad bought balloons, each of which was either amber or brass in color.

You are provided this information in a string s consisting of characters ‘a‘ and ‘b‘ only, where ‘a‘ denotes that the balloon is amber, where ‘b‘ denotes it being brass colored. When Nella saw the balloons, she was furious with anger as she wanted all the balloons of the same color. In her rage, she painted some of the balloons with the opposite color (i.e., she painted some amber ones brass and vice versa) to make all balloons appear to be the same color.

It took her a lot of time to do this, but you can probably show her the right way of doing so, thereby teaching her a lesson to remain calm in difficult situations, by finding out the minimum number of balloons needed to be painted in order to make all of them the same color.

Input Format:
The first and only line of input contains a string s.

Output Format:
Output a single line containing an integer — the minimum number of flips required.
Refer sample input and output for formatting specifications.

Sample Input1:
ab
Sample Output1:
1

Sample Input2:
baaba
Sample Output2:
2

Solution

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        int aCount=0,bCount=0;
        for(int i=0;i<s.length();i++)
        {
            //sauravhathi
            if(s.charAt(i)=='a')
            aCount++;
            else if(s.charAt(i)=='b')
            bCount++;
        }
        if(aCount<=bCount)
          //sauravhathi
        System.out.println(aCount);
        else
        System.out.println(bCount);
    }
}
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    string s1;
    cin >> s1;
    int aCount = 0, bCount = 0;
    //sauravhathi
    for (int i = 0; i < strlen(s1.c_str()); i++)
    {
        if (s1[i] == 'a')
            aCount++;
        else if (s1[i] == 'b')
        // sauravhathi
            bCount++;
    }
    if (aCount <= bCount)
        cout << aCount;
    else
        cout << bCount;
    return 0;
}
s1 = input();
aCount = 0, bCount = 0
for i in range(len(s1)):
    if s1[i] == 'a':
        aCount = aCount + 1
    else:
        bCount = bCount + 1
if aCount <= bCount
    print(aCount)
else:
    print(bCount)

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 *