Skip to main content

Nuclear Fission Online Test Program

In one of the online java programming test, I have been asked this question to solve. I tried my best and come up with the solution below. This can be improve a lot to work much better with large data set. Please do let me know if found useful or it can be improve in certain way to write a very very efficient code:

Question:

Nuclear fission is a process in which a large nucleus splits into two smaller nuclei with the release of energy.
The process involves releasing of three neutrons from each split and which further splits more atoms until the fuel gets finished.
Now let us consider a very efficient nuclear fission reaction. You have n number of atoms. The reaction starts with one external neutron.
The neutron hits the first atom and causes three neutrons to split. These hit the other large nuclei and so on. Find the total number of neutrons
generated at the end of the reaction.
Examples
Let the number of atoms in the fuel be 7. Look at the diagram below:



We see that there are 15 neutrons generated at the end of the reaction. So the required output is 15.
Input Format
The first line of input consists of an integer t. This is the number of test cases.
Each test case contains only single integer. This represents the number of large nucleii in the fuel.
Output Format
For each test case, output a single integer that is the number of neutrons generated at the last step.
Constraints
0 < t < 100000 (This is the number of test cases)
0 < n < 1000000000000000 (These are the initial number of atoms in the fuel.)
Sample input:
2
7
22
Sample Output:
15
45
Program below:



       
        private TernaryNode root;
       
        public boolean isEmpty() {
                return (this.root == null);
        }
        public void insert(BigInteger data) {
                        if(root == null) {
                        this.root = new TernaryNode(data);
                        return;
                }
                insertNode(this.root, data);
       
        }
       
        private TernaryNode insertNode(TernaryNode root, BigInteger data) {
                TernaryNode tmpNode = null;
                Queue<TernaryNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
                TernaryNode node = queue.poll();
                if(node.getLeft() == null && node.getMiddle() == null && node.getRight() == null) {
                        node.setLeft(new TernaryNode(data));
                        return node.getLeft();
                }
                if(node.getLeft() != null && node.getMiddle() == null && node.getRight() == null) {
                        node.setMiddle(new TernaryNode(data));
                        return node.getMiddle();
                }
                if(node.getLeft() != null && node.getMiddle() != null && node.getRight() == null) {
                        node.setRight(new TernaryNode(data));
                        return node.getRight();
                }
                if(node.getLeft() == null && node.getMiddle() == null && node.getRight() == null) {
                        tmpNode = node;
                }
                queue.add(node.getLeft());
                queue.add(node.getMiddle());
                queue.add(node.getRight());
        }
        tmpNode.setLeft(new TernaryNode(data));
        return tmpNode.getLeft();              
        }
       
       
        public BigInteger levelOrderTraversal() {
                Queue<TernaryNode> discoverNode = new LinkedList<>();
                BigInteger count = BigInteger.ZERO;
                if(this.root == null) {
                        return BigInteger.ZERO;
                }
                discoverNode.add(this.root);
                while(!discoverNode.isEmpty()) {
                        TernaryNode tmpNode = discoverNode.remove();
                        if(tmpNode.getLeft() != null) { discoverNode.add(tmpNode.getLeft()); }
                        else {
                                count = count.add(count.ONE);
                        }
                        if(tmpNode.getMiddle() != null) { discoverNode.add(tmpNode.getMiddle()); }
                        else {
                                count = count.add(count.ONE);
                        }
                        if(tmpNode.getRight() != null) { discoverNode.add(tmpNode.getRight()); }
                        else {
                                count = count.add(count.ONE);
                        }
                }
                return count;
        }




Comments

  1. hey bro its not working

    ReplyDelete
  2. this has a very simple solution
    take input for t
    run a while loop for t not equal to 0
    inside the loop take two variables say a and b
    cin a
    b = (a*2)+1;
    cout b
    t = t-1;
    this code works fine

    ReplyDelete

Post a Comment

Popular posts from this blog

Struts2 and Hibernate Example using Annotation

Hi Guys, Today we are going to create an example using Struts2 and Hibernate using Annotation. For the database, we are going to use MySQL. This example will register a record for a user in the mysql database, which later will be used to login to the application. First of all we need to create our database table in MySQL. Log into your mysql database and type the following command to create a database in the mysql prompt. create database mydb; Use the above created database to create table. use mydb; Now we need to create a database table named "users". create table users( uid int primary key auto_increment, uname char(15), password char(20), email char(20), phone long, city char(15)); Now to create this application, we are going to use eclipse. Open your eclipse and create a dynamic web project. Put the following jars related to Struts2 and Hibernate in the WEB-INF/lib folder Now create the following packages in the src folder for the ...

Single LindedList in C. Program to create, insert, delete, traverse in Single Linked List.

For creating the LinkedList program I have used Linux(ubuntu 12.04) machine. When I was in my college days, I would use the Tourbo C compiler which was easy to use in Windows Type the following command to install the build-essential which create our environment and C  compiler to build and run C programs. This will be enough for our purpose to create C programs. $ sudo apt−get install build−essential SinglyLinkedList.c ----------------------- #include <stdio.h> #include <stdlib.h> /* Linded List is having an element and a pointer which points to the same type of structure. I have type defined a NODE type and given alias as "node". */ typedef struct NODE { int info; struct NODE *next; }node; /* I have created a boolean type which returns 0, 1 and 2 which returns false, true and done respectively. This is basically for return type to be used in the program to return boolean values. */ typedef enum {false, true, done}boolean; /* Now crea...

N-Queens Problem

N-Queens Problem N- queen problem states that if no of queens are placed on a chessboard, then no two queens can attack each other. The following program states the same: package com.backtracking.test; public class QueenProblem { private int[][] chessTable; private int numOfQueens; public QueenProblem(int numOfQueens) { this.chessTable = new int[numOfQueens][numOfQueens]; this.numOfQueens = numOfQueens; } public void solve() { if (setQueens(0)) { printQueens(); } else { System.out.println("There is no solution.."); } } private boolean setQueens(int colIndex) { if (colIndex == numOfQueens) { return true; } for (int rowIndex = 0; rowIndex < numOfQueens; ++rowIndex) { if (isPlaceValid(rowIndex, colIndex)) { chessTable[rowIndex][colIndex] = 1; // Queeen is placed if (setQueens(colIndex + 1)) { return true; } // BACKTRACKING chessTable[rowIndex][colI...