Re: [algogeeks] BT

2011-01-14 Thread anoop chaurasiya
if the root of T2 is duplicated in T1,this code may give a wrong
answer(as there is no provision of backtracking in case there is a mis
match after some matchings).given above KMP is the best possible answer
i think..

On Wed, Jan 12, 2011 at 11:28 AM, pacific pacific wrote:

> Here is my pseudo code :
> check( node t1 , node t2 )
> {
> if ( t1->data == t2->data)
> {
> return check( t1->left , t2->left ) && check(t1->right, t2->right) ;
> }
> else
> {
> return check(t1->left , t2) || check(t1->right , t2);
> }
> }
>
> time complexity : o(n) because each node in t1 needs to be visited once.let
> me know if this works.
>
>
> On Sun, Jan 9, 2011 at 1:30 PM, Harshal  wrote:
>
>> @Nishaanth
>> T1 has millions of nodes. Suppose all the nodes of T1 are equal to root of
>> T2. Then u will have to check every where in T1. Putting height as
>> constraint, u will have to check only those nodes whose height is equal to
>> T2. It will reduce time complexity.
>>
>> Well m not able to think of better time complexity, another way would be:
>> Find Height of T2 ... O(k)  //k is no. of nodes in T2
>> Find Height of each node in T1... O(N)  //N is no. of nodes in T1
>>
>> now if p nodes in T1 have height same as T2, then we can find if a subtree
>> rooted at any of those p nodes are identical to T2 in O(pk) time.
>>
>> Thus total time complexity: O(N) + O(k) + O(pk).
>> correct me if I am wrong..
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: ThreeListSum

2011-01-11 Thread anoop chaurasiya
Thanx Dave...same logic as finding a sum in a sorted array..fool to miss
it

On Tue, Jan 11, 2011 at 8:40 AM, Dave  wrote:

> @Anoop: It can be done in O(n^2):
>
> Sort arrays B and C
> For i = 1 to n Do
>j = 1
>k = n
>While( j <= n and k >= 1)
>If( A(i) + B(j) + C(k) < 0 ) Then j = j + 1
>Else if( A(i) + B(j) + C(k) > 0 ) Then k = k - 1
>Else Return TRUE
>End While
> Return FALSE
>
> The While loop is iterated at most 2N times for each i.
>
> Dave
>
> On Jan 11, 4:02 am, anoop chaurasiya 
> wrote:
> > does any better solution exists than O(N^2logN)??
> >
> > On Tue, Jan 11, 2011 at 2:56 AM, juver++  wrote:
> > > There is no need to sort first two arrays.
> >
> > > --
> > > 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
> > > algogeeks+unsubscr...@googlegroups.com
> 
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/algogeeks?hl=en.
> >
> > --
> > Anoop Chaurasiya
> > CSE (2008-2012)
> > NIT DGP
>
> --
> 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
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
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 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] ThreeListSum

2011-01-11 Thread anoop chaurasiya
can't u presort one of them in O(nlogn)...???

On Tue, Jan 11, 2011 at 5:05 AM, dinesh bansal  wrote:

> I think for unsorted lists, it will be O(n^3).
>
>
>
> On Tue, Jan 11, 2011 at 1:26 PM, juver++  wrote:
>
>> There is no need to sort first two arrays.
>>
>> --
>> 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
>> algogeeks+unsubscr...@googlegroups.com
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> Dinesh Bansal
> The Law of Win says, "Let's not do it your way or my way; let's do it the
> best way."
>
>  --
> 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
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
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 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] ThreeListSum

2011-01-11 Thread anoop chaurasiya
does any better solution exists than O(N^2logN)??

On Tue, Jan 11, 2011 at 2:56 AM, juver++  wrote:

> There is no need to sort first two arrays.
>
> --
> 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
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
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 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] find triplets in an integer array A[] which satisfy condition: a[i]^2 + a[j]^2 = a[k]^2

2010-12-01 Thread anoop chaurasiya
@fenghuang try this array:
a[]={3,3,3,3,3,3,4,4,4,4,4,4,5}
so asq[]={9,9,9,9,9,9,16,16,16,16,16,16,25}
here as u can see the total number of requisite triple pairs are 6*6=36,
in general for above array total number of pairs is (n/2)*(n/2) i.e. n^2/4
where n is the size of the array.
by using O(n) algo and since you are choosing them one by one,u can't
include all of them as they are of order O(n^2).
so removing repetitions is the only option i think..

On Wed, Dec 1, 2010 at 11:37 PM, fenghuang wrote:

