Re: [algogeeks] Re: Amazon: sort array

2010-07-12 Thread Amit Mittal
I think this will work. # include stdio.h void my_print(int *,int); void swap(int a , intb) { a = a + b; b = a - b; a = a - b; } void sort(int* array, int SIZE, int i, int j) { while(ij) { my_print(array,SIZE); if(array[i] = array[j])

[algogeeks] Re: Amazon: sort array

2010-07-12 Thread Abhishek Kumar Singh
Is not ur code is running in o(n^2) complexity You have used two while loop ... plzz eleborate if i m wrong . On Jul 12, 11:00 am, Amit Mittal amit.mitt...@gmail.com wrote: I think this will work. # include stdio.h void my_print(int *,int); void swap(int a , intb) {  a = a + b;

Re: [algogeeks] sort in O(n)

2010-07-12 Thread srikanth sg
if we have 1 2 5 7 3 4 6 can you explain how to do in place merge... the elements are in an array ? On Sun, Jul 11, 2010 at 10:29 AM, jalaj jaiswal jalaj.jaiswa...@gmail.comwrote: its easy suppose the array is 12345 4321 2345 4321 it increases then dcreases then increases and

[algogeeks] partition a number

2010-07-12 Thread srikanth sg
Partitioning a given integer n into unique partitions of size m. like if n=10,m=4, 7111 is one such partition. Give all such partitions -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to

[algogeeks] Re: Difference b/w two elements in a array

2010-07-12 Thread vikas kumar
traverse array with 2 elements keeping track of 2 min elements. time O(n) space O(1) On Jul 11, 9:34 pm, Amit Jaspal amitjaspal...@gmail.com wrote: Constraint - O(n) On Sun, Jul 11, 2010 at 9:24 AM, amit amitjaspal...@gmail.com wrote: Given an array of size n.find 2 numbers from array whose

[algogeeks] Re: There are 2 type of values - 1 byte and 2 byte given a pointer to any point in the string tell me if its a 1B or 2B value ending there. MSB of 1-byte character is 0 and MSB of 2-byte

