[algogeeks] Re: What is the algorithm for this problem

2014-07-08 Thread Don
card by one of the hints which you do request. There are only 120 combinations of 7 hints. If you try each one of those to determine if it distinguishes each pair of cards selected, you can determine if only 7 hints will suffice. Keep trying with fewer hints until you find the lower limit. Don

[algogeeks] Re: What is the algorithm for this problem

2014-07-07 Thread Don
was to find the minimum number of hints required, not to minimize the computation time to find those hints. A faster algorithm is to ask for hints randomly, but that will require more hints. Don On Wednesday, July 2, 2014 11:20:32 AM UTC-4, vikas wrote: Don, if you are talking about cards

[algogeeks] Re: What is the algorithm for this problem

2014-07-01 Thread Don
from the list which are not consistent with that answer. Repeat the process until you have only one order remaining. Don On Tuesday, July 1, 2014 12:15:14 PM UTC-4, vikas wrote: One of my friend posted this question to me and I am unable to get anywhere. Could you guys please help A game

Re: [algogeeks] Re: Solving coin change problem with limited coins.

2014-05-20 Thread Don
Shashwat, very nice. Don -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com.

Re: [algogeeks] Re: Shortest path in grid with dynamic programming.

2014-05-20 Thread Don
BFS might be faster, though. Don On Tuesday, April 22, 2014 1:11:00 AM UTC-4, bujji wrote: Hi Don, At most we can reach a point from 4 adjacent points. So, time complexity will be O(XY) only. -Thanks, Bujji. On Mon, Apr 21, 2014 at 1:38 PM, bujji jajala jajal

Re: [algogeeks] Re: Solving coin change problem with limited coins.

2014-05-19 Thread Don
In my solution, the line which reads: if (sum result) sum = result; Should read if (sum result) result = sum; Don On Friday, May 16, 2014 11:51:52 PM UTC-4, Shashwat Anand wrote: @Don int coins [] = {1, 3, 5}; int cnt [] = {7, 3, 1}; int S = 9; Your code returns 9

Re: [algogeeks] Re: Solving coin change problem with limited coins.

2014-05-16 Thread Don
If you find a way to do that for more than a few coins I'd be interested in seeing it too. Don On Thursday, May 15, 2014 3:00:04 PM UTC-4, atul007 wrote: @Don : i am intersted in DP bottom up approach with less time complexity. solving it recursively is a simpler approach... On 15 May 2014

[algogeeks] Re: Solving coin change problem with limited coins.

2014-05-15 Thread Don
How about this: const int N = 6; unsigned int coins[N] = {100,50,25,10,5,1}; unsigned int count[N] = {2, 1, 1, 5, 4, 10}; int best = 10; int minCoins(int s, int f=0, int d=0) { if (d == 0) best = s; // It can never take more than s coins if (s 1) return 0; // No coins required

Re: [algogeeks] Remove minimum set of vertices to make the grap divided into more than one component

2014-05-15 Thread Don
Sid, Your proposal is to try removing every possible combination of nodes and for each case, do a DFS to determine if the graph is still connected? What is the time complexity of that approach? Don On Friday, May 9, 2014 4:58:15 AM UTC-4, siddharam wrote: use DFS remove 1( and all

[algogeeks] Re: Shortest path in grid with dynamic programming.

2014-04-21 Thread Don
. Don On Sunday, April 20, 2014 11:52:44 AM UTC-4, bujji wrote: Consider a city whose streets are defined by an X ×Y grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. Unfortunately, the city has bad neighborhoods, whose

[algogeeks] Re: Max difference of Monotonic Pair in An Array

2014-04-21 Thread Don
// The result will always include the smallest value of A[i] where i is less than j, // so just keep track of that minimum value as you scan through the array. int findPair(int *A, int N) { min = A[0]; max = 0; for(j = 1; j N; ++j) { if ((A[j] - min) max) max = A[j] -

[algogeeks] Re: Shortest path in grid with dynamic programming.

2014-04-21 Thread Don
to (%d, %d)\n, i,j); } } On Monday, April 21, 2014 11:08:55 AM UTC-4, Don wrote: Create a matrix distance[x][y] and set all values to a large number. This will represent the distance to travel to the destination on the shortest route. Now set the distance at the destination to zero. Set

[algogeeks] Re: Calculating C(n.r)

2014-04-09 Thread Don
Very nice solution. Don On Wednesday, April 9, 2014 12:55:43 AM UTC-4, Dave wrote: What you want to do is automate the process of cancelling common factors that you would do if you were calculating nCr by hand. Fill an array a[] with the numerator terms, n, n-1, n-2, ..., n-r+1

