[algogeeks] Re: Testing if 3 points form a triangle

2007-05-26 Thread aakash mandhar
Any 3 points always form a triangle, unless they are collinear. So all you need to do is to find if the slopes wrt the 3 planes is the same for lines formed using any 2 points. Regards, Aakash On 5/27/07, Feng <[EMAIL PROTECTED]> wrote: > > Hi all! > > Given 3 points in 3D, what is the fast and

[algogeeks] Re: Loop in a linked list

2007-04-04 Thread aakash mandhar
There are many efficient ways to do this. I am listing them out. 1) Use two pointers. One advancing one node at a time and the other advancing 2 nodes at a time. So that the relative speed between them is always one node. If the first and second pointer coincide then there is a loop. 2) Destructi

[algogeeks] Re: find the closest common ancestor of node u and v?

2007-03-21 Thread aakash mandhar
There is a highly optimal O(n) solution available. 1) Find the depth at which each node is situated let them be du and dv respectively for u and v 2) Not bring u and v to the same depth. that is skip abs(du-dv) nodes from the greater depth node 3) Compare both nodes and do a (u=u->parent and v=v->

[algogeeks] Re: integers n1,n2 such that n1+n2 = x

2007-02-14 Thread aakash mandhar
Looks like the analysis sucked. The answer is as follows: Sort array (using merge sort) => n*log(n) complexity for(i=0;a[i] n complexity { if(binary_search for value (x-a[i]) in the range i+1,n =>log(n) complexity } So sorting = n*log(n) for loop and search = n*log(n) Which gives overall comp

[algogeeks] Re: a data structure for phone directory

2007-02-14 Thread aakash mandhar
faster retrievals trie is the way to go. If you want to overcome the overheads of a trie (mainly the space utilization) try hashing the names or use a B Tree. Regards, Aakash On 2/15/07, aakash mandhar <[EMAIL PROTECTED]> wrote: > > I would recommend using a trie, and a list of all num

[algogeeks] Re: a data structure for phone directory

2007-02-14 Thread aakash mandhar
I would recommend using a trie, and a list of all numbers for the same name at a terminating point of the trie. Regards, Aakash On 2/14/07, Karthik Singaram L <[EMAIL PROTECTED]> wrote: > > It does depend on the size of the problem you have in mind. Tries can be > expensive for names depending on

[algogeeks] Re: (need help) How to solve this random number generatioin problem?

2007-01-30 Thread aakash mandhar
/* You can do it as shown below. */ #include #include using namespace std; int random(int low,int high) { srand(time(NULL)); int n=(high-low+1); return low + (rand()%n); } int main() { cout< wrote: > > > Question: > > > Given a program which can generate one of {1, 2, 3, 4, 5} randomly. >

[algogeeks] Re: 2D arrays

2007-01-24 Thread aakash mandhar
for(int i=0;i<=n/2;i++) { for(int j=0;j<=i;j++) { a[i][j]=='*'; } for(int j=i+1;j wrote: > > > OK, I need to write an algorithm to populate a 2D array A(i,j) of size > n x n. There is 1 '*' in the 1st row, increasing to n/2 in the middle > row, this decreases back to 1'*' in the last row. We can a

[algogeeks] Re: whats the fastest way to find the odd man out?

2007-01-16 Thread aakash mandhar
/* A compiling version of Atamurad's code */ #include using namespace std; int oddman(int a[], int n) { int res=0; for(int i=0;i wrote: > I am not sure how XOR will be defined on numbers. Let X be number which occurs only once in sequence. and i1, i2 are same, ie i1=i2. where i is one of

[algogeeks] Re: whats the fastest way to find the odd man out?

2007-01-16 Thread aakash mandhar
The XOR solution is the best. The underlying asssumption is that the numbers are int and the bitwise operators are fast on the machine (as on most machines). Regards, Aakash On 1/16/07, Satish <[EMAIL PROTECTED]> wrote: Hangjin Zhang wrote: > Do an XOR on all numbers. The resulte is the numb