2010-07-12 Thread vikas kumar
is it something different than this foo(char *p, int num) { if(!p) return error if(num 0 p[num-1] == 0) print 2 byte value else print 1 byte value } On Jul 11, 11:18 pm, Tech Id tech.login@gmail.com wrote: Question is not clear to me :( Can

[algogeeks] Re: Difference b/w two elements in a array

2010-07-12 Thread vikas kumar
traverse array with 2 elements keeping track of 2 min elements. time O(n) space O(1) On Jul 11, 9:34 pm, Amit Jaspal amitjaspal...@gmail.com wrote: Constraint - O(n) On Sun, Jul 11, 2010 at 9:24 AM, amit amitjaspal...@gmail.com wrote: Given an array of size n.find 2 numbers from array whose

Re: [algogeeks] Re: Difference b/w two elements in a array

2010-07-12 Thread srikanth sg
2 6 3 7 check for this On Mon, Jul 12, 2010 at 12:46 AM, vikas kumar vikas.kumar...@gmail.comwrote: traverse array with 2 elements keeping track of 2 min elements. time O(n) space O(1) On Jul 11, 9:34 pm, Amit Jaspal amitjaspal...@gmail.com wrote: Constraint - O(n) On Sun, Jul 11,

Re: [algogeeks] Re: Difference b/w two elements in a array

2010-07-12 Thread ankur aggarwal
order nlogn is trivial .. any thing better ?? On Mon, Jul 12, 2010 at 2:51 PM, srikanth sg srikanthini...@gmail.comwrote: 2 6 3 7 check for this On Mon, Jul 12, 2010 at 12:46 AM, vikas kumar vikas.kumar...@gmail.comwrote: traverse array with 2 elements keeping track of 2 min elements.

Re: [algogeeks] Re: amazon:-

2010-07-12 Thread ankur aggarwal
edit distance.. On Mon, Jul 12, 2010 at 10:22 AM, Anil Kumar S. R. sra...@gmail.com wrote: Use the A* Search approach, which is based on a function f(x) = g(x) + h(x), where f(x) is the total cost, g(x) is the cost to travel to the current node and h(x) is the heuristic estimate of the cost

Re: [algogeeks] xoring

2010-07-12 Thread ankur aggarwal
i can give an idea.. start from the LHS bit.. ( for 3 =*0*101) divide all numbers in two groups A0and A1 , A1 = 1XXX, A0 = 0XXX form we know that an element in A1 xor an element in A0 will give the soln for this.. IF either of set is empty then we have to go further like this way now divide A1

Re: [algogeeks] Minimum Window String

2010-07-12 Thread ankur aggarwal
i think ques is not complete.. otherwise above ans is rite.. i suppose.. On Mon, Jul 12, 2010 at 10:00 AM, Anand anandut2...@gmail.com wrote: you can use longest common subsequence algorithm to solve it. http://codepad.org/NDAeIpxR On Sun, Jul 11, 2010 at 9:32 AM, amit

Re: [algogeeks] Re: Difference b/w two elements in a array

2010-07-12 Thread Anil C R
you can find both the smallest and the second smallest number ... Anil On Mon, Jul 12, 2010 at 3:21 PM, ankur aggarwal ankur.mast@gmail.comwrote: order nlogn is trivial .. any thing better ?? On Mon, Jul 12, 2010 at 2:51 PM, srikanth sg srikanthini...@gmail.comwrote: 2 6 3 7 check

Re: [algogeeks] Re: Difference b/w two elements in a array

2010-07-12 Thread Anil C R
oh never mind that was wrong... :P Anil On Mon, Jul 12, 2010 at 5:03 PM, Anil C R cr.a...@gmail.com wrote: you can find both the smallest and the second smallest number ... Anil On Mon, Jul 12, 2010 at 3:21 PM, ankur aggarwal ankur.mast@gmail.comwrote: order nlogn is trivial ..

[algogeeks] Re: Histogram Problem

2010-07-12 Thread Tech Id
You will need width of each bar too perhaps. Why should this need any algo? Just get the one which has maximum height or if width is also given then the one with maximum height x width It is O(n) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks

[algogeeks] Re: sort in O(n)

2010-07-12 Thread Tech Id
This is a good solution. Reversing the arrays will be O(n) Then merging will be O(n) too. In place merge is something like this. Have two indices as start1 and start2 start1 points to beginning of mini-sorted portion1 start2 points to beginning of mini-sorted portion2 Increase both start1 and

[algogeeks] Re: partition a number

2010-07-12 Thread Tech Id
This can be done by recursion. Each number can be represented as: N = 1) (N-1), 1 2) (N-2), 2 3) (N-3), 3 N) (1), (N-1) For each number (N-k), repeat the above in recursion. = Gives all the unique partitions. -- You received this message because you are subscribed to the Google

[algogeeks] Mainframe Developer

2010-07-12 Thread sam Dwlabs
send your matching profiles to *...@dwlabs.com * Hello, We have this immediate need with our direct client in Tampa, please let me know if you have any suitable candidates. *Req: Mainframe Developer (Telecom industry experience is a huge plus)* Skills:* Mainframe, **Sync Sort**, JCL,

Re: [algogeeks] Re: sort in O(n)

2010-07-12 Thread Amit Jaspal
@ above can u please be more specific let A[1,9,10] and B [2,4,6] Now how to swap so that the complexity remains O(n) -- Forwarded message -- From: Tech Id tech.login@gmail.com Date: Mon, Jul 12, 2010 at 8:18 PM Subject: [algogeeks] Re: sort in O(n) To: Algorithm Geeks

[algogeeks] Pattern Matching

2010-07-12 Thread amit
Given a text file, implement a solution to find out if a pattern similar to wild cards can be detected. fort example find if a*b*cd*, or *win or *def* exists in the text. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group,

Re: [algogeeks] Minimum Window String