[algogeeks] Re: Calculating C(n.r)

2014-04-08 Thread Don
Use the fact that a*b*c*d = exp(log a + log b + log c + log d) double sum = 0.0; double x; for(x = n-r+1.0; x = n; x += 1.0) sum += log(x); for(x = 2; x = r; x += 1.0) sum -= log(x); result = exp(sum); Don On Tuesday, April 8, 2014 2:06:47 PM UTC-4, kumar raja wrote: Hi all, I know

[algogeeks] Re: Calculating C(n.r)

2014-04-08 Thread Don
Note that my example is for C(n,r), but you can use the same method for your second formula. Just add up the logs of whatever you multiply in the numerator and subtract the logs of the factors of the denominator. Then the result is exp of the resulting sum. Don On Tuesday, April 8, 2014 2:15

Re: [algogeeks] Re: maximum subsquare such that all four borders are filled black

2014-03-31 Thread Don
00 00 010100 011100 01 00 In this case, when i and j are 1, your algorithm will set s = 3. The if statement will fail, and it will never notice that it could have formed a square with s=2. Don On Sunday, March 30, 2014 9:49:21 AM UTC-4, atul007 wrote: @don : According

[algogeeks] Re: explanation of solution required.

2014-03-31 Thread Don
not give a result exactly equal to S. But if A contains positive real numbers I think that it will work. Don On Sunday, March 30, 2014 10:02:08 AM UTC-4, atul007 wrote: Hello, i found this question online and its solution tooBut i am not able to fully understand the solution.Need your

Re: [algogeeks] Re: maximum subsquare such that all four borders are filled black

2014-03-31 Thread Don
The only square is found when s=2. Your program will look at s=3 and not find a square, so it will never find the square. Try it and you will see what I mean.. Don On Monday, March 31, 2014 2:42:13 PM UTC-4, atul007 wrote: @Don: what is the point of considering s=2 when we have already

Re: [algogeeks] Re: maximum subsquare such that all four borders are filled black

2014-03-28 Thread Don
No, that is not the same. It won't find a square smaller than s. Don On Thursday, March 27, 2014 2:56:29 AM UTC-4, atul007 wrote: @Don : your algo time complexity is O(n^2) It can be reduced to this :- // Now look for largest square with top left corner at (i,j) for(i = 0; i n; ++i

Re: [algogeeks] Find 3 digit number.

2014-02-12 Thread Don
I like that Python code, and for three digits numbers it is just fine. If the number could be in a larger range, factoring out digits starting with larger digits should give the correct sequence, in reverse. If the number has a prime factor larger than 9, there is no solution. // Returns the

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-11 Thread Don
a number in the range 0..n-1 is to divide the output by 65535/n. Then check the result and if it is = n, repeat the process until you get a valid result. Don On Tuesday, February 11, 2014 1:18:26 AM UTC-5, SVIX wrote: :) true... that was silly of me.. I was too quick to post... anyway, the point

Re: [algogeeks] Re: Generate random number from 1 to 10.

2014-02-10 Thread Don
for a 64-bit multiply with carry generator, while what I posted was a 32-bit multiply with carry generator. Don On Saturday, February 8, 2014 2:26:34 AM UTC-5, atul007 wrote: @don : awesome +1 . I was not aware of George Marsaglia's Multiply With Carry generator. But it is soo clll

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-07 Thread Don
, 2014 1:47:15 PM UTC-8, Don wrote: Just noticed that you asked for 1-10, and my code will clearly generate 0-9. Add one for the desired range. Don On Wednesday, February 5, 2014 4:29:26 PM UTC-5, Don wrote: // Using George Marsaglia's Multiply With Carry generator int rnd10

Re: [algogeeks] Re: Generate random number from 1 to 10.

2014-02-07 Thread Don
[i] = n[j]; n[j] = i; } // n now contains a permutation of the values 1-10. On Thursday, February 6, 2014 11:33:53 PM UTC-5, atul007 wrote: @don: algo look fine..i tested it ,but it did not generate 1-10 with probabilty of 1/10 everytime. actually question was asked in an intrview

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-06 Thread Don
Just noticed that you asked for 1-10, and my code will clearly generate 0-9. Add one for the desired range. Don On Wednesday, February 5, 2014 4:29:26 PM UTC-5, Don wrote: // Using George Marsaglia's Multiply With Carry generator int rnd10() { static unsigned int x = time(0); x

Re: [algogeeks] Find longest consecutive subsequence

