[algogeeks] Re: random number generator

2011-03-20 Thread Jammy
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(); f2 = non_uniform(); }while(!(f1 ^ f2)); return f1; } On Mar 17, 10:24 am, saurabh agrawal wro

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

2011-03-03 Thread Jammy
BFS search the maze On Mar 2, 11:26 am, Don wrote: > 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() > { >   //

[algogeeks] Re: sort partially sorted linked list

2011-03-03 Thread Jammy
More comments on Ashish's approach. When implementing, you could reverse the list when you see it's in descending order. Then merge would be easier. On Mar 3, 1:03 am, Ashish Goel wrote: > find two consecutive sequences(can be two increasing2I, 1i1d,1d1i,2D), merge > them and then merge every nex

[algogeeks] Re: Missing Number in New Way Please Read Question Carefully..

2011-03-03 Thread Jammy
for numbers from 0 to n, if you count the numbers whose LSB is 0 vs. those has 1, if count(0) > count(1), the missing number's LSB is 1, otherwise it's 0. Continue for the second LSB, your can find the missing number bitwise. On Mar 1, 3:14 pm, bittu wrote: > An array A[1...n] contains all the in

[algogeeks] Re: Amazon Interview question

2011-03-01 Thread Jammy
@gaurav. They way I see it it's O(nlogn)? you have n/4 for each level of the recursion tree and logn height. In total its O(nlogn) On Feb 28, 9:27 pm, Vinay Pandey wrote: > Hi, > > Here is my solution, let me know: > > /* a helper function */ > void swap(int* arr, int index1, int index2) { > /

[algogeeks] Re: Robot Moving on The Maze..Need All possible Path

2011-03-01 Thread Jammy
DFS would work I think, if you try to find shortest path, use BFS On Mar 1, 3:30 pm, bittu wrote: > Imagine a robot sitting on the upper left hand corner of an NxN grid. > The robot can only move in two directions: right and down. How many > possible paths are there for the robot? > > FOLLOW UP >

[algogeeks] Re: In Place Merging of Two Sorted .Array...Not Easy as seems to be,....

2011-02-25 Thread Jammy
@ankit How do store the maximum? I know you mark the last element in the heap invalid. But for the case above, first 80 was extracted and then 60 should be extracted. But 60 would still lie in the first array. On Feb 25, 6:10 am, ankit sambyal wrote: > Hey, it can be done in o(n*logn) time by ca

Re: [algogeeks]

2011-02-24 Thread Jammy
Using Dynamic programming. Divide array into two parts a and b. Run the minimum edit distance solution for each pair of (a,b)(except this time for b we need to start from the end of the array). Find the minimum among those. On Feb 24, 10:41 am, sukhmeet singh wrote: > How do Levenshtein distanc

[algogeeks] Re: amazon

2011-02-23 Thread Jammy
Are you talking about IPC? On Feb 22, 10:05 am, jaladhi dave wrote: > What do you mean by data element here ? Also by file you mean the file where > you wrote the code ? And above all which programming language are we talking > ? > > You hit send button too early I  guess :) > > On 22-Feb-2011 7:

[algogeeks] Re: Directory Structure

2011-02-18 Thread Jammy
use a tree. For the first N lines, build a tree accordingly. For the next M lines search the tree. If miss, bump up the counter and add the node. On Feb 17, 7:53 am, Akshata Sharma wrote: >  On Unix computers, data is stored in directories. There is one root > directory, and this might have seve

[algogeeks] Re: Large File & Millions of Records

2011-02-16 Thread Jammy
Divide records into parts that could fit into main memory. Do rank find algorithm for certain range if necessary. On Feb 16, 7:26 pm, bittu wrote: > given a very large file (millions of record), find the 10 largest > numbers from it.in minimum time complexity > Don't think about hashing . Again I

[algogeeks] Re: String Operation