2010-07-12 Thread Amit Jaspal
@ above Consider S=anand T={'d','n'.'a'} LCS is na but the Answer is and . Here the order of characters don't matter as i have mentioned . One way is you can take LCS with every possible permutation of T but that would be exponential On Mon, Jul 12, 2010 at 3:23 PM, ankur aggarwal

[algogeeks] Re: xoring

2010-07-12 Thread Tech Id
I am not sure if the above is very clear. Ankur, if you can explain it some more, it would be really great. -- 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

[algogeeks] graph

2010-07-12 Thread Love-143
1.Give an efficient algorithm which takes as input a directed graph G = (V,E), and determines whether or not there is a vertex s is in V from which all other vertices are reachable.? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to

[algogeeks] graph

2010-07-12 Thread Love-143
Give a linear-time algorithm for the following task. Input: : A directed acyclic graph G (V,E) Question: Does G contain a directed path that touches every vertex exactly once? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this

Re: [algogeeks] Minimum Window String

2010-07-12 Thread Anand
@amit: In this case, we can calculate the length of string S and T and reverse the string whose length is smaller than the other. Then Take LCS of it. To get the final answer. On Mon, Jul 12, 2010 at 7:05 AM, Amit Jaspal amitjaspal...@gmail.comwrote: @ above Consider S=anand T={'d','n'.'a'}

Re: [algogeeks] graph

2010-07-12 Thread Anand
topological sort would cover every vertex once. The path given by Topological sort would be the answer. We can also calculate the vertices given by topological sort and compare it with given vertices. if it is same then graph G does contain a directed path that touches every vertex exactly once.

[algogeeks] 32 comparisons only

2010-07-12 Thread Tech Id
How will you search an integer in just 32 comparisons in an unsorted group of integers? Devise a data structure for it. -- 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

Re: [algogeeks] Re: sort in O(n)

2010-07-12 Thread Anand
@amit: according to your given example this how the logic works. In the below code I took an array a[]={1,9,10,2,4,6}; in the example at the mid point second array start so mid point logic works here. but according to given question we can scan through the array once and can find out where

Re: [algogeeks] Re: sort in O(n)

2010-07-12 Thread srikanth sg
u are using extra memory which is not supposed to be done.. On Mon, Jul 12, 2010 at 10:56 AM, Anand anandut2...@gmail.com wrote: @amit: according to your given example this how the logic works. In the below code I took an array a[]={1,9,10,2,4,6}; in the example at the mid point second

Re: [algogeeks] Re: sort in O(n)

2010-07-12 Thread Anand
In case if you need in place merging than you need to use bi- tonic merge. But the only condition is total array length should be power of two. http://codepad.org/hG8CnM1l On Mon, Jul 12, 2010 at 11:13 AM, srikanth sg srikanthini...@gmail.comwrote: u are using extra memory which is not

[algogeeks] algorithm to draw a line

2010-07-12 Thread Anand
2 coordinate points p(x,y)and q(x,y) are given and write an algorithm to draw aline -- 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] algorithm to draw a line

2010-07-12 Thread ashish agarwal
draw a line or equation of a line? On Mon, Jul 12, 2010 at 4:38 PM, Anand anandut2...@gmail.com wrote: 2 coordinate points p(x,y)and q(x,y) are given and write an algorithm to draw aline -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To

Re: [algogeeks] graph

2010-07-12 Thread ashish agarwal
@anand ..can you explain it more with example On Mon, Jul 12, 2010 at 10:19 AM, Anand anandut2...@gmail.com wrote: topological sort would cover every vertex once. The path given by Topological sort would be the answer. We can also calculate the vertices given by topological sort and compare

Re: [algogeeks] 2_D matrix

2010-07-12 Thread Amir hossein Shahriari
@jalaj: sorry for delay i was busy these days the implementation of my method is a bit hard because R2 and R4 which i mentioned before are not always squares and in that case we must first break the rectangles into squares and then perform the search on squares here breakToSquares function breaks

Re: [algogeeks] Minimum Window String