2014-01-30 Thread Don
No. If you start at any number in a sequence it will find the entire sequence. There is no need to start again at some other number in that sequence. Don On Wednesday, January 29, 2014 12:19:21 AM UTC-5, Varun Sachdeva wrote: If we don't process the same number more than once, does

Re: [algogeeks] Find longest consecutive subsequence

2014-01-27 Thread Don
This works, and I think is O(N*log(N)) which is similar to sorting and scanning. An unordered map will be faster, in general. It could be made faster in most cases by looping over items left in the map, to avoid processing the same number more than once. Also, when the number of items left in

[algogeeks] Re: changing structure of binary tree based on gravity and node selected.

2014-01-20 Thread Don
Good point, Vikas. What if the balls all have a negative charge and repel each other? On Thursday, January 16, 2014 6:36:06 AM UTC-5, vikas wrote: it will all collapse and form a single monolithic straight line with bunch of balls hanging in between :D On Wednesday, 15 January 2014

[algogeeks] Re: changing structure of binary tree based on gravity and node selected.

2014-01-15 Thread Don
It is no longer a binary tree. The node you pick may have three children. The path from the original root to that node will now be a third branch. Don On Wednesday, January 15, 2014 12:17:47 AM UTC-5, atul007 wrote: Imagine a binary tree lying on the floor with nodes as balls and edges

[algogeeks] Re: DISTINCT Permutations ( Not Easy)

2014-01-10 Thread Don
Sort the input string and remove duplicates, keeping a count of the number of occurrences of each character. They you can build the permutations easily. For your example, you would start with char *str = aba; int len = strlen(str); Which would be converted to char *str ab; int rep[N] =

Re: [algogeeks] Re: Median Finding in Sorted Arrays

2014-01-09 Thread Don
http://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/ This gives a reasonable solution. However, I would use iteration instead of tail recursion. Don On Monday, January 6, 2014 4:15:41 AM UTC-5, atul007 wrote: @dave : could you provide pseudo code for ur approach On 6

[algogeeks] Re: output

2013-12-09 Thread Don
Printf will print one value per item in the format string. If you don't supply that many, it will look at the undefined memory where that value should have been, so you will get gibberish. Don On Friday, December 6, 2013 9:17:36 AM UTC-5, segfault wrote: Hi All, I'm not able to get output

Re: [algogeeks] Re: Saving and restoring Binary trees in files

2013-11-13 Thread Don
, 'B' if it has only a left subtree, 'C' if it has only a right subtree, and 'D' if it has both left and right subtrees. Then you read the line, store the string minus the first character, and recursively build the left and then right subtree, as appropriate. Don On Monday, November 11, 2013 1

Re: [algogeeks] Re: Saving and restoring Binary trees in files

2013-11-11 Thread Don
; } On Friday, November 8, 2013 1:00:35 PM UTC-5, atul007 wrote: @don : it is not BST , it is binary tree ...so your approach will not work in this case. @kumar : save pre-order and in-order traversal with some delimiter in b/w traversals. pre-order : a b c d e in-order : c b e a d

[algogeeks] Re: Saving and restoring Binary trees in files

2013-11-08 Thread Don
Save it in pre-order. Rebuild by inserting nodes in the order they occur in the file. On Friday, November 8, 2013 8:33:19 AM UTC-5, kumar raja wrote: What is the effective way to save and restore the binary trees to files effectively? Regards, Kumar Raja. -- You received

Re: [algogeeks] Message distribution through rooted tree dynamic programming

2013-11-06 Thread Don
Atul, the depth is the important factor, not the number of nodes. If you always pass the message to the deeper subtree first, followed by the other subtree, you get the minimum number of rounds. The problem is to compute the required number of rounds. This can be done in a way similar to

Re: [algogeeks] Message distribution through rooted tree dynamic programming

2013-11-06 Thread Don
Atul, the depth is the important factor, not the number of nodes. If you always pass the message to the deeper subtree first, followed by the other subtree, you get the minimum number of rounds. The problem is to compute the required number of rounds. This can be done in a way similar to

[algogeeks] Re: Matchbox Game from Amazon Ninja Coder Challenge 2013

2013-10-28 Thread Don
that, you can solve a big problem easily. Don On Monday, October 28, 2013 3:22:54 AM UTC-4, robin wrote: We have a game of matchboxes with the following rules: * It is a 2-player game. * There are n matchboxes with varying number of matchsticks in them. The n matchboxes are numbered from 1

[algogeeks] Re: Generating random number without using rand()

