Re: [algogeeks] Check if 2 linked lists are identical

2010-06-03 Thread Sundeep Singh
merge sort both lists: O(nlogn) Now, for both lists to be identical, just compare the corresponding elements in the lists i.e. L1(1) == L2(1), L1(2) == L2(2) ... = O(n) --Sundeep. On Wed, Jun 2, 2010 at 10:47 PM, Raj N rajn...@gmail.com wrote: @Antony: The 2 lists should have the same

Re: [algogeeks] Check if 2 linked lists are identical

2010-06-03 Thread divya jain
construct bst from both list and check if the bst are equal by printing their inorder traversal. On 2 June 2010 22:47, Raj N rajn...@gmail.com wrote: @Antony: The 2 lists should have the same elements as well the number must be equal On Wed, Jun 2, 2010 at 5:38 PM, Antony Vincent Pandian.S.

Re: [algogeeks] Check if 2 linked lists are identical

2010-06-03 Thread jaladhi dave
@ Raj, Just to clarify, same elements refer to copy of identical data or link list members pointing to same data ? On Wed, Jun 2, 2010 at 10:47 PM, Raj N rajn...@gmail.com wrote: @Antony: The 2 lists should have the same elements as well the number must be equal On Wed, Jun 2, 2010 at 5:38

Re: [algogeeks] Implementing 2 stacks within a single linear array

2010-06-03 Thread divya jain
for three stacks u can use indices 0 3 6 etc for stack1. 1 4 7 for stack 2 and 2 5 8 etc for stack 3. now if any of the stack overflows but there is still space in array as other stacks have few element then now u can grow ur stack in reverse direction as shown below : indices of array : 0

Re: [algogeeks] Implementing 2 stacks within a single linear array

2010-06-03 Thread manchit gupta
three or more stacks can be made by using linked array representation -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to

Re: [algogeeks] Re: partion of array

2010-06-03 Thread divya jain
u have not sorted the array . first sort the array nd then apply the approach. u ll get the ryt ans On 1 June 2010 21:32, Jitendra Kushwaha jitendra.th...@gmail.com wrote: Using subset sum method we can solve this. It actually DP check out Paybill question and its solution on topcoder website

Re: [algogeeks] Re: Can you solve this?

2010-06-03 Thread divya jain
same question as wat i asked in partioning of array such that the diff is min. On 31 May 2010 22:07, Senthilnathan Maadasamy senthilnathan.maadas...@gmail.com wrote: Same question with interesting answers in stackoverflow :

Re: [algogeeks] Removing extra parentheses in an infix string

2010-06-03 Thread divya jain
1.calculte the postfix of given expression. 2.now remove a particular parenthesis from expression and check if the postfix of this expression is equal to the postfix of original expression. if yes then the parenthesis we have removed were extra. if no then the parenthesis were not exta. 3 now

Re: [algogeeks] Re: partion of array

2010-06-03 Thread Rohit Saraf
@divya: but still haven't you seen Jagadish's example? It is a counterexample to your greedy approach. -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You

Re: [algogeeks] Removing extra parentheses in an infix string

2010-06-03 Thread Terence
You can restore the infix expression from the postfix expression calculated, only add parenthesis when necessary in each step. On 2010-6-3 15:35, divya jain wrote: 1.calculte the postfix of given expression. 2.now remove a particular parenthesis from expression and check if the postfix of

Re: [algogeeks] Re: partion of array

2010-06-03 Thread divya jain
oh...okay... good example... On 3 June 2010 13:23, Rohit Saraf rohit.kumar.sa...@gmail.com wrote: @divya: but still haven't you seen Jagadish's example? It is a counterexample to your greedy approach. -- Rohit Saraf Second Year

Re: [algogeeks] Re: Can you solve this?

2010-06-03 Thread saurabh gupta
@Divya: nope, your Q requires equal count too whereas this doesn't. On Thu, Jun 3, 2010 at 1:06 PM, divya jain sweetdivya@gmail.com wrote: same question as wat i asked in partioning of array such that the diff is min. On 31 May 2010 22:07, Senthilnathan Maadasamy

Re: [algogeeks] Removing extra parentheses in an infix string

2010-06-03 Thread Algoose Chase
Will this work ? consider A+(B*C) have an operator stack to hold the operators. As we scan elements from left to right,push the operators in operator stack. when you encounter a '(' , then scan to find the first operator that comes after '(' (in this case *). If this operator has a higher

[algogeeks] Valid permutation of the brackets

2010-06-03 Thread Jitendra Kushwaha
The question is that you have to print all the valid permutations of the given number of brackets for example for input 3 we have the output 1 ((())) 2 (()()) 3 (())() 4 ()(()) 5 ()()() total five valid permutation this can be solved in O( 2^(2n) ) where n is number of brackets . Algo will be