2010-07-12 Thread Amit Jaspal
@ anand I think u did'nt get the point what if T=['n' , 'n', 'a'] then if u reverse u wont get any LCS but answer is nan . Plz try to get the jist of the question. On Mon, Jul 12, 2010 at 10:44 PM, Anand anandut2...@gmail.com wrote: @amit: In this case, we can calculate the length of string S

Re: [algogeeks] Dynamic Programming Problem on Strings

2010-07-12 Thread Amir hossein Shahriari
dp[i][j] is the number of strings that have i As and j Bs dp[0][0]=1; // s= for (i=1;i=n/2;i++) dp[i][0]=1; // s=AAA... for (i=1;i=n/2;i++) dp[0][i]=0; // the 2nd constraint for (i=1;i=n/2;i++) for (j=1;j=n/2;j++) if (ji) dp[i][j]=0; // the 2nd constraint else

Re: [algogeeks] 32 comparisons only

2010-07-12 Thread Amir hossein Shahriari
make a bitwise trie since the height would be 32 (number of bits in an integer) u only need 32 comparisons to find an element -- 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

[algogeeks] Re: Difference b/w two elements in a array

2010-07-12 Thread aejeet
This problem cannot be solved in less than O(nlgn) time with O(1) space. The Element Distinctness Problem has a proven lower bound of Omega(nlgn). If the least difference problem could be solved in O(n) then we can reduce the ED Problem to Least Difference problem and solve it in O(n) time. This

Re: [algogeeks] graph

2010-07-12 Thread Amir hossein Shahriari
your 1st Q: for each vertex run a dfs and count the number of vertices it can reach O( V^2 + VE ) another solution would be using an algorithm similar to floyd-warshal O(V^3) a[src][dest] is true if there is an edge src-dest for (k=0;kn;k++) for (i=0;in;i++) for (j=0;jn;j++) if (a[i][k]

Re: [algogeeks] Phone Numbers Yet Again!!!

2010-07-12 Thread Amir hossein Shahriari
this can be done in O(n) using DP: for (i=n-1;i=0;i--){ dp[i]=max(dp[i+2],dp[i+3]); // usual if (a[i]==a[i+1]) // excellent size 2 dp[i]=max(dp[i],2+dp[i+2]); if (a[i]==a[i+1] a[i+1]==a[i+2])//excellent size 3 dp[i]=max(dp[i],2+dp[i+3]); if (a[i]==a[i+1] || a[i]==a[i+2] ||

Re: [algogeeks] Re: sort in O(n)

2010-07-12 Thread jalaj jaiswal
@abv amit askd inplace merging On Mon, Jul 12, 2010 at 11:26 PM, Anand anandut2...@gmail.com wrote: @amit: according to your given example this how the logic works. In the below code I took an array a[]={1,9,10,2,4,6}; in the example at the mid point second array start so mid point logic

Re: [algogeeks] algorithm to draw a line

2010-07-12 Thread Anand
Algorithm to draw a line. While searching on net, I got few pointers. http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html On Mon, Jul 12, 2010 at 4:53 PM, ashish agarwal ashish.cooldude...@gmail.com wrote: draw a line or equation of a line? On Mon, Jul 12, 2010 at 4:38 PM, Anand

Re: [algogeeks] Phone Numbers Yet Again!!!

2010-07-12 Thread ashish agarwal
@amit..can you little explain this On Mon, Jul 12, 2010 at 4:50 PM, Amir hossein Shahriari amir.hossein.shahri...@gmail.com wrote: this can be done in O(n) using DP: for (i=n-1;i=0;i--){ dp[i]=max(dp[i+2],dp[i+3]); // usual if (a[i]==a[i+1]) // excellent size 2

[algogeeks] Re: sort in O(n)

2010-07-12 Thread Gene
On Jul 12, 10:48 am, Tech Id tech.login@gmail.com wrote: This is a good solution. Reversing the arrays will be O(n) Then merging will be O(n) too. In place merge is something like this. Have two indices as start1 and start2 start1 points to beginning of mini-sorted portion1 start2

Re: [algogeeks] Phone Numbers Yet Again!!!

2010-07-12 Thread Amir hossein Shahriari
@ashish: i think u meant amir! anyway... profit for an excellent group is 2 and for a good group it's 1 dp[i] is the max profit for memorizing a[i], a[i+1], ... , a[n] dp is initialized to 0 so for example if a[i]==a[i+1]==a[i+2] it means that we can group a[i] a[i+1] a[i+2] together in an

Re: [algogeeks] Minimum Window String

2010-07-12 Thread Amir hossein Shahriari
suppose n is the size of S and m is the size of T make to pointers p1=0 and p2=0 make another array C[]={0,0,...} which c[i] counts the number of occurrence of T[i] in S[p1],S[p1+1]...S[p2] n1=0 is the number of nonzero elements in C so each time n1==m means that range p1..p2 can be an answer

[algogeeks] Re: algorithm to draw a line

2010-07-12 Thread Dave
See en.wikipedia.org/wiki/Bresenham's_line_algorithm Dave On Jul 12, 7:38 pm, Anand anandut2...@gmail.com wrote: 2 coordinate points p(x,y)and q(x,y) are given and write an algorithm to draw aline -- You received this message because you are subscribed to the Google Groups Algorithm Geeks

[algogeeks] Re: Remove Extra Parenthesis

2010-07-12 Thread Gene
On Jul 11, 9:58 am, amit amitjaspal...@gmail.com wrote: Give a algorithm to remove extra pair of parenthesis Remove unnecessary from an expression : 1) (((a))) = a 2) (a+b) = a+b 3) (a+b)*c = (a+b)*c 4)(((a+b)*(c+d))+e) = (a+b)*(c+d)+e We can build an abstract syntax tree with a simple

