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 ...

Java program to create staircase

Observe that its base and height are both equal to  , and the image is drawn using  #  symbols and spaces.  The last line is not preceded by any spaces. Write a program that prints a staircase of size  . Input Format A single integer,  , denoting the size of the staircase. Output Format Print a staircase of size   using  #  symbols and spaces. Note : The last line must have   spaces in it. package com.rohan.test; import java.util.Scanner; public class StaircaseTest {     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int n = in.nextInt();                  for(int i = 0; i< n; i++) {         for(int k = 0; k < n; k++) {         if(k < n-i-1)         System.out.p...

Java program to print the maximum hourglass value of the matrix

Context   Given a    2D Array ,  : 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 We define an hourglass in   to be a subset of values with indices falling in this pattern in  's graphical representation: a b c d e f g There are   hourglasses in  , and an  hourglass sum  is the sum of an hourglass' values. Task   Calculate the hourglass sum for every hourglass in  , then print the  maximum  hourglass sum. Note:  If you have already solved the Java domain's  Java 2D Array  challenge, you may wish to skip this challenge. Input Format There are   lines of input, where each line contains   space-separated integers describing  2D Array   ; every value in   will be in the inclusive range of   to  . Constraints Output Format Print the largest (maximum) hourglass sum f...