Re: [algogeeks] Re: question

2010-05-15 Thread divya jain
Very simple N^2*logN solution: sort the input array, then go through all pairs Ai, Aj (N^2 time), and for each pair check whether (S - Ai - Aj) is in array (logN time). On 12 May 2010 23:08, jalaj jaiswal jalaj.jaiswa...@gmail.com wrote: @sateesh suppose after sorting the array is

[algogeeks] k the smallest in BST without using static variable

2010-05-15 Thread kaushik sur
Hi Friends I have encountered the question in sites - Given a Binary Search Tree, write a program to print the kth smallest element without using any static/global variable. You can't pass the value k to any function also. I have tried my hands using explicit stack for inorder and keeping a

Re: [algogeeks] partion of array

2010-05-15 Thread Anurag Sharma
One more way to do this can be: 1. sorting the number in decreasing order 2. start with 2 empty sets A and B and assign first 2 elements to each of them (see the following example) 3. maintain the sums of A and B in variables. 4. take the next number from list and add it to the set

Re: [algogeeks] Convert a Binary tree into spike.

2010-05-15 Thread Algoose Chase
Hi , We can do Breadth first search but without any additional Memory like Queue. Since we connect the siblings we can traverse through siblings. Going from Top to bottom, Each Internal node(non-leaf) must connect its children. If that internal node has a right sibling then connect the right most

Re: [algogeeks] Re: tree from linked list

2010-05-15 Thread Yalla Sridhar
@atul linked list can be converted to array with O(n) both space @ time overhead. then tree can be built in O(n) On Sat, May 15, 2010 at 2:53 AM, W Karas wka...@yahoo.com wrote: See the definition of: templatetypename fwd_iter bool build(fwd_iter p, size num_nodes) Within the iter

Re: [algogeeks] Re: median of a bst

2010-05-15 Thread Nikhil Agarwal
We can try rotation too. 1.We iterate rotating of the left/right sub-tree having greater number of nodes. 2. if number of keys are :- ODD- Equal number of nodes exist on both left and rt subtree of root .Key at root is the median of the BST.. EVEN-left subtree should have 1 less node than

Re: [algogeeks] Re: partion of array

2010-05-15 Thread divya jain
my approach: 1. sort the array 2. take a variable diff. intitialize it to 0. 3. take the 1st element from array nd place it in array A and 2nd element in array B. stoe in diff sum(A)-sum(B). 4. now place the next element in array A or B according to the condition if if sum(A+element)-sum(B)

Re: [algogeeks] question

2010-05-15 Thread Anurag Sharma
Cant it be done this way:- 1. Sort the array in O(n log(n)) time 2. select all pairs of numbers in array and keep track of minimum sum (closest to zero) and also their indices, in O(n^2) time 3. In another iteration skipping the indices which gave the minimum sum, check whether the

Re: [algogeeks] Re: partion of array

2010-05-15 Thread Rohit Saraf
so what will ur algo give for array 1,200,500,2000 On 5/15/10, divya jain sweetdivya@gmail.com wrote: my approach: 1. sort the array 2. take a variable diff. intitialize it to 0. 3. take the 1st element from array nd place it in array A and 2nd element in array B. stoe in diff

Re: [algogeeks] k the smallest in BST without using static variable

2010-05-15 Thread Rohit Saraf
there is something called morris inorder traversal. credits to donald knuth On 5/15/10, kaushik sur kaushik@gmail.com wrote: Hi Friends I have encountered the question in sites - Given a Binary Search Tree, write a program to print the kth smallest element without using any