Re: [algogeeks] Re: Find duplicate element in an array

2011-02-17 Thread nphard nphard
I fail to see how any of them work in the general case. In fact, I don't even see them working with range restrictions for n. Also what is the missing number you are talking about? The question is to find the repeated number. As I said before, its an element uniqueness problem whose lower bound

Re: [algogeeks] UVa - Gold Coins

2011-02-16 Thread nphard nphard
Let f(n) = n(n+1)/2 We have to find n1 and n2 such that f(n1) N = f(n2) and n2 = n1 + 1. Solution is n2. Can be done in O(1) as follows: Solve N = n(n+1)/2 for unknown n. Requires us to solve quadratic equation: n^2 + n - 2N = 0 Find positive root of the equation which could be a real number.

Re: [algogeeks] Linked List Amazon

2011-02-16 Thread nphard nphard
Node *flatten(Node *node) { if (!node) return NULL; Node *head = node; Node *next = node-next; node-next = NULL; if (node-down) node-next = flatten(node-down); while (node-next != NULL) node = node-next; node-next = flatten(next); return head; } On Wed, Feb 16, 2011 at 8:38 PM, bittu

Re: [algogeeks] UVa - Gold Coins

2011-02-16 Thread nphard nphard
using namespace std; int main(){ int n,ac,k,sum; while(cinn n){ ac=0; k=ceil((sqrt(1+8*n)-1)/2)-1; ac+=k*(k+1)*(2*k+1)/6; sum=(k+1)*(k+2)/2; ac+=(k+1)*((k+1)-(sum-n)); coutn acendl; } } 2011/2/16 nphard nphard nphard.nph

Re: [algogeeks] Re: Find duplicate element in an array

2011-02-14 Thread nphard nphard
This problem does not seem to have a O(n) time solution in the general case because it is a version of the Element uniqueness problem ( http://en.wikipedia.org/wiki/Element_distinctness_problem) which has a proven tight asymptotic bound of *n log n*. On Sun, Jan 16, 2011 at 11:17 PM,

Re: [algogeeks] language

2011-02-06 Thread nphard nphard
For len = 6, isn't bcbcbc also valid? On Sun, Feb 6, 2011 at 9:08 PM, Anand anandut2...@gmail.com wrote: There is a language with only two elements ie 'a' and 'bc'. How many words can be made out of it if whose Length is n. I think it is n+1. if the len = 3 abc bca if the len is 6

Re: [algogeeks] Re: Amazon Written Tes Q1

2011-01-30 Thread nphard nphard
O(n) is impossible even for an array where we can do random access. On Sun, Jan 30, 2011 at 11:39 AM, juver++ avpostni...@gmail.com wrote: There is a well-known bound for the comparison sort, O(nlogn). If there is a way to sort doubly-linked list in a linear time, so we are able to sort all

Re: [algogeeks] Re: Amazon Question

2011-01-29 Thread nphard nphard
, nphard nphard nphard.nph...@gmail.comwrote: Not correct. You cannot assume that the right node always points to the successor. If you do that, your traversal will be affected. Consider that when you reach a node B from the right pointer of its parent A, you traverse that subtree rooted at B

Re: [algogeeks] Distance in a dictionary

2011-01-27 Thread nphard nphard
One more thing is that you cannot stop if you reach the goal state as you still do not know if its the optimal path or not (heuristic does not help in that). So, you need to search in a Breadth First manner anyway regardless of reaching goal states in some places to determine the optimal path (as

Re: [algogeeks] Re: Amazon Question

2011-01-27 Thread nphard nphard
are NULL pointers are replaced with pointer to successor. On Jan 26, 1:18 pm, nphard nphard nphard.nph...@gmail.com wrote: If you convert the given binary tree into right threaded binary tree, won't you be using extra space while doing so? Either the given tree should already

Re: [algogeeks] Re: Amazon Question

2011-01-27 Thread nphard nphard
we suppose that right pointer always gives the successor.. The solution to get the linear inorder traversal is just tailored for a situation where there is no extra space,not even for stack!!! On Fri, Jan 28, 2011 at 5:42 AM, nphard nphard nphard.nph...@gmail.comwrote: @Ritu - Do you

Re: [algogeeks] Re: Amazon Question

2011-01-26 Thread nphard nphard
using additional space(as successor of any node is available O(1) time) and keep track of 5th largest element. Regards, Ritu On Jan 26, 8:38 am, nphard nphard nphard.nph...@gmail.com wrote: Theoretically, the internal stack used by recursive functions must be considered for space

Re: [algogeeks] Re: Amazon Question

2011-01-26 Thread nphard nphard
: No,no extra space is needed. Right children which are NULL pointers are replaced with pointer to successor. On Jan 26, 1:18 pm, nphard nphard nphard.nph...@gmail.com wrote: If you convert the given binary tree into right threaded binary tree, won't you be using extra space

Re: [algogeeks] Re: Amazon Question

2011-01-25 Thread nphard nphard
Theoretically, the internal stack used by recursive functions must be considered for space complexity. On Mon, Jan 24, 2011 at 5:40 AM, juver++ avpostni...@gmail.com wrote: internal stack != extra space -- You received this message because you are subscribed to the Google Groups Algorithm

Re: [algogeeks] Re: Amazon Question

2011-01-25 Thread nphard nphard
I don't understand what you mean. Consider a simple inorder traversal of a balanced binary tree. Using recursion, the code is simply: void inorder(Node *node) { if (node == NULL) return; inorder(node-left); print(node-val); inorder(node-right); } What do you consider to be the above

Re: [algogeeks] master thm

2011-01-16 Thread nphard nphard
O(n) is correct. On Sun, Jan 16, 2011 at 6:04 AM, snehal jain learner@gmail.com wrote: The running time of an algorithm is represented by the following recurrence relation: if n = 3 then T(n) = n else T(n) = T(n/3) + cn Which one of the following represents the time

[algogeeks] Find duplicate element in an array

2011-01-15 Thread nphard nphard
Given an array of integers of size 'n' - consisting of 'n-2' unique integers and 1 integer with a duplicate, find the repeated element in O(n). Note: This is a converse of finding the unique element in an array consisting of duplicates - which can be solved with the XOR technique - but I am not

Re: [algogeeks] Find duplicate element in an array

2011-01-15 Thread nphard nphard
No. On Sat, Jan 15, 2011 at 11:06 PM, Nikhil Agarwal nikhil.bhoja...@gmail.comwrote: do you have the range of elements in the array? On Sun, Jan 16, 2011 at 8:51 AM, nphard nphard nphard.nph...@gmail.comwrote: Given an array of integers of size 'n' - consisting of 'n-2' unique integers

[algogeeks] Re: google written

2011-01-15 Thread nphard nphard
A cool way of solving this is by using the suffix tree. 1. Concatenate the string with itself. 2. Create a suffix tree. 3. Descend along the least lexicographic path from the root. Solution works in O(n). On Jan 15, 4:55 pm, Jammy xujiayiy...@gmail.com wrote: @Wei Please test you code on