2011-02-16 Thread Jammy
Greedy, always choose the remaining one that is the lexicographically smallest since choose any other way will yield a lexicographically greater one. void concante(char **strings, int n){ qsort(strings,n,sizeof(char *),compareStr); } int compareStr(const void *a, const void *b){ return

[algogeeks] Re: question at K10

2011-02-16 Thread Jammy
Nice solution. Similarly you could #define: void change() { #define change() i=10 } On Feb 16, 12:55 pm, ankit sablok wrote: > nice solution > > On Feb 15, 11:22 pm, jagannath prasad das wrote: > > > > > > > > > void change() > > { > >    #define i i=10,n} > > > this will do.. > > > On Tue, F

[algogeeks] Re: Byte or Bite...Its byte Array

2011-02-16 Thread Jammy
since it is guaranteed that l points to the start of the character. Thus I would refer to 'X‘ as the byte l points to. Each number in the following represents the MSB of the corresponding byte. E.g. 101X --> the byte preceding X has MSB 1 and the byte preceding that has MSB 0 the byte preceding tha

[algogeeks] Re: Binary Tree Amazon

2011-02-15 Thread Jammy
hash would be a perfect choice. I make a MinMaxHash class which would keep track of the min and max value of the key.(since in this case we would never remove a key, thus this primitive approach works. Otherwise use treeMap) class MinMaxHash extends HashMap{ private Integer min = Integer.MAX_

[algogeeks] Re: interview quest..

2011-02-15 Thread Jammy
@Abhijit. Does your code takes O(N^2)? I think the following code would do it in O(N) iterate the string once: void remove(char *a){ if(!a){ int i = 0, j = 1; while(a[j]!='\0'){ if(i<0 || a[i]!=a[j]){

[algogeeks] Re: Finding elements near the median

2011-01-27 Thread Jammy
I agree time should be O(kn) On Jan 26, 9:55 pm, Sharath Channahalli wrote: > a) Find the median - O(n) > b) remove the element and again find the median > c) conitnue b until you get k-1 elements > > time complexity - kO(n) > > On Wed, Jan 26, 2011 at 9:55 PM, ritu wrote: > > > solution is nice

[algogeeks] Re: Doubt regarding Pointers in C......

2011-01-27 Thread Jammy
call it by reference. bst *rec = Null; insert(rec,5); void insert(bst *&rec,int data){rec->data = data;} Or declare it as void insert(bst **rec,int data){*rec->data = data;} and invoke with insert(&rec,5); First one is just syntactic sugar to me although you can argue about the whole pass-by-va

[algogeeks] Re: google written

2011-01-15 Thread Jammy
@Wei Please test you code on "cdbbcbbca". I believe it outputs 2 instead of 8. On Jan 14, 4:09 am, "Wei.QI" wrote: > FindStartIndex(char[] a) > { >     int start = 0; >     int current = 1; >     while(current < a.Length) >     { >         if(a[current] < a[start]) >         { >             start

[algogeeks] Re: Amazon Interview - Algorithms

2011-01-15 Thread Jammy
; > > At each location if the value is k  , > > > find the largest value in the next k elements and jump there. > > > > This greedy approach works in 0(n^2) and i believe it works. If not can > > > someone give me a counter example ? > > > > On Sat,

[algogeeks] Re: Amazon Interview - Algorithms

2011-01-14 Thread Jammy
@Avi Greedy approach doesn't work since you can't ensure the choice is locally optimum. Consider 3,9,2,1,8,3. Using greedy alg. would give you 3,1,8,3 while otherwise DP would give you 3,9,3. On Jan 14, 6:11 am, Avi Dullu wrote: > I guess u got confused with the comment I wrote, I have added 2 p

[algogeeks] Re: Building A Special Tree

2011-01-14 Thread Jammy
It's irrelevant but Building Special Tree has the same acronyms as Binary Search Tree...lame joke I know On Jan 14, 8:44 am, vaibhav agrawal wrote: > If it is a BST...then having a pre-order traversal can give us the unique > binary tree. > > Also, as per the problem statement, > > > every node c

[algogeeks] Re: probability

2011-01-14 Thread Jammy
Bayes' theorem: http://en.wikipedia.org/wiki/Bayes'_theorem P(x=even|x>3) = P(x>3|x=even)*P(x=even)/P(x>3)===>B On Jan 14, 2:29 am, snehal jain wrote: > An unbalanced dice (with 6 faces, numbered from 1 to 6) is thrown. The > probability that the face value is odd is 90% of the probability that >

[algogeeks] Re: palindrome

2011-01-13 Thread Jammy
if you meant starting the leftmost 1, to check if its palindrome: unsigned int o = v; unsigned int r; for (r = 0; v; v >>= 1) { r <<= 1; r |= v & 1; } if (o == r) printf("palindrome\n"); else

[algogeeks] Re: Sort string based upon the count of characters

2011-01-13 Thread Jammy
sort on two keys, primary key is frequency, secondary key is occurrence. using namespace std; struct Item{ int freq; int occur; char chr; Item(){ freq = 0; occur = -1; chr = -1;}; }; bool sortItem(const Item a, const Item b){ if(a.freq != 0 && b.freq != 0){

[algogeeks] Re: amazon questions

2011-01-13 Thread Jammy
@snehal 1. Both are valid. 2. see taocp's sol. The probability of selecting AB to shoot is 1/3, so is BC,AC If AB were selected, the probability of hitting the target is (1- probability of both of them missed) = (1-(1-P(A)(1-P(B)), similar with case BC and AC. On Jan 11, 11:58 am, snehal jain wro

[algogeeks] Re: Implement a queue in which push_rear(), pop_front() and get_min() are all constant time operations.

2011-01-13 Thread Jammy
implemented it based on juver++ & zhang. I think both stacks need to provide get_min functionality... class MyQueue{ private ItemStack pushstack; private ItemStack popstack; class Item{ int val; int min; public Item(int val,int min){

[algogeeks] Re: Adobe Interview Question

2011-01-13 Thread Jammy
brute force approach On Jan 12, 5:42 am, AKS wrote: > can someone just expain the plain simple logic used to solve this > problem ?? > Cdn't get it seeing the code > > On Jan 11, 10:08 pm, Jammy wrote: > > > > > > > > > There are apparently m

[algogeeks] Re: Amazon Again

2011-01-12 Thread Jammy
I think you meant n^3*log(K). Multiplication on two matrices would take O(n) per entry. And total #entries is n*n, thus multiplying 2 matrices takes O(n^3) To get a^(K), use simple divide-and-conquer technique. Since once one gets a^(K/2), to get a^(K) only needs to square it. Now we get T(K) = T(K

[algogeeks] Re: Adobe Interview Question

2011-01-11 Thread Jammy
ou, output will be : > (John)1 4 5 6 2 2 - (Mary)2 2 4 5 6 1 --- (john) 1 7 8 -- (Mary) 8 1 > 7 > minimum cuts = 3 > > > > On Tue, Jan 11, 2011 at 8:38 PM, Jammy wrote: > > @Arpit Please explain your solution to me. As far as I understand, > > every alte

[algogeeks] Re: Adobe Interview Question

2011-01-11 Thread Jammy
@Arpit Please explain your solution to me. As far as I understand, every alternate of two person should sum up equally. Which means every pair of (john, mary) has the same sum for john and mary. On Jan 11, 2:55 am, Arpit Sood wrote: > @jammy your code isnt working for the mentioned test c

[algogeeks] Re: Adobe Interview Question

2011-01-10 Thread Jammy
(a) it is intuitive to see we need to make a recursive function which takes the following arguments: 1) array, 2) start index, 3) length of the array, 4) a sentinel indicating if it is the first half or second half 5) a sum if it is the second half 6) number of cuts so far

[algogeeks] Re: floating point

2011-01-08 Thread Jammy
I guess it has to do with how float/double is stored on your computer. Always use an error tolerance when it comes to floating-point numbers comparison. By the way, on my machine it outputs the same thing("Hello") e.g. #define epsilon 10e-6 if(275.7-a>epsilon) printf("HI"); else printf("Hello"

[algogeeks] Re: Problem from ACM ICPC 2010

2011-01-08 Thread Jammy
I wondered that too...seems kid 3 gets too many wishes On Jan 8, 8:21 pm, bhawana goel wrote: > In the 3rd test case, how come the answer is Yes. 1,2 and 3 are > forming a cycle. > > On Jan 8, 1:19 pm, juver++ wrote: > > > > > > > > > Also, you may use hash_set and hash_map if such exists in you