If two nodes and root of tree are give to me. How to find the common ancestor of two nodes?
You can follow this approach
While traversing Binary Search Tree from top to bottom, the first node n we encounter with value between n1 and n2, i.e., n1 < n < n2 is the Lowest or Least Common Ancestor(LCA) of n1 and n2 (where n1 < n2). So just traverse the BST in pre-order, if you find a node with value in between n1 and n2 then n is the LCA, if it's value is greater than both n1 and n2 then our LCA lies on left side of the node, if it's value is smaller than both n1 and n2 then LCA lies on right side.
Its a commonly asked programming question in interview you could have easily found its solution on interview sites
Hi here's a python implementation that I found useful for LCA :-
class BST(object):
def __init__(self, val=None, right=None, left=None):
self.val = val
self.right = right
self.left = left
return None
def __nonzero__(self):
return self.val is not None
def insert(self, item):
if not self:
self.val = item
elif item < self.val:
if self.left:
return self.left.insert(item)
else:
self.left = BST(val=item)
elif item > self.val:
if self.right:
return self.right.insert(item)
else:
self.right = BST(val=item)
return None
def lca(self, m, n):
if n < self.val:
return self.left.lca(m, n)
elif m > self.val:
return self.right.lca(m, n)
else:
return self.val
if __name__ == "__main__":
b = BST()
for k in [8, 3, 10, 1, 6, 14, 4, 7, 13]:
b.insert(k)
for pair in [(4, 7), (4, 10), (1, 4), (1, 3), (3, 6)]:
print b.lca(*pair)
Related
We have an unsorted array size of n-1, that has the numbers from 1 to n excluding one number. We need to find that missing number.
Like if we have as input [8,1,5,4,2,7,6], then it should return 3.
Constraints I got:
Try to collect smaller patient numbers to the left and bigger numbers to the right. In your solution, you have to use the Partition algorithm that is used by QuickSort (solutions that do not use partitioning and recursion, such as subtracting the sum of the elements in A from the sum of the integers up-to n will not be a valid answer).
How is this possible?
This problem can be solved with an algorithm that is much like quickselect:
The idea is to partition the input array like with quickselect (and quicksort). At that point we know the pivot element appears where it would be when the array would have been sorted. There are two possibilities:
The value of the pivot is 2 more than the index where it resides. For instance if we have [1, 3, 4] and the middle element is the pivot, then indeed the value 3 is two more than the index (1) where it resides.
In this case we can conclude that the missing value is less than the pivot value, and we could repeat the process recursively in the left partition
The value of the pivot is 1 more than the index where it resides. For instance if we have [1, 2, 4] and the middle element is the pivot, then indeed the value 2 is one more than the index (1) where it resides.
In this case we can conclude that the missing value is greater than the pivot value, and we could repeat the process recursively in the right partition
Here is an implementation of that algorithm in Python code:
from random import randint
def partition(arr, l, r):
i = randint(l, r) # choose pivot randomly
arr[i], arr[r] = arr[r], arr[i] # swap it out of the way
x = arr[r] # get pivot value
i = l
for j in range(l, r):
if arr[j] <= x:
arr[i], arr[j] = arr[j], arr[i] # swap
i += 1
# swap pivot element into its final place
arr[i], arr[r] = arr[r], arr[i]
return i
def findmissing(arr, l, r):
if l > r: # base case
return arr[r] + 1 if r >= 0 else 1
pivotindex = partition(arr, l, r)
if arr[pivotindex] == pivotindex + 2: # missing element must be on the left
return findmissing(arr, l, pivotindex - 1)
else: # missing element is on the right
return findmissing(arr, pivotindex + 1, r)
# Demo run
arr = [8,1,5,4,2,7,6]
print(findmissing(arr, 0, len(arr)-1)) # 3
I have an array A along with 3 variables k, x and y.
I have to find number of unordered pairs (i,j) such that the sum of two elements mod k equals x and the product of the same two elements mod k is equal to y. Pairs need not be distinct. In other words, the number of (i,j) so that
(A[i]+A[j])%k == x and (A[i]*A[j])%k == y where 0 <= i < j < size of A.
For example, let A={1,2,3,2,1}, k=2, x=1, y=0. Then the answer is 6, because the pairs are: (1,2), (1,2), (2,3), (2,1), (3,2), and (2,1).
I used a brute force approach, but obviously this is not acceptable.
Modulo-arithmetic has the following two rules:
((a mod k) * (b mod k)) mod k = (a * b) mod k
((a mod k) + (b mod k)) mod k = (a + b) mod k
Thus we can sort all values into a hashtable with separate chaining and k buckets.
Addition
Find m < k, such that for a given n < k: (n + m) mod k = x.
There is exactly one solution to this problem:
if n < x: m < x must hold. Thus m = x - n
if n == x: m = 0
if n > x: we need to find m such that n + m = x + k. Thus m = x + k - n
This way, we can easily determine for each list of values the corresponding values such that for any pair (a, b) of the crossproduct of the two lists (a + b) mod k = x holds.
Multiplication
Multiplication is a bit trickier. Luckily we've already been given the matching congruence-class for addition (see above), which must as well be the matching congruence-class for the multiplication, since both constraints need to hold. To verify that the given congruence-class matches, we only need to check that (n * m) mod k = y (n and m defined as above). If this expression holds, we can build pairs, otherwise no matching elements exist.
Implementation
This would be the working python-code for the above example:
def modmuladd(ls, x, y, k):
result = []
# create tuples of indices and values
indices = zip(ls, range(0, len(ls)))
# split up into congruence classes
congruence_cls = [[] for i in range(0, k)]
for p in indices:
congruence_cls[p[0] % k].append(p)
for n in range(0, k):
# congruence class to match addition
if n < x:
m = x - n
elif n == x:
m = 0
else:
m = x + k - n
# check if congruence class matches for multiplication
if (n * m) % k != y or len(congruence_cls[m]) == 0:
continue # no matching congruence class
# add matching tuple to result
result += [(a, b) for a in congruence_cls[n] for b in congruence_cls[m] if a[1] <= b[1]]
result += [(a, b) for a in congruence_cls[m] for b in congruence_cls[n] if a[1] <= b[1]]
# sort result such according to indices of first and second element, remove duplicates
sorted_res = sorted(sorted(set(result), key=lambda p: p[1][1]), key=lambda p: p[0][1])
# remove indices from result-set
return [(p[0][0], p[1][0]) for p in sorted_res]
Note that sorting and elimination of duplicates is only required since this code concentrates on the usage of congruence-classes than perfect optimization. This example can be easily tweaked to provided ordering without the sorting by minor modifications.
Test run
print(modmuladd([1, 2, 3, 2, 1], 1, 0, 2))
Output:
[(1, 2), (1, 2), (2, 3), (2, 1), (3, 2), (2, 1)]
EDIT:
Worst-case complexity of this algorithm is still O(n^2), due to the fact that building all possible pairs of a list of size n is O(n^2). With this algorithm however the search for matching pairs can be cut down to O(k) with O(n) preprocessing. Thus counting resulting pairs can be done in O(n) with this approach. Assuming the numbers are distributed equally over the congruence-classes, this algorithm could build all pairs that are part of the solution-set in O(n^2/k^2).
EDIT 2:
An implementation that only counts would work like this:
def modmuladdct(ls, x, y, k):
result = 0
# split up into congruence classes
congruence_class = {}
for v in ls:
if v % k not in congruence_class:
congruence_class[(v % k)] = [v]
else:
congruence_class[v % k].append(v)
for n in congruence_class.keys():
# congruence class to match addition
m = (x - n + k) % k
# check if congruence class matches for multiplication
if (n * m % k != y) or len(congruence_class[m]) == 0:
continue # no matching congruence class
# total number of pairs that will be built
result += len(congruence_class[n]) * len(congruence_class[m])
# divide by two since each pair would otherwise be counted twice
return result // 2
Each pair would appear exactly twice in the result: once in-order and once with reversed order. By dividing the result by two this is being corrected. Runtime is O(n + k) (assuming dictionary-operations are O(1)).
The number of loops is C(2, n) = 5!/(2!(5-2)! = 10 loops in your case, and there is nothing magic that would drastically reduce the number of loops.
In JS you can do:
A = [1, 2, 3, 2, 1];
k = 2;
x = 1;
y = 0;
for(i=0; i<A.length; i++) {
for(j=i+1; j<A.length; j++) {
if ((A[i]+A[j])%k !== x) {
continue;
}
if ((A[i]*A[j])%k !== y) {
continue;
}
console.log('('+A[i]+', '+A[j]+')');
}
}
Ignoring A, we can find all solutions of n * (x - n) == y mod k for 0 <= n < k. That's a simple O(k) algorithm -- check each such n in turn.
We can count, for each n, how often A[i] == n, and then reconstruct the counts of pairs. For if cs is an array of these counts, and n is a solution of n * (x - n) == y mod k, then there's cs[n] * cs[(x-n)^k] pairs of things in A that solve our equations corresponding to this n. To avoid double counting we only count n such that n < (x - n) % k.
def count_pairs(A, k, x, y):
cs = [0] * k
for a in A:
cs[a % k] += 1
pairs = ((i, (x-i)%k) for i in xrange(k) if i * (x-i) % k == y)
return sum(cs[i] * cs[j] for i, j in pairs if i < j)
print count_pairs([1, 2, 3, 2, 1], 2, 1, 0)
Overall, this constructs the counts in O(|A|) time, and the remaining code runs in O(k) time. It uses O(k) space.
This was a recent interview question. The question asked to find the size of the largest subtree of a BST contained within a range [x, y] were x < y. The BST is defined recursively where each node has an integer value, a left child node, and a right child node. I was only able to the total number of nodes in the tree that lie within the range but was not able to find the largest subtree. Here's the code I used, in python:
def solution(x, y, T):
if T is None:
return 0
size = 0
if x < T.val:
size += solution(x, y, T.left)
if x <= T.val and y >= T.val:
size += 1
# The following if statement was my attempt at resetting the count
# whenever we find a node outside the range, but it doesn't work
if x > T.val or y < T.val:
size = 0
if B > T.x:
size += solution(A, B, T.right)
return size
The solution should be O(N) where N is the number of nodes in the tree.
We can solve the problem recursively. We need to know the left and the right border of each subtree (that is, the smallest and the largest element). If it lies in range [x, y], we can just update the answer with the total size of the current subtree. Here is some code (the solution function returns a tuple with some extra information on top of the answer. If just want it to return the size of the largest subtree in range, you can wrap it around and use it as a helper function).
def min_with_none(a, b):
"""
Returns the minimum of two elements.
If one them is None, the other is returned.
"""
if a is None:
return b
if b is None
return a
return min(a, b)
def max_with_none(a, b):
"""
Returns the maximum of two elements.
If one them is None, the other is returned.
"""
if a is None:
return b
if b is None:
return a
return max(a, b)
def solution(x, y, T):
"""
This function returns a tuple
(max size of subtree in [x, y] range, total size of the subtree, min of subtree, max of subtree)
"""
if T is None:
return (0, 0, None, None)
# Solves the problem for the children recursively
left_ans, left_size, left_min, _ = solution(x, y, T.left)
right_ans, right_size, _, right_max = solution(x, y, T.right)
# The size of this subtree
cur_size = 1 + left_size + right_size
# The left border of the subtree is T.val or the smallest element in the
# left subtree (if it's not empty)
cur_min = min_with_none(T.val, left_min)
# The right border of the subtree is T.val or the largest element in the
# right subtree (if it's not empty)
cur_max = max_with_none(T.val, right_max)
# The answer is the maximum of answer for the left and for the right
# subtree
cur_ans = max(left_ans, right_ans)
# If the current subtree is within the [x, y] range, it becomes the new answer,
# as any subtree of this subtree is smaller than itself
if x <= cur_min and cur_max <= y:
cur_ans = cur_size
return (cur_size, cur_ans, cur_min, cur_max)
This solution clearly runs in linear time as it visits every node only once and performs a constant number of operations per node.
With each node in BST, you can associate a valid range for it say [Li,Ri], which implies that all elements in subtree of that node lie in the valid range.
You can easily define these ranges recursively:
For a node i let's say the range is [Li, Ri] and the value stored in this node is val.
For left child of i, the range is [Li, val − 1]. And similarly for right child the range is [val + 1, Ri].
For root node the valid range is [−inf, inf].
Let's say our size function returned -1 for an invalid subtree. A (sub)tree is valid if both the node's value and all the values in its subtrees are in range.
# returns a tuple: (size of T, best valid size among itself and its subtrees)
def f(x,y,T):
if T is None:
return (0,0)
l_size, l_best = f(x,y,T.left)
r_size, r_best = f(x,y,T.right)
if x <= T.value <= y and l_size >= 0 and r_size >= 0:
return (1 + l_size + r_size,1 + l_size + r_size)
else:
return (-1,max(l_best,r_best))
Given an array A with N elements, I want to find the sum of minimum elements in all the possible contiguous sub-sequences of A. I know if N is small we can look for all possible sub sequences but as N is upto 10^5 what can be best way to find this sum?
Example: Let N=3 and A[1,2,3] then ans is 10 as Possible contiguous sub sequences {(1),(2),(3),(1,2),(1,2,3),(2,3)} so Sum of minimum elements = 1 + 2 + 3 + 1 + 1 + 2 = 10
Let's fix one element(a[i]). We want to know the position of the rightmost element smaller than this one located to the left from i(L). We also need to know the position of the leftmost element smaller than this one located to the right from i(R).
If we know L and R, we should add (i - L) * (R - i) * a[i] to the answer.
It is possible to precompute L and R for all i in linear time using a stack. Pseudo code:
s = new Stack
L = new int[n]
fill(L, -1)
for i <- 0 ... n - 1:
while !s.isEmpty() && s.top().first > a[i]:
s.pop()
if !s.isEmpty():
L[i] = s.top().second
s.push(pair(a[i], i))
We can reverse the array and run the same algorithm to find R.
How to deal with equal elements? Let's assume that a[i] is a pair <a[i], i>. All elements are distinct now.
The time complexity is O(n).
Here is a full pseudo code(I assume that int can hold any integer value here, you should
choose a feasible type to avoid an overflow in a real code. I also assume that all elements are distinct):
int[] getLeftSmallerElementPositions(int[] a):
s = new Stack
L = new int[n]
fill(L, -1)
for i <- 0 ... n - 1:
while !s.isEmpty() && s.top().first > a[i]:
s.pop()
if !s.isEmpty():
L[i] = s.top().second
s.push(pair(a[i], i))
return L
int[] getRightSmallerElementPositions(int[] a):
R = getLeftSmallerElementPositions(reversed(a))
for i <- 0 ... n - 1:
R[i] = n - 1 - R[i]
return reversed(R)
int findSum(int[] a):
L = getLeftSmallerElementPositions(a)
R = getRightSmallerElementPositions(a)
int res = 0
for i <- 0 ... n - 1:
res += (i - L[i]) * (R[i] - i) * a[i]
return res
If the list is sorted, you can consider all subsets for size 1, then 2, then 3, to N. The algorithm is initially somewhat inefficient, but an optimized version is below. Here's some pseudocode.
let A = {1, 2, 3}
let total_sum = 0
for set_size <- 1 to N
total_sum += sum(A[1:N-(set_size-1)])
First, sets with one element:{{1}, {2}, {3}}: sum each of the elements.
Then, sets of two element {{1, 2}, {2, 3}}: sum each element but the last.
Then, sets of three elements {{1, 2, 3}}: sum each element but the last two.
But this algorithm is inefficient. To optimize to O(n), multiply each ith element by N-i and sum (indexing from zero here). The intuition is that the first element is the minimum of N sets, the second element is the minimum of N-1 sets, etc.
I know it's not a python question, but sometimes code helps:
A = [1, 2, 3]
# This is [3, 2, 1]
scale = range(len(A), 0, -1)
# Take the element-wise product of the vectors, and sum
sum(a*b for (a,b) in zip(A, scale))
# Or just use the dot product
np.dot(A, scale)
Given a unimodal array A of n distinct elements (meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order), an integer p (that is the length of the increasing first part) and k (the k-th minimum element) Give an algorithm to compute the value of the kth minimal element that runs in O(log n) time.
Example:
A= {1,23,50,30,20,2}
p= 2
k=3
Answer: 20
Edit
I tried this:
def ksmallest(arr1, arr2, k):
if len(arr1) == 0:
return arr2[len(arr2)-k-1]
elif len(arr2) == 0:
return arr1[k]
mida1 = (int)(len(arr1)/2)
mida2 = (int)((len(arr2)-1)/2)
if mida1+mida2<k:
if arr1[mida1]>arr2[mida2]:
return ksmallest(arr1, arr2[:mida2], k-(len(arr2)-mida2))
else:
return ksmallest(arr1[mida1+1:], arr2, k-mida1-1)
else:
if arr1[mida1]>arr2[mida2]:
return ksmallest(arr1[:mida1], arr2, k)
else:
return ksmallest(arr1, arr2[mida2+1:], k)
For starters have another look at your indexes. You begin with:
if len(arr1) == 0:
return arr2[len(arr2)-k-1]
elif len(arr2) == 0:
return arr1[len(arr1)-k-1]
But surely if arr1 is in ascending order, and arr2 is in descending order the kth minimal element will not be found in the same location.