> @anoop
> in fact, it always work even if there are repeated elements, because they
> don't change the decision.
> in detail, assume ii, ,jj, kk is one of the answers, then
> a[ii]+a[jj]=a[kk]. since the array is sorted, so a[ii-1]+a[jj] <= a[kk] and
> a[ii] + a[jj+1] >= a[kk].
> so  when you try the pair of 'ii-1'  and 'jj', the next step must be
> calculate a[ii] + a[jj] as long as a[ii-1]+a[jj] is not equal to a[kk]. the
> same to the pair 'ii' and 'jj+1'.
> the algorithm is correct and in the whole procedure,  repeated elements
> don't affect the decision.
> I'm sorry for my poor English.
> Thank You!
>
> On Thu, Dec 2, 2010 at 12:14 PM, anoop chaurasiya <
> anoopchaurasi...@gmail.com> wrote:
>
>> sorry for the interruption,we can make it work even if the elements are
>> repeated, by removing the duplicacy in linear time(as the array is already
>> sorted) and taking a count of no. of duplicates in the seperate array.
>>
>> On Wed, Dec 1, 2010 at 9:37 PM, Senthilnathan Maadasamy <
>> senthilnathan.maadas...@gmail.com> wrote:
>>
>>> A small correction to the algorithm above.  In Step 3, instead of finding
>>> *any* pair (a,b) such that a+b = x we need to find *all* such pairs.
>>> However the complexity remains the same.
>>>
>>> --
>>> 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
>>> algogeeks+unsubscr...@googlegroups.com
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>
>>
>> --
>> Anoop Chaurasiya
>> CSE (2008-2012)
>> NIT DGP
>>
>> --
>> 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
>> algogeeks+unsubscr...@googlegroups.com
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> 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
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
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 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] find triplets in an integer array A[] which satisfy condition: a[i]^2 + a[j]^2 = a[k]^2

2010-12-01 Thread anoop chaurasiya
sorry for the interruption,we can make it work even if the elements are
repeated, by removing the duplicacy in linear time(as the array is already
sorted) and taking a count of no. of duplicates in the seperate array.

On Wed, Dec 1, 2010 at 9:37 PM, Senthilnathan Maadasamy <
senthilnathan.maadas...@gmail.com> wrote:

> A small correction to the algorithm above.  In Step 3, instead of finding
> *any* pair (a,b) such that a+b = x we need to find *all* such pairs.
> However the complexity remains the same.
>
> --
> 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
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
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 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: find triplets in an integer array A[] which satisfy condition: a[i]^2 + a[j]^2 = a[k]^2

2010-12-01 Thread anoop chaurasiya
@DEV,your idea is nice.but i think it wont work in case the array is
having repeated elementsam i right

On Wed, Dec 1, 2010 at 1:51 PM, nishaanth  wrote:

> I have a O(n^2) solution..
>
> Hash all the squares.0(n)
>
> Now for every pair find the sum, see if its there in the  hash table.O(n^2)
>
> Total complexity : O(n^2)
>
>
> On Wed, Dec 1, 2010 at 11:00 PM, fenghuang wrote:
>
>> yeah, you're right. thank you  and  is it the optimal solution about this
>> question?
>>
>>
>> On Thu, Dec 2, 2010 at 1:08 AM, Dave  wrote:
>>
>>> @Fenghuang: No. You don't have to search for b for every a, or more
>>> precisely, you don't have to search for a j for every i. Something
>>> like this should work for step 3:
>>>
>>> i = 0
>>> j = k-1
>>> repeat while i <= j
>>>if asq[i] + asq[j] < asq[k] then i = i+1
>>>else if asq[i] + asq[j] > asq[k] then j = j-1
>>>else break
>>> end repeat
>>> if i <= j then you have found an i, j, and k satisfying the desired
>>> relationship;
>>> otherwise, no such i and j exist for this k.
>>>
>>> This loop is O(n). Surround this with a loop over k and precede that
>>> loop with a sort to get Senthilnathan's algorithm. So, as he says, the
>>> whole task is O(n log n + n^2) = O(n^2).
>>>
>>> Dave
>>>
>>> On Dec 1, 10:11 am, fenghuang  wrote:
>>> > should it be O(n^2*lgn)? for each x in n, it's O(n), and for each a,
>>> > it'sO(n), and searching b, it's O(lgn), so it's O(n*n*lgn),Thank You!
>>> >
>>> > On Wed, Dec 1, 2010 at 11:02 PM, Senthilnathan Maadasamy <
>>> >
>>> >
>>> >
>>> > senthilnathan.maadas...@gmail.com> wrote:
>>> > > Hi,
>>> > >   How about the following approach?
>>> >
>>> > > Step 1:  Replace each array element with its square  -  O(n)
>>> >
>>> > > Step 2:  Sort the array  -  O(n*log(n))
>>> >
>>> > > Step 3: For each element x in the array
>>> > > Find two elements a, b in the array (if any) such
>>> that
>>> > > a+b = x.
>>> > >  Since finding two such elements can be done in O(n) time
>>> in a
>>> > > sorted array, the complexity of this step 3 is O(n^2).
>>> >
>>> > > While reporting the output you can take the square root and print it
>>> out.
>>> >
>>> > > Total complexity is O(n^2).
>>> >
>>> > >  --
>>> > > 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
>>> > > algogeeks+unsubscr...@googlegroups.com
>>> 
>>> > > .
>>> > > For more options, visit this group at
>>> > >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text -
>>> >
>>> > - Show quoted text -
>>>
>>> --
>>> 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
>>> algogeeks+unsubscr...@googlegroups.com
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>  --
>> 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
>> algogeeks+unsubscr...@googlegroups.com
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> S.Nishaanth,
> Computer Science and engineering,
> IIT Madras.
>
> --
> 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
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
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 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] palindrome problem