Re: [algogeeks] Implementing 2 stacks within a single linear array

2010-06-03 Thread Jitendra Kushwaha
@Raj N : You are right overflow is top2-(top1+1)==0 @Anand : The oveflow condition is dependent how underflow condition are implemented. Considering underflow above condition for overflow suffice For 3 stacks a efficient algo is not easy to think in a single array , yet we can optimise

Re: [algogeeks] Valid permutation of the brackets

2010-06-03 Thread sharad kumar
@jitendra #includeiostream using namespace std; void parens(int pairs) { int i, j, s, n = 2*(pairs - 1); for (i = 0; i 1 n; i++) { for (s = 1, j = 0; (j n) (s = 0) (s = pairs); j++) s += ((i j) 1) ? 1 : -1; if ((j != n) || (s != 1))

Re: [algogeeks] Re: Can you solve this?

2010-06-03 Thread Rohit Saraf
Still the solution will be similar and actually a bit simpler. -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received this message because you

Re: [algogeeks] Removing extra parentheses in an infix string

2010-06-03 Thread Rohit Saraf
A*(B+C*D) -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group.

Re: [algogeeks] Removing extra parentheses in an infix string

2010-06-03 Thread Rohit Saraf
So there is a prob algoose A*(B*C) and a*(b*c+d) i hope you understood -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received this message

Re: [algogeeks] Valid permutation of the brackets

2010-06-03 Thread Rohit Saraf
why don't you make use of the optimal substructure. You can easily use the recurrence relation as DP @all : people don't just paste your code. Words are better than code -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this

Re: [algogeeks] Check if 2 linked lists are identical

2010-06-03 Thread Rohit Saraf
simple in place O(n lg n) solution. Choose a pivot in first array and partition it like in quicksort. Find the pivot in second array and partition. Now recurse on both halves. At any point if no of elements in array are not equal... Abort! -- --

Re: [algogeeks] Valid permutation of the brackets

2010-06-03 Thread Anil Kishore
A recursive way of filling each position is better, so we can cut it down when its invalid and not proceed further. Something like below, // 1-based indexing - global n , A[2*n+1] - go( ci , cs ) // cur.index , cur.sum { if( ci == 2n+1 ) print A with [ 1 = '(', -1 = ')' ]

Re: [algogeeks] Valid permutation of the brackets

2010-06-03 Thread Terence
Discard illegal prefix as soon as possible, and only proceed with legal one. * For some position, if the number of '(' and ')' are equal, then do not try ')' on that position. * If the number of '(' reaches n, fill the rest positions with ')' to get a valid permutation. On 2010-6-3 16:15,

Re: [algogeeks] Re: Can you solve this?

2010-06-03 Thread saurabh gupta
yep, constraints are fewer here but in terms of problem statement 'same' and 'similar' can create diversions. On Thu, Jun 3, 2010 at 6:01 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: Still the solution will be similar and actually a bit simpler. --

Re: [algogeeks] Check if 2 linked lists are identical

2010-06-03 Thread Anurag Sharma
But perhaps the elements in lists may not be in order. Anurag Sharma http://anuragsharma-sun.blogspot.com/ On Thu, Jun 3, 2010 at 6:38 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: simple in place O(n lg n) solution. Choose a pivot in first array and partition it like in quicksort. Find

Re: [algogeeks] Re: Can you solve this?

2010-06-03 Thread saurabh gupta
This should be solvable by dp as in knapsack problem where value = weights and W = S/2. where S = sum of all elements in the array. The claim here is diff in sum of two subsets will be minimum when one maximizes a sub-set sum (say s) where sum (s) is closest to S/2. Divya's problem is looking

Re: [algogeeks] Valid permutation of the brackets

2010-06-03 Thread Senthilnathan Maadasamy
@Jitendra: Catalan number grows at least exponentially: http://mathworld.wolfram.com/CatalanNumber.html -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this

Re: [algogeeks] Removing extra parentheses in an infix string

2010-06-03 Thread Algoose Chase
Thats right !!! On Thu, Jun 3, 2010 at 6:08 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: So there is a prob algoose A*(B*C) and a*(b*c+d) i hope you understood -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer

Re: [algogeeks] Removing extra parentheses in an infix string

2010-06-03 Thread Antony Vincent Pandian.S.
If the base logic follows the below rule, it should work. If any operator within parenthesis is of less precedence to the operator before the opening parenthesis, the parenthesis can not be removed. For cases of equal precedence of operators before parenthesis and within parenthesis,

Re: [algogeeks] Check if 2 linked lists are identical

2010-06-03 Thread Antony Vincent Pandian.S.
Consider two linked lists l1 and l2. Create 2 hash maps,h1 and h2, one for each list with the format, element, number of occurrence. Traverse one element in each list at a time. For each element in list l1, check whether it is present in h2. If yes, remove the element from h2 if the number of