multiplication using fenwick tree - multiplication

We can do updation in a fenwick tree like adding a value and multiplication by a value. I have the following code for adding a value x to the element at position l .
while(l <= n-1)
{
tree[l] = tree[l] + x;
l = l + (l&(-l));
}
Similarly I want to perform the multiplication operation.I am not getting how to do it. Any help is appreciable.

Compute the difference between the old and new node values, then use the addition logic to add that difference to the node value.

Related

How to solve mixed differential equations? Or how to assign the dPsdt = o in first iteration and it will get value after iterations

Error generates like this: UnboundLocalError: local variable 'dPsdt' referenced before assignment
dL1dt = (m_si*(h1-hf) + pi*di*alphai_1*L1*(Tm1-Ts1) - d13*dPsdt)/d11
dL2dt = (m_si*hf-m_so*hg + pi*di*alphai_2*L2*(Tm2-Ts2)- d21 * dL1dt -d23*dPsdt-
d24*dhodt)/d22
dPsdt = (m_so*(hg-ho) + pi*di*alphai_3*L3*(Tm3-Ts3)-d31*dL1dt-d32*dL2dt -
d34*dhodt)/d33
dhodt = (m_si - m_so -(d41*dL1dt) - (d42*dL2dt) - (d43*dPsdt))/d44
dzdt = [dL1dt, dL2dt, dPsdt, dhodt, dTm1dt, dTm2dt, dTm3dt, dTp1dt, dTp2dt, dTp3dt]
return dzdt
It seems that your derivatives are implicitly defined by some linear system
A*dxdt = b
which you try to solve via Gauss-Seidel iteration. This you have to actually implement as iteration, that is, several passes over the system of equations. Note that you need a convergence condition like diagonal dominance so that that works at all.
But for these small dimensions you can be faster and more exact by using
dxdt = nump.linalg.solve(A,b)

Calculating time complexity using master method

Can anyone explain the time complexity of the below using the master method?
int sum(Node node) {
if (node == null) {
return 0;
}
return sum(node.left) + node.value + sum(node.right);
}
I know a's value is 2 but its hard for me to identify b and d. Is b=1 and d=cO(n)? In that case can anyone explain how b and d should be identified
well, to make the recurrence relation less complicated, we can assume a balanced binary tree that has 2^inodes, so we obtain a recurrence of T(n) = 2T(n/2) + 1 (ignoring the base case).
From above, we can find a = 2, b = 2, and c = 0, since 1 is O(1). Applying the master method, it passes case 1 and we can get our complexity as T(n) = Θ(nlog22) or O(n)
This is a function used to sum up all nodes in a binary tree. First down from root to leave and then goes up (stack unwinding). So, time complexity is O(N), as it needs to visit each node at least once.

Find all pairs of consecutive numbers in BST

I need to write a code that will find all pairs of consecutive numbers in BST.
For example: let's take the BST T with key 9, T.left.key = 8, T.right.key = 19. There is only one pair - (8, 9).
The naive solution that I thought about is to do any traversal (pre, in, post) on the BST and for each node to find its successor and predecessor, and if one or two of them are consecutive to the node - we'll print them. But the problem is that it'll will the O(n^2), because we have n nodes and for each one of them we use function that takes O(h), that in the worst case h ~ n.
Second solution is to copy all the elements to an array, and to find the consecutive numbers in the array. Here we use O(n) additional space, but the runtime is better - O(n).
Can you help me to find an efficient algorithm to do it? I'm trying to think about algorithm that don't use additional space, and its runtime is better than O(n^2)
*The required output is the number of those pairs (No need to print the pairs).
*any 2 consecutive integers in the BST is a pair.
*The BST containts only integers.
Thank you!
Why don't you just do an inorder traversal and count pairs on the fly? You'll need a global variable to keep track of the last number, and you'll need to initialize it to something which is not one less than the first number (e.g. the root of the tree). I mean:
// Last item
int last;
// Recursive function for in-order traversal
int countPairs (whichever_type treeRoot)
{
int r = 0; // Return value
if (treeRoot.leftChild != null)
r = r + countPairs (treeRoot.leftChild);
if (treeRoot.value == last + 1)
r = r + 1;
last = treeRoot.value;
if (treeRoot.rightChild != null)
r = r + countPairs (treeRoot.rightChild);
return r; // Edit 2016-03-02: This line was missing
}
// Main function
main (whichever_type treeRoot)
{
int r;
if (treeRoot == null)
r = 0;
else
{
last = treeRoot.value; // to make sure this is not one less than the lowest element
r = countPairs (treeRoot);
}
// Done. Now the variable r contains the result
}

Riemann Sum Estimation

I'm trying to calculate the value of n that solves the problem below. I am not exactly sure where I am messing up. I tried using a do while loop also, but I am having trouble figuring out the logic error. Could anyone assist?
If S = √ (6*( 1+1/2^2+1/3^2 +1/4^2 + 1/5^2 + ... ) ) = (pi^2)/6, after how many terms will the sum be equal to PI to 6 decimal places. PI to 6 decimal places is 3.141592. The relevant part of my code is shown below:
double s = 0;
for(int n=1;abs(sqrt(6*s) - 3.141592) >= pow(10,-6);n++) {
s += (1/(pow(n,2)));
NSLog(#"%i",n);
}
int abs(int i)
computes the absolute value of an integer. Therefore in
abs(sqrt(6*s) - 3.141592)
the floating point number sqrt(6*s) - 3.141592 is converted to an int
first, which gives zero as soon as the absolute value of this number is less than one.
You want to use fabs() instead.

NTL Galois Field elements iterate

I'm learning NTL and I have a doubt: How will I be able to get any specific element of any finite field?
Here is my code
GF2X P = BuildIrred_GF2X(256);
GF2E::init(P);
GF2E zero = GF2E::zero();
GF2E one;
GF2E r = random_GF2E(); //I want change the function random_GF2E()
I want change the function random_GF2E() by any other to get a specific element.
The Elements of a finite field with 2256 elements are represented as the polynomials f of deg(f) < 256.
If you want a special element, you can declare a polynom p by something like
GF2X p;
p.SetLength(n);
SetCoeff(p,i,1);
There is deg(p) = n. If n < deg(P) (in your case n < 256), then this is a special element of the finite field. If n >= deg(P) you can reduce it modulo P by conv<GF2E>(p).
I hope this is what you were looking for.