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

2013-04-30 Thread Don
constants, so the complexity is O(row*col). Don On Apr 26, 8:19 am, rahul sharma 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 > > > > > > > &g

[algogeeks] Re: max path

2013-05-02 Thread Don
ood solution, but if there are very many mines, finding the best solution becomes very expensive. Don On Apr 29, 6:42 am, sreekanth guru 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 to

[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
time complexity is O(N). Don On May 2, 12:49 pm, Nishant Pandey 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 re

[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 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 Mo

[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) b

[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 wrote: > I want to implement rope data stucture from scratch in c , is there any > good material that can help me implement this with ease. > > Thanks

[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 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 1

[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 wrote: > Saw this on  a seperate group .. Since answer not known to me sharing it > here > > you have a sta

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

2013-05-22 Thread Don
it as a stack , but as an array .. > > thanks > --mac > > > > > > > > On Wed, May 22, 2013 at 9:34 PM, Don wrote: > > Do you mean the middle position or median value? > > > If you implement the stack in an array, finding the middle position > > should be

[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 wrote: > I hav

[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 wrote: > http://www.careercup.com/question?id=14804702 > > On

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

2013-05-28 Thread Don
coins. Don #include 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: searching all matching words in a Trie with a given filter.

2013-05-29 Thread Don
for(int i = 'a'; i <= 'z' ; ++i) 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 wrot

[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 wrote: > Don, I'm trying to get all the words fro

[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: 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;

[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 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 wrote: > > // Flip a region in

[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 wrote: > I would suggest splitting the data into four pieces and having each > thread do an external s

[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 the

[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 wrote: > Yes the process is IO bound but you have to read them anyways! > Let's say you are unable to do pa

[algogeeks] Re:

2013-06-21 Thread Don
int bitCount[N]; bitCount[0] = 0; for(int i = 1; i < N; ++i) bitCount[i] = bitCount[i>>1] + (i&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 any better solution ?? > -

[algogeeks] Re:

2013-06-21 Thread Don
int bitCount(int n) { if (n < 3) return n; int x=31-__builtin_clz(n); n -= 1< > 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 received this message because you are subscribed to the Google Groups "Algorit

Re: [algogeeks] Re:

2013-06-21 Thread Don
is set 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,

Re: [algogeeks] Adobe question

2013-06-26 Thread Don
ould save it and try 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 > > > wrote: > >> There is a Bl

[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

[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 true;

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 >wrote: > >>

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

2013-07-12 Thread Don
right1.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: > > Y

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 t

[algogeeks] Re: why does this work?

2013-07-18 Thread Don
x = *temp; 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 > int mai

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

[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

[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,

[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 smaller

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,

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 sa

[algogeeks] Re: Masked random generator

2013-10-10 Thread Don
ult, so the xth value 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 n

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

2013-10-11 Thread Don
t need one 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 wi

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

2013-10-28 Thread Don
. Once you know 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

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 comput

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 comput

[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. > --

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

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

2013-11-13 Thread Don
is a leaf, '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 appropria

[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&

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 ap

[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] = {2,

[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: 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 10:

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 t

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,

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

2014-02-05 Thread Don
// Using George Marsaglia's Multiply With Carry generator int rnd10() { static unsigned int x = time(0); x = 63663 * (x&65535) + (x>>16); return (x & 65535) % 10; } On Sunday, February 2, 2014 2:51:47 AM UTC-5, atul007 wrote: > > Generate random number form 1 - 10 with probability of 1/10

[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

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

2014-02-07 Thread Don
gt; On Thursday, February 6, 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: >>> &

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

2014-02-07 Thread Don
mp; 65535) % (i+1); n[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. > actu

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

2014-02-10 Thread Don
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 i

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

2014-02-11 Thread Don
generate 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

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 sm

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

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 : Accord

[algogeeks] Re: explanation of solution required.

2014-03-31 Thread Don
might 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

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 a

[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: >

[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

[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, ...,

[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 ne

[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
+1] == distance[i][j]-1) ++j; else if (distance[i][j-1] == distance[i][j]-1) --j; printf("Move 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 rep

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

2014-04-22 Thread Don
Good catch. A function called "markShortestPath" ought to mark the shortest path, huh? Don On Monday, April 21, 2014 4:38:32 PM UTC-4, bujji wrote: > > > Nice solution. good. Looks like in your markShortestPath(int > i, int i, int d) function > you

[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 > > > re

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.

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

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,

[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 he

[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

[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-09 Thread Don
not distinguish those cards. So a valid combination of hints has a non-zero bitwise AND with all values in the pairs list. Find the value of i with the fewest bits which meets this condition. Don On Tuesday, July 1, 2014 12:15:14 PM UTC-4, vikas wrote: > > One of my friend posted this ques

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

2014-07-09 Thread Don
// This function returns the number of hints required to // determine the order of the cards. // Parameter int c[n] specifies n cards which are provided // in an unknown order. Each card is represented as a 10-bit field // with two bits set, one representing the color of the card and the // other r

[algogeeks] Mastermind

2015-04-30 Thread Don
determine the correct response to a guess. The algorithm should require a feasible amount of memory for P<1000 and C<1000 (meaning you can't have a table of size C^P). Don -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To

[algogeeks] Re: question at K10

2011-02-15 Thread Don
void change() { printf("10"); while(1) {} } On Feb 15, 10:17 am, Balaji S wrote: > Insert only one line in the function change() so that the output of the > program is 10. > You are not allowed to use exit(). You are not allowed to edit the function > main() or to > pass the parameter to change

[algogeeks] Re: question at K10

2011-02-15 Thread Don
A semicolon is valid in the middle of a line in C or C++. For instance, no one says that for(i = 0; i < 10; ++i) is three lines of code. Don On Feb 15, 11:31 am, jalaj jaiswal wrote: > after termination of semicolon , that will be considered a separate line i > guess > > >

[algogeeks] Re: Help

2011-02-15 Thread Don
Mike Johnson wrote: > Plesae rite a program for me to find prime nummers. It should be recursive > prorgram. What duz that mean? > If u type a nummer like 10 it should say "1 is prime, 2 is prime, 3 is prime, > 4 is not prime" up to 10. > This iz not homewurk I just thout of it myself. Lol. /* S

[algogeeks] Re: General Sorting Header File Adobe

2011-02-16 Thread Don
template void sort(Iterator first, Iterator last, Compare cmp); On Feb 16, 5:57 am, bittu wrote: > write header file for sorting general element. it can sort any type of > object(int,float,complex number, objects) > > Thanks > Shashank -- You received this message because you are subscribed to

[algogeeks] Re: Puzzle For Puzzled Minds - Robots

2011-02-16 Thread Don
ed of 1 space every third turn until you find the other robot's starting spot SKIP NEXT INSTRUCTION IF OIL GOTO Search Fast: RIGHT // Move at speed of 1 space every other turn GOTO Fast Both robots will move to the right, but the one on the left will eventually find the oil left by the other rob

[algogeeks] Re: Maze & labyrinth Solve Puzzle & WAP Efficiently

2011-03-02 Thread Don
class coord { public: int x; int y; }; class SolveMaze { public: SolveMaze() { location.x = location.y = 0; } bool Explore(); private: list visited; coord location; }; bool Explore() { // Determine if we've already been here if (visited.found(location)) return false; // Bas

[algogeeks] Re: 2march

2011-03-02 Thread Don
Did he get frostbite on his toes? On Mar 2, 1:54 am, Lavesh Rawat wrote: > *WalK The River Problem Solution* > * > *A whole village crowded together at the edge of the lake, they all came for > a common purpose. > To see the man who could walk on water. > Many bets were placed, many a dreamer ima

[algogeeks] Re: Tough Problem of Probability ..Surely Will Stuck ..So Think More & More

2011-03-02 Thread Don
Game 2 is better if p > 0.5. P(win game 1) = p P(win game 2) = p^3 + 3(p^2 * (1-p)) To find the solution, solve for p when p^3 + 3(p^2 * (1-p)) > p Which simplifies to 3p - 2p^2 > 1 Which is true when p > 0.5 On Mar 1, 5:15 pm, bittu wrote: > You have a basketball hoop and someone says that

[algogeeks] Re: brain teaser

2011-03-03 Thread Don
be to make the total odd or even, as indicated by the previous prisoner. Each prisoner in line must keep track of the current state, odd or even, which changes each time someone behind him indicates having a red hat. Don On Mar 3, 2:18 am, freecoder wrote: > You are one of 20 prisoners on death

[algogeeks] Re: Maze & labyrinth Solve Puzzle & WAP Efficiently

2011-03-03 Thread Don
The problem with BFS is that you have to remember how to get from where you are to every other point you have explored. Of course, the problem with a depth-first search is that in an unbounded maze, you may never reach a point even if it is very close to your starting point. Don On Mar 3, 1:31

[algogeeks] Re: Clock Algorithm

2011-03-04 Thread Don
I'm not sure what you mean. You might use an priority queue to store scheduled events and process them in time order. On Mar 4, 10:27 am, Luciano Junior wrote: > Hello, > > I need a clock algorithm to use with in a simulation system that I be > creating. > > Someone have any Idea ? > -- > --

[algogeeks] Re: linked list

2011-03-16 Thread Don
Another alternative to a void pointer is a union. Typically you would also want an enumerated type to indicate the actual type stored in the union. On Mar 16, 9:57 am, himanshu kansal wrote: > can nodes of linked list in c/c++ contain different types of > datameans suppose 1st node of the lis

[algogeeks] Re: A Billion Number Question

2011-03-17 Thread Don
This only works if the file is sorted. If the file starts out with values 5,7,6,... and never contains another 7, the result will be 7, which is in the file. On Mar 17, 12:19 pm, "arpit.gupta" wrote: > read the first no. . > now ans= first no +1; > if now ans is encountered while reading the next

[algogeeks] Re: random number generator

2011-03-17 Thread Don
bool uniform() { static bool state = true; state = !state; return state ^ nonuniform(); } On Mar 17, 9:24 am, saurabh agrawal wrote: > Given a  function which returns true 60% time and false 40% time. > > Using this function you have to write a function which returns true 50% of > the time.

[algogeeks] Re: Print Hello

2011-03-17 Thread Don
int n = printf("Hello\n"); int main() { } On Mar 17, 8:08 am, himanshu kansal wrote: > is there any way to print hello in c also wdout writing anythng in > main() > > On Thu, Mar 17, 2011 at 6:34 PM, kunal srivastav > > wrote: > > n...c does not support classes > > > On Thu, Mar 17, 20

[algogeeks] Re: random number generator

2011-03-22 Thread Don
Which is why stating the requirements precisely is important. If modeling a coin toss is the goal, this should work. bool uniform() { static unsigned int x = 1234567; x = 63663*(x&65535)+(x>>16); return ((x&64)==0) ^ nonUniform(); } On Mar 17, 3:29 pm, Dave

[algogeeks] Re: random number generator

2011-03-22 Thread Don
There is no upper bound on the runtime of this function. On Mar 20, 4:33 pm, Jammy wrote: > clearly the prob. of getting true|false and false|true are equal to > 0.6*0.4. Therefore the following code works, > > bool uniform(){ >    bool f1; >    bool f2; >    do{ >        f1 = non_uniform(); >  

<    1   2   3   4   5   6   >