2013-10-11 Thread Don
and the statistical properties are not important, you can do something really simple like: int x = (time() * getpid()) % N; Don On Thursday, October 10, 2013 5:51:42 AM UTC-4, atul007 wrote: Hello, Given integer N , How to generate random number from 0 to N without using rand() function

[algogeeks] Re: Masked random generator

2013-10-10 Thread Don
is x+min. return x+min; } You can prove that it works by trying all possible values of x and verifying that it produces the set of valid responses. Don On Thursday, October 10, 2013 12:41:10 AM UTC-4, Dave wrote: @Don: Note that a[j] - j is the number of non-excluded responses a[j]. Here

Re: [algogeeks] Masked random generator

2013-10-09 Thread Don
You can do a lot better than N log(N). Don On Wednesday, October 9, 2013 2:03:02 PM UTC-4, hppygrry wrote: What's wrong with just a simple approach with checking the values in the array[s] ? Since its already sorted, it takes log(s) time. Assuming S approaches N, we are still only

Re: [algogeeks] Masked random generator

2013-10-09 Thread Don
Describe in more detail how you would use reservoir sampling. What would be the memory requirement? There is a solution to this problem which is O(log S) time and constant memory. Don On Tuesday, October 8, 2013 2:24:54 PM UTC-4, atul007 wrote: can't we use idea of reservoir sampling here

[algogeeks] Masked random generator

2013-10-08 Thread Don
Provide a function which returns a value randomly and uniformly selected from the range 0..N-1 excluding values in the array a[S] containing sorted values in the same range. A rejection algorithm would work well enough if S is much smaller than N, but what if N is large and S is slightly

[algogeeks] Re: find the contiguous subarray which produce largest sum

2013-08-30 Thread Don
Use Kadane's algorithm. http://en.wikipedia.org/wiki/Maximum_subarray_problem Don On Thursday, August 29, 2013 11:09:59 PM UTC-4, yash wrote: 1. find largest sum contiguous subarray 2. find the contiguous subarray which produce largest sum Ex: a[7] = {2,1,-7,5,8,-1,-3} Ans is: a[3

[algogeeks] Re: Array problem

2013-08-08 Thread Don
? Don On Thursday, August 8, 2013 1:02:08 AM UTC-4, payel roy wrote: There is a stream of numbers coming in and you have to find K largest numbers out of the numbers received so far at any given time. Next problem is that a constraint is added. memory is limited to m. m k. How would you

[algogeeks] Re: why does this work?

2013-07-18 Thread Don
; printf(%d\n, x); You know that x=*temp; will crash, but I'm guessing that it will not reach the first printf. Don On Thursday, July 18, 2013 9:33:51 AM UTC-4, phoenix wrote: I tried the following on ideone #include stdio.h int main(){ int *temp; printf http://www.opengroup.org

Re: [algogeeks] Re: why does this work?

2013-07-18 Thread Don
Just mail me a check for my drinks. Thanks! Don On Thursday, July 18, 2013 2:43:46 PM UTC-4, .bashrc wrote: Hi all, Party at my house on 28 July, 5 pm. Unlimited drinks. All are welcome. :) Regards, Dinesh Sohu On Thu, Jul 18, 2013 at 9:19 PM, Don dond...@gmail.com javascript:wrote

Re: [algogeeks] Re: determine if 2 BSTs will be identical or not based on input order of its keys

2013-07-16 Thread Don
partition algorithms are not stable. That's really the question I was asking. Can you do that partition in place? Don On Sunday, July 14, 2013 2:48:46 PM UTC-4, sourabh wrote: You don't need to take vector for this you can have input in array also. You just need to check the elements in each

Re: [algogeeks] Re: determine if 2 BSTs will be identical or not based on input order of its keys

2013-07-12 Thread Don
.push_back(t1[i]); if (t2[i] t2[0]) left2.push_back(t2[i]); else right2.push_back(t2[i]); } result = sameBstFormed(left1, left2) sameBstFormed(right1, right2); } return result; } On Tuesday, July 9, 2013 5:41:57 PM UTC-4, Don wrote: Yes, you are right. Good catch

[algogeeks] Re: determine if 2 BSTs will be identical or not based on input order of its keys

2013-07-09 Thread Don
The trees would be the same if 1. The root is the same 2. The left and right subtrees are both the same bool sameBstFormed(intVector t1, intVector t2) { if (t1.size() != t2.size()) return false; // Trees with different number of nodes can't be the same if (t1.size() == 0) return

Re: [algogeeks] Re: determine if 2 BSTs will be identical or not based on input order of its keys

2013-07-09 Thread Don
Yes, you are right. Good catch. On Tuesday, July 9, 2013 2:13:47 PM UTC-4, Sambhavna Singh wrote: Amazing solution Don, thank you :) You missed a small check in the code: if(t1[0] != t2[0]) return 0; On Tue, Jul 9, 2013 at 11:27 PM, Don dond...@gmail.com javascript:wrote: The trees