[algogeeks] Re: partition a number

2010-07-12 Thread Gene
On Jul 12, 10:56 am, Tech Id tech.login@gmail.com wrote: This can be done by recursion. Each number can be represented as: N = 1)   (N-1), 1 2)   (N-2), 2 3)   (N-3), 3 N)  (1), (N-1) For each number (N-k), repeat the above in recursion. You have the right idea, but this

[algogeeks] AMAZON Interview Question

2010-07-12 Thread aejeet
You have an array like ar[]= {1,3,2,4,5,4,2}. You need to create another array ar_low[] such that ar_low[i] = number of elements lower than or equal to ar[i] in ar[i+1:n-1]. So the output of above should be {0,2,1,2,2,1,0} Time complexity : O(nlogn) use of extra space allowed. Can anyone help

Re: [algogeeks] Re: Sub-2Dmatrix maximum

2010-07-12 Thread Amir hossein Shahriari
here's an algorithm that can find the the max black rectangle in O(n^3) here sum[i][j] is the number of contiguous 1s in the ith row from j column until the end of the row for (i=0;in;i++){ sum[i][m]=0; for (j=m-1;j=0;j--){ if (a[i][j]==1) sum[i][j]=1+sum[i][j+1]; else sum[i][j]=0; } } so now

Re: [algogeeks] 32 comparisons only

2010-07-12 Thread ankur aggarwal
good 1. On Tue, Jul 13, 2010 at 2:56 AM, Amir hossein Shahriari amir.hossein.shahri...@gmail.com wrote: make a bitwise trie since the height would be 32 (number of bits in an integer) u only need 32 comparisons to find an element -- You received this message because you are subscribed to

[algogeeks] Re: partition a number

2010-07-12 Thread Debajyoti Sarma
@Gene Please explain the recursive function and the first if condition i m not getting it. -- 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,

[algogeeks] Re: Difference b/w two elements in a array

2010-07-12 Thread vikas kumar
I did not get your point. for 2 6 3 7 min 2 sec min 3 difference is 1 answer is 2 and 3 what more is asked?? On Jul 12, 2:21 pm, srikanth sg srikanthini...@gmail.com wrote: 2 6 3  7 check for this On Mon, Jul 12, 2010 at 12:46 AM, vikas kumar vikas.kumar...@gmail.comwrote: traverse array