2010-11-16 Thread anoop chaurasiya
the idea for O(|S|^2) is as follows

Let string be s[1..n]

p[i][j]=1 if s[i...j] is a palindrome
p[i][j]=0 otherwise.

we can precalculate table p[i][j] as follows..in O(|s|^2)
p[i][i]= 1
p[i][i+1]=1 if s[i]==s[i+1], 0 otherwise
for all others..
p[i][j]= (s[i]==s[j] && p[i+1][j-1]==1).

now let m[i] represents the minimum number of palindromes needed to
represent s[1...i],
so m[n] is our required answer..
now...let us define m[i] recursively as follows..
m[0]=0, m[1]=1
m[i]=min { m[j-1]+1 where 1<=j<=i && p[j][i]==1}
what it actually says is that...we can go through all possible palindromes
which can be at the end of s[1...i],and check choosing which one is the best
option for us...
i guess its correct...if there is any problem with it then do suggest

On Tue, Nov 16, 2010 at 7:56 PM, cheng lee  wrote:

> how to do that in O(|s|^2)?
>
> 2010/11/16 anoop chaurasiya 
>
>> will O(|s|^2) dp workor u need something more efficient than that
>>
>>
>> On Tue, Nov 16, 2010 at 1:51 PM, lichenga2404 wrote:
>>
>>> A palindrome is a string which is identical to itself in reverse
>>> order. For example
>>> \ABAAABA" is a palindrome. Given a string, we'd like to break it up
>>> into the small-
>>> est possible number of palindromes. Of course, any one-character
>>> string is a palindrome
>>> so we can always break a length n string into n palindromes. Design an
>>> e cient al-
>>> gorithm to determine the minimum number of palindromes in a
>>> decomposition for a
>>> given string, and analyze the running time. You may assume you are
>>> given a proce-
>>> dure Palindrome(S) which runs in time O(jSj) and returns true if and
>>> only if S is a
>>> palindrome.
>>>
>>>  How to solve this problem with the dynamic programming method?
>>>
>>> --
>>> 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
>>> algogeeks+unsubscr...@googlegroups.com
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>
>>
>> --
>> Anoop Chaurasiya
>> CSE (2008-2012)
>> NIT DGP
>>
>>  --
>> 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
>> algogeeks+unsubscr...@googlegroups.com
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> licheng
>
> --
> 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
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
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 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] palindrome problem

2010-11-16 Thread anoop chaurasiya
will O(|s|^2) dp workor u need something more efficient than that

On Tue, Nov 16, 2010 at 1:51 PM, lichenga2404 wrote:

> A palindrome is a string which is identical to itself in reverse
> order. For example
> \ABAAABA" is a palindrome. Given a string, we'd like to break it up
> into the small-
> est possible number of palindromes. Of course, any one-character
> string is a palindrome
> so we can always break a length n string into n palindromes. Design an
> e cient al-
> gorithm to determine the minimum number of palindromes in a
> decomposition for a
> given string, and analyze the running time. You may assume you are
> given a proce-
> dure Palindrome(S) which runs in time O(jSj) and returns true if and
> only if S is a
> palindrome.
>
>  How to solve this problem with the dynamic programming method?
>
> --
> 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
> algogeeks+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Anoop Chaurasiya
CSE (2008-2012)
NIT DGP

-- 
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 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.