[algogeeks] Re: biginteger

2013-07-08 Thread Don
Get the NTL library: http://www.shoup.net/ntl/ ZZ is the extended precision integer class. Don On Saturday, July 6, 2013 2:31:02 PM UTC-4, subrat kumar prasad wrote: can anyone provide working code for biginteger in c++? please upload!! -- You received this message because you

Re: [algogeeks] Adobe question

2013-06-26 Thread Don
for N+1 using only the N+1 smallest letters. Don On Tuesday, June 25, 2013 7:14:53 AM UTC-4, jagannath wrote: Is this not similar to knapsack problem? On Fri, Jun 21, 2013 at 11:24 AM, Ravi Ranjan ravi.c...@gmail.comjavascript: wrote: There is a Blank Paper Sheet, Given a list

[algogeeks] Re:

2013-06-21 Thread Don
int bitCount[N]; bitCount[0] = 0; for(int i = 1; i N; ++i) bitCount[i] = bitCount[i1] + (i1); On Thursday, June 20, 2013 11:03:35 PM UTC-4, shubham saini wrote: How to count no of set bits for all numbers from 1 to n for a given n... i knew brute force any better solution ?? -- You

[algogeeks] Re:

2013-06-21 Thread Don
int bitCount(int n) { if (n 3) return n; int x=31-__builtin_clz(n); n -= 1x; return x*(1(x-1)) + bitCount(n) + n + 1; } On Thursday, June 20, 2013 11:03:35 PM UTC-4, shubham saini wrote: How to count no of set bits for all numbers from 1 to n for a given n... i knew brute force

Re: [algogeeks] Re:

2013-06-21 Thread Don
plus the number of bits in 0..n-2^x. That is what the last line computes. Don On Friday, June 21, 2013 1:42:08 PM UTC-4, shubham saini wrote: thanx Don for your algos, bt i m not able to understand your second approach can you please explain it a liitle On Fri, Jun 21, 2013 at 9:50 PM

[algogeeks] Re: Sorting a very large number of intergers

2013-06-14 Thread Don
It the four threads have to share the bottleneck resource, you won't get anything close to a 4x speedup. That's all I'm saying. Don On Jun 13, 11:17 pm, nick tarunguptaa...@gmail.com wrote: Yes the process is IO bound but you have to read them anyways! Let's say you are unable to do parallel IO

[algogeeks] Re: Sorting a very large number of intergers

2013-06-13 Thread Don
I should mention that this process is file I/O bound, so having four threads will not help significantly unless each thread can use a different disk device. Don On Jun 13, 3:31 pm, Don dondod...@gmail.com wrote: I would suggest splitting the data into four pieces and having each thread do

[algogeeks] Re: Sorting a very large number of intergers

2013-06-13 Thread Don
I would suggest splitting the data into four pieces and having each thread do an external sort on one piece. Essentially that means breaking up the data into chunks which can be stored in memory, sorting them, and then merging the chunks back into one piece. After the 4 threads are done sorting

[algogeeks] Re: Intrestting problem

2013-06-12 Thread Don
It touches an edge of the region, and is therefore not surrounded. On Jun 12, 2:39 am, Nishant Pandey nishant.bits.me...@gmail.com wrote: juat want to ask why the last region was not flipped from o to x? On Tue, Jun 11, 2013 at 11:57 PM, Don dondod...@gmail.com wrote: // Flip a region

[algogeeks] Re: Intrestting problem

2013-06-11 Thread Don
// Flip a region including location (x,y) from from to to. // Returns true if region is surrounded. bool flip(char board[][], int n, int x, int y, char from, char to) { if ((x = 0) (y = 0) (x n) (y n)) return false; if (board[x][y] != from) return true; board[x][y] = to; return

[algogeeks] Re: searching all matching words in a Trie with a given filter.

2013-05-30 Thread Don
It won't use a lot of stack space because the stack space is related to the depth of the trie which is only as deep as the length of the longest word. But I'm all for doing it iteratively. Don On May 30, 7:57 am, avinesh saini avinesh.sa...@gmail.com wrote: Don, I'm trying to get all the words

[algogeeks] Re: searching all matching words in a Trie with a given filter.

