[Solved] Vivaan’s Robot with Java, C++

Vivaan’s Robot: Vivaan is a little scientist as his Dad. Vivaan has designed a small robot to exhibit at the “Young Inventors”, a tech talent show organized at his school.

On the day of the Event, numerous young techies from various schools participated in the show. The judging panel examined each inventions of the techies in various ways to ensure the efficiency. Vivaan’s Robot was also inspected by the Judges wherein the Robot was asked to find all n-bit binary numbers having more 1’s than 0’s for any prefix of the number, by giving a positive integer ‘n’. The Judges were convinced by the solution given by the Robot but they wanted Vivaan to program the Robot to give a more efficient solution for the problem using recursions.

Help Vivaan to write a recursive method to output all n-bit binary numbers having more 1’s than 0’s for any prefix of the number, given a positive integer ‘n’.

Hence create a class named BinaryNumbers with the following method.

Method NameDescription
void printNums(int)                        This recursive method should display the n-bit binary numbers.

Create a driver class called Main. In the Main method, obtain input from the user in the console and call the printNums method present in the BinaryNumbers class.

[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.

All class names, attribute names and method names should be the same as specified in the problem statement.]
Input Format:
First line of the input is a positive integer ‘n’.

Output Format:
Output all the n-bit binary numbers in a single line, separated by a single space.
Refer sample input and output for formatting specifications.

Sample Input 1:
3

Sample Output 1:
111 110 101

Sample Input 2:
2

Sample Output 2:
11 10

Solution

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        printNums(n);
         //sauravhathi
    }
    public static void printNums(int n){
        String str = "";
        printRec(str, 0, n);
    }
    public static void printRec(String number, int extraOnes, int remainingPlaces){
        if(remainingPlaces == 0){
             //sauravhathi
            System.out.print(number + " ");
            return;
        }
        printRec(number + "1", extraOnes + 1, remainingPlaces - 1);
        if(extraOnes > 0)
         //sauravhathi
            printRec(number + "0", extraOnes - 1, remainingPlaces - 1);
    }
}
#include <iostream>
using namespace std;

void printRec(string number, int extraOnes,
              int remainingPlaces)
{

    if (0 == remainingPlaces)
    {
        cout << number << " ";
        //sauravhathi
        return;
    }

    printRec(number + "1", extraOnes + 1,
             remainingPlaces - 1);

    if (0 < extraOnes)
    //sauravhathi
        printRec(number + "0", extraOnes - 1,
                 remainingPlaces - 1);
}

void printNums(int n)
{
    string str = "";
    printRec(str, 0, n);
}

int main()
{
    int n;

    cin>>n;

    printNums(n);
    return 0;
    //sauravhathi
}

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 *