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:
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.
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.
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.)
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;
}

hey bro its not working
ReplyDeletethis has a very simple solution
ReplyDeletetake 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
Thanks for your input...
Delete