2013-05-30 Thread Don
Sorry, that was not really clear. I was just adding the newly found word to a list of words. That list will be the output. I treated it as if it was declared globally. It would be better form to pass the list in as a reference, or to have the function return the list. Don On May 29, 12:14 am

[algogeeks] Re: searching all matching words in a Trie with a given filter.

2013-05-29 Thread Don
) findWords(root-link[i], filter+1, word+i); } else // Search for words with the required letter { findWords(root-link[*filter], filter+1, word+*filter); } } On May 28, 11:36 pm, avinesh saini avinesh.sa...@gmail.com wrote: Thank you Don, I was also trying in similar

[algogeeks] Re: Minimum number of coins to SUM to S

2013-05-28 Thread Don
coins. Don #include stdio.h int main() { const int numCoins = 6; const int maxS = 1; int coins[numCoins] = {1,5,10,25,50,100}; // You can change this if you want different sets of coins int min[maxS]; int i, j; for(i = 1; i maxS; ++i) min[i] = 10; min[0

[algogeeks] Re: searching all matching words in a Trie with a given filter.

2013-05-28 Thread Don
void findWords(trie *root, char *filter) { if (!root) return; if (*filter == 0) // When you reach the end of the filter at the end of a valid word, add the word. { if (root-words) words.add(root-word); } else if (*filter == '.') // Search for words with any letter

[algogeeks] Re: PLease suggest the Algo for this problem

2013-05-23 Thread Don
This is not necessarily possible. If you have teams A, B, and C, it is possible that A beat B B beat C C beat A. Based on the first two games the ranking should be A,B,C. But based on the third game, C should be ranked higher than A. Don On May 23, 11:06 am, Nishant Pandey nishant.bits.me

[algogeeks] Re: PLease suggest the Algo for this problem

2013-05-23 Thread Don
If you create a directed graph where each node is a team and an edge exists from A-B if A lost to B, then find a Hamiltonian Path in the graph. That path will be the sequence you need. Don On May 23, 12:02 pm, bharat b bagana.bharatku...@gmail.com wrote: http://www.careercup.com/question?id

[algogeeks] Re: count number of set bits in an (big) array (Asked in Google interview)

2013-05-22 Thread Don
My program works with any numbers. Don On May 22, 3:45 am, Pramida Tumma pramida.tu...@gmail.com wrote: This above program works only if the array contains consecutive numbers starting from 1 to n. What to do if the array contains random numbers? On Fri, May 17, 2013 at 6:55 PM, Don

[algogeeks] Re: stack. Mid element in o(1)

2013-05-22 Thread Don
Do you mean the middle position or median value? If you implement the stack in an array, finding the middle position should be easy. Don On May 22, 11:45 am, MAC macatad...@gmail.com wrote: Saw this on  a seperate group .. Since answer not known to me sharing it here you have a stack

[algogeeks] Re: stack. Mid element in o(1)

2013-05-22 Thread Don
, but as an array .. thanks --mac On Wed, May 22, 2013 at 9:34 PM, Don dondod...@gmail.com wrote: Do you mean the middle position or median value? If you implement the stack in an array, finding the middle position should be easy. Don On May 22, 11:45 am, MAC macatad...@gmail.com wrote

[algogeeks] Re: count number of set bits in an (big) array (Asked in Google interview)

2013-05-17 Thread Don
Counting the set bits in one integer is not the problem which was asked. However, I think that something like this is both faster and more easy to understand than what you have written: int bitCount(unsigned int x) { int result = 0; while(x) { if (x 1) ++result; x = 1; }

[algogeeks] Re: Rope Data Structure Implementation

2013-05-17 Thread Don
http://en.wikipedia.org/wiki/Rope_(data_structure) I don't know if this will make it easy, but it should help. On May 17, 4:28 am, Nishant Pandey nishant.bits.me...@gmail.com wrote: I want to implement rope data stucture from scratch in c , is there any good material that can help me implement

[algogeeks] Re: count number of set bits in an (big) array (Asked in Google interview)

2013-05-16 Thread Don
I don't think that there is a shortcut, if the array contains arbitrary data. You'll want to create an array of bit counts for either 8 or 16 bits. I would go with 16. int bitCount[65536]; // Do this once at initialization time void init() { bitCount[0] = 0; for(int i = 1; i 65536; ++i)

[algogeeks] Re: Print a Random word from a file. Input is path to a file, constraints- No extra memory like hashing etc. All the words in the file should have equal probability.

2013-05-14 Thread Don
, you can't do that in O(n) time and constant space. Don On May 7, 8:49 am, Nishant Pandey nishant.bits.me...@gmail.com wrote: i am little confused with the solution, like if i say there are duplicate words,  will this algo produce the words with equal probability? On Mon, May 6, 2013 at 7

[algogeeks] Re: For a given set of points find a point along with 3 others that are closest to each other

2013-05-03 Thread Don
is O(N). Don On May 2, 12:49 pm, Nishant Pandey nishant.bits.me...@gmail.com wrote: Guys i am looking for optimize algorithm for this, please help. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To unsubscribe from this group and stop receiving

[algogeeks] Re: max path

2013-05-02 Thread Don
, but if there are very many mines, finding the best solution becomes very expensive. Don On Apr 29, 6:42 am, sreekanth guru sreekanth.i...@gmail.com wrote: Problem: We have m * n grids. Each grid can have one of earth/water/mines. You can go to top/bottom/left/right adjacent grids. You can go

[algogeeks] Re: Find the number of islands/connected components

2013-04-30 Thread Don
the complexity is O(row*col). Don On Apr 26, 8:19 am, rahul sharma rahul23111...@gmail.com wrote: got the islands...but first we scan each element then also dfs for them if all are 1..then how it can be o(row*col)...plz explain me complexity ofr this On Fri, Apr 26, 2013 at 2:07 PM

[algogeeks] Re: Median of two sorted arrays of different sizes

2013-04-24 Thread Don
if (B[j+1] A[i]) max = i-1; else break; } printf(Median is %d\n, (A[i] B[j]) ? A[i] : B[j]); Don On Apr 24, 1:19 pm, rahul sharma rahul23111...@gmail.com wrote: IS this code correct? float findMedianUtil( int A[], int N, int B[], int M ) {     // If the smaller array has only one

[algogeeks] Re: least common ancestore bst

2013-04-22 Thread Don
There is no need to use recursion here. Tail recursion is essentially using recursion as an expensive loop. int leastCommonAncestor(struct node *root, int n1, int n2) { while(root) { if ((root-data == n1) || (root-data == n2) || ((root-data n1) != (root-data n2))) return

[algogeeks] Re: Find words in string

2013-04-18 Thread Don
. Don On Apr 18, 12:04 am, marti amritsa...@gmail.com wrote: Given a list of words wordlist on 1st line (no of words = 100) and a string qstr of len = 1 million on 2nd line, print the index at qstr where a continuous substring exists that contains all the given words in wordlist. -- You

[algogeeks] Re: Ceil in sorted array

2013-04-18 Thread Don
Yes, you can use mid+2 there. On Apr 17, 1:23 pm, rahul sharma rahul23111...@gmail.com wrote: following is logn soln int ceilSearch(int arr[], int low, int high, int x) {   int mid;   /* If x is smaller than or equal to the first element,     then return the first element */   if(x =

[algogeeks] Re:

2013-04-17 Thread Don
is known at node A ,whether it is up or down. On Tue, Apr 16, 2013 at 12:23 AM, Don dondod...@gmail.com wrote: I think that is called a load leveler. It needs to keep track of the status of the resources, and the capacity of each one, when means that it must be able to detect when

[algogeeks] Re: algorithm to sort based on frequency.

2013-04-16 Thread Don
I have not tested this, but it should get you started: struct item { int val; int freq; int index; }; // Compare function for qsort. // Returns 1 if a occurs with greater frequency than b // Returns -1 if b occurs with greater frequency than a // Otherwise returns 1 if a is encountered

[algogeeks] Re: Primality testing

2013-04-15 Thread Don
for more information. Don On Apr 14, 1:16 pm, payel roy smithpa...@gmail.com wrote: You are to test whether a number if prime or not. Digit of that number can be as large as 1000. How do you do that? I was thinking of basic idea. a) First I shall calculate all primes which is less than

[algogeeks] Re: Primality testing

2013-04-15 Thread Don
For numbers larger than what can be stored in your computer's native integer types, you should use an extended precision math library. NTL is one such option. It provides a type called ZZ which supports integer operations where the size is limited only by your computer's memory and speed. Don

[algogeeks] Re: Find the number of islands/connected components

2013-04-10 Thread Don
Reformatting to make it easier to see: 11000 01001 10011 0 10101 In this case an island is any set of 1's which are connected vertically, horizontally, or diagonally. So the five islands are 11000 01002 10022 0 30405 Don On Apr 10, 4:34 pm, rahul sharma rahul23111...@gmail.com wrote

[algogeeks] Re: Interview question

2013-04-10 Thread Don
M is a matrix, not a number. M is NxN, so the algorithm is O(N^3) as stated in the text. On Apr 10, 4:19 pm, rahul sharma rahul23111...@gmail.com wrote: isnt the complexity should be o(m*n*n) instead of (n*n*n) as m can be greater than n..plz comment On Wed, Apr 10, 2013 at 10:11 PM, rahul

[algogeeks] Re: Dlete Linked List

2013-04-09 Thread Don
head is not even declared, so I doubt that it would compile. I believe that you want to free head, not next. On Apr 9, 11:31 am, rahul sharma rahul23111...@gmail.com wrote:  Is the following code correct for linked list deletion or i need to copy head in some tem. pointer and dlete and

[algogeeks] Re: Dlete Linked List

2013-04-09 Thread Don
is pointing to a node which has already been deallocated. Don On Apr 9, 2:22 pm, rahul sharma rahul23111...@gmail.com wrote: sory..following is correct code void deleteList(struct node** head) {    /* deref head_ref to get the real head */      struct node* next;    while (*head != NULL

[algogeeks] Re: Amazon interview question

2013-04-09 Thread Don
, at which point it will crash. Don On Apr 9, 2:29 pm, rahul sharma rahul23111...@gmail.com wrote:  A = {5, 3, 8, 9, 16} After one iteration A = {3-5,8-3,9-8,16-9}={-2,5,1,7} After second iteration A = {5-(-2),1-5,7-1} sum =7+(-4)+6=9 Given an array, return sum after n iterations my sol/ void

[algogeeks] Re: Amazon interview question

2013-04-09 Thread Don
. For bonus points, can you determine how this problem relates to Pascal's Triangle. Don On Apr 9, 2:43 pm, rahul sharma rahul23111...@gmail.com wrote: If you have any other solution ..please post that...i thnik recursion is ok with base case...we need to scan again after first iteration

[algogeeks] Re: Get Target

2013-04-04 Thread Don
Use a backtracking search to build a prefix expression. If there are two more operands than operators in the expression, the next item could be either an operand or an operator. Otherwise, it must be an operand. In very loose pseudocode: search(int target, list operands, stack opStack, string

[algogeeks] Re: how to solve this?

2013-04-04 Thread Don
Simplify the expression by evaluating expressions inside parenthesis first. Follow the order of evaluation, doing multiplications first and then addition and subtraction. It should be possible to reduce any expression to the form ax+b=0. Then x=-b/a. Don On Apr 4, 11:18 am, arun kumar kumar0

[algogeeks] Re: Get Target

2013-04-04 Thread Don
I meant postfix, of course. Don On Apr 4, 10:32 am, Don dondod...@gmail.com wrote: Use a backtracking search to build a prefix expression. If there are two more operands than operators in the expression, the next item could be either an operand or an operator. Otherwise, it must be an operand

[algogeeks] Re: how to solve this?

2013-04-04 Thread Don
I like that solution better than the one I suggested. Don On Apr 4, 4:45 pm, Dave dave_and_da...@juno.com wrote: @Kumar0746: Technically, you can't solve an _expression_; you can solve an _equation_, which is a statement of the form expression = expression, which is what you have. Don's

[algogeeks] Re: facebook programming question

2013-03-25 Thread Don
they are asking for, you have to find the median location for each distinct value in the array and find the sum of the differences between each value and the median. I would consider using a map to store the locations indexed by value. Then iterate over the map and process each set of locations one by one. Don

[algogeeks] Re: separate coins into heaps of similar weights using balance in minimum comparisons

2013-03-05 Thread Don
I don't see how a statement like 3 coins together weigh x kg provides any new information. Using a binary search algorithm you should be able to find any coins which weigh the same in 17 comparisons in the worse case. On Mar 2, 12:42 am, Shubham Sandeep s.shubhamsand...@gmail.com wrote: @dave

[algogeeks] Re: Number of paths

2013-02-21 Thread Don
precisely is very important. BFS is not a workable solution. For any matrix larger than about 10x10 it will take longer than you want to wait. Don On Feb 21, 2:56 am, shady sinv...@gmail.com wrote: Given a matrix of size mXn, find the number of paths from the top left cell to the bottom right cell

[algogeeks] Re: Amazon Interview Question

2013-02-13 Thread Don
that in your input array and you'll find that you don't get the desired output. I don't know of a solution better than sorting and scanning the array. Don On Feb 12, 3:14 pm, prakhar singh prakharsngh.1...@gmail.com wrote: #includestdio.h int main() {    int a[] = {2,2,3,3,3,1,1,4,4

  1   2   3   4   5   6   >