[algogeeks] Re: google question
You are assuming is to be a binary tree, its not. Some nodes will share a common pour. On Feb 25, 9:24 pm, atul anand atul.87fri...@gmail.com wrote: i guess this would work... n=number of nodes h=height; pour=quantity poured; capacity = capacity of each cup n=pow(2,h+1) -1; call(capacity,pour,0,n) node* fillCup(float capacity,float pour,int left,int right) { node *root; int mid; if(left right) return NULL; root=(node *)malloc(sizeof(node)); if(left==right) { if(pour =capacity) root-data=capacity; else root-data=pour; root-left=root-right=NULL;} else { mid=left+(right-left)/2; if(pour = capacity) { root-data=capacity; pour=pour-capacity; pour=pour/2;} else { root-data=pour; root-left=root-right=NULL; return root; } root-left=fillCup(capacity,pour,left,mid-1); root-right=fillCup(capacity,pour,mid+1,right); } return root; } On Sat, Feb 25, 2012 at 5:05 PM, Ravi Ranjan ravi.cool2...@gmail.comwrote: |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| Each cup has capacity C and once a cup gets full, it drops half extra amount to left child and half extra amount to right child for Eg : let' first cups get 2C amount of liquid then extra amount C(2C-C) will be divided equally to left and right child cup of next level i.e. C/2 to left child and C/2 to right child Write a function which takes input parameter as amount of liquid poured at top (L) and height of particular cup (h) index of that cup (i) and it should return amount of liquid absorbed in that cup. source http://www.careercup.com/question?id=12770661 whats exactly the qestion??? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Google Question--Suggest Algo
Hi I may not have understood your solution properly. But i think that your solution is an implementation of brute force where you are dealing with all cases valid in the range n/2k and 3n/2k without any optimization with regard to DP. Am i right? On Nov 28, 2:17 am, sourabh sourabhd2...@gmail.com wrote: Consider the example that you have given: [0,0,1,1,0,0,1,1,0,1,1,0] , here n = 12 and k=3 Now we need to partition the array into 3 contiguous sub arrays such that : a) The expected sum value is maximum b) and the size of each sub array should be between 2 and 6, both inclusive. In case, this constraint is not satisfied then its not a valid candidate for the solution even if the partition produces the max value. 2 = ceil (n / 2k) = ceil (12/6) 6 = floor (3n / 2k) = floor (36/6) - As mentioned above the following equation : F(n,k) = MAX for all r such that ceil(n/2k) = r = floor(3n/2k) { (expected value of array elems from A[n] to A[n-r+1]) + F(n-r, k-1) } /** For understanding how partitioning of the array is represented by the above equation: Say there is an array denoted by A[i] and it needs to be divided into 3 contiguous parts, one of the ways to do so would be to take the following steps : Let K(partition no.) be initialized to 3. Let array size N be 12. a) If N is 0, the goto step 'f' b) If K == 1 then call it as partition K and goto step 'e'. c) Lets take X no. of elements from the end of array A of size N and call it partition K. d) Decrement K by 1 and N by X { --K; and N-=X;} d) Goto step 'a' e) Valid partition and End. f) Not a valid partition and End. Now if the above set of steps is run for all values of X such that 2=X=6 , then it will generate all possible candidates (partitions) as per the given problem statement. And for all the valid partitions(the ones that will hit step 'e') we need to calculate the expected sum value. **/ can be translated into, // I am using 1-based array indexing for better clarity // A[x .. y] means all elements from A[y] to A[x].. F(12, 3) = MAX { ExpVal (A[12 .. 11]) + F(10, 2) , ExpVal (A[12 .. 10]) + F(9, 2) , ExpVal (A[12 .. 9]) + F(8, 2) , // this will yield the maximum sum.. ExpVal (A[12 .. 8]) + F(7, 2) , ExpVal (A[12 .. 7]) + F(6, 2) } which is nothing but, F(12, 3) = MAX { 1/2 + F(10, 2) , 2/3 + F(9, 2) , 2/4 + F(8, 2) , // this will yield the maximum sum.. 3/5 + F(7, 2) , 4/6 + F(6, 2) } Trace the above equation and you should get it.. On Nov 28, 12:57 am, Ankur Garg ankurga...@gmail.com wrote: Hey Sourabh Could you please explain the solution in a bit detail perhaps using an example or so..It wud be really helpful ..Just logic not code On Mon, Nov 28, 2011 at 1:03 AM, sourabh sourabhd2...@gmail.com wrote: Looks like a dynamic programming problem Say F(n,k) denotes the maximum expected sum value for an array of n elements and partition k , then F(n,k) = MAX for all r such that ceil(n/2k) = r = floor(3n/2k) { (expected value of array elems from A[n] to A[n-r+1]) + F(n-r, k-1) } Base condition: 1) F(N, 1) = expected value for array A[n] such that ceil(n/2k) = N = floor(3n/2k) 2) If any of the sub problems where the array size is not between ceil(n/2k) and floor(3n/2k) , both inclusive, then its not a valid candidate for the final solution. This is can be handled by giving initial value to all such combination a value of -1. To store that the intermediate computations take an array Max[N][K], F(N,K) = Max[N][K] On Nov 28, 12:17 am, sourabh sourabhd2...@gmail.com wrote: Because in the previous example k = 3. On Nov 27, 10:46 pm, Piyush Grover piyush4u.iit...@gmail.com wrote: Optimal split: [0,0][1,1][0,0][1,1][0,1][1,0] Expected value of optimal split: 0 + 1 + 0 + 1 + 1/2 + 1/2 = 3 why this is not the optimal split??? On Sun, Nov 27, 2011 at 6:58 PM, Ankur Garg ankurga...@gmail.com wrote: You have an array with *n* elements. The elements are either 0 or 1. You want to *split the array into kcontiguous subarrays*. The size of each subarray can vary between ceil(n/2k) and floor(3n/2k). You can assume that k n. After you split the array into k subarrays. One element of each subarray will be randomly selected. Devise an algorithm for maximizing the sum of the randomly selected elements from the k subarrays. Basically means that we will want to split the array in such way such that the
[algogeeks] Re: Facebook Campus Test Question
plz post a samle input output.. On Nov 4, 10:24 am, sunny agrawal sunny816.i...@gmail.com wrote: What can be a better solution than a Brute Force O(N^2* No of iteration) -- Sunny Aggrawal B.Tech. V year,CSI Indian Institute Of Technology,Roorkee fb1_input.txt 1KViewDownload facebook.jpg 119KViewDownload fb1_output.txt 1KViewDownload -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Facebook Campus Test Question
is ur brute force O(1) for space? On Nov 4, 10:24 am, sunny agrawal sunny816.i...@gmail.com wrote: What can be a better solution than a Brute Force O(N^2* No of iteration) -- Sunny Aggrawal B.Tech. V year,CSI Indian Institute Of Technology,Roorkee fb1_input.txt 1KViewDownload facebook.jpg 119KViewDownload fb1_output.txt 1KViewDownload -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Intersection of arrays
I think Dan's solution is the best one here. TC O(n log n) and SC O(1) where n is the maximum no. of elements in any array Instead of starting with K given arrays, just take the first 2. Sort both of them - time is nlogn Now run two pointers on each array to save the common elements as they are and clear the rest in 1st array. Discard 2nd array now. We have 1st array with intersection elements only. Now continue the same thing - With 1st array and 3rd array. Sort 3rd array. Keep common elements in 1st array and clear the rest. Keeping common elements in 2 arrays and clearing the other elements can be done in O(n) TC. On Oct 25, 11:17 am, kumar raja rajkumar.cs...@gmail.com wrote: Find intersection of K unsorted array of N elements each. Intersection consists of elements that appear in all the K arrays. what data structure is useful here?? -- Regards Kumar Raja M.Tech(SIT) IIT Kharagpur, 10it60...@iitkgp.ac.in -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon OS question
How are we going to write a program which if fed the data gives the answer? Any ideas for the algorithm? My approach: We have a graph here. We have vertices with indegree 0 and outdegree 1. - call it set A (start points) (m vertices) We have vertices with indegree 1 and outdegree 0. - call it set B (end points) (n vertices) The paths from set A to set B can be run on multiple processors (m x n paths possible). if no. of processors = m x n then no. of steps will be maximum no. of elements in any path. In the above ques, m = 1 and n = 3. if no. of processors m x n, then run a BFS on the graph and find the number of vertices at each level. In the above question, the vertices at levels are 1, 1, 3 , 1. ((Max of these i.e. 3) / number of processors) + maximum no. of elements in any path = would be the answer. Now this part would have a problem if m!=1; Another problem would be how to find the maximum no. of elements in any path possible. On Sep 25, 9:03 am, sivaviknesh s sivavikne...@gmail.com wrote: A parallel program consists of 8 tasks – T1 through T8. Each task requires one time step to be executed on a single processor. Let X - Y denote the fact that task X must be executed before task Y is executed. Suppose only the tasks X, Y are to be executed. On any multiprocessor machine it would require at least 2 time steps since in the first step X could be executed, and Y could be executed in the next time step (since it requires X to complete first). Now, suppose the following dependencies exist between the tasks T1 – T8: T1 - T2 T2 - T3 T3 - T6 T2 - T4 T4 - T7 T2 - T5 T5 - T8 What is the minimum number of time steps required to execute these 8 tasks on a 2 processor machine and a 4 processor machine? a)4 2 b)5 2 c)5 4 d)6 2 -- Regards, $iva -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: google os ques on pipelining
@bharat: for the second part where u multiplied (160+5) with 999, it should be 160*999 because it is max of (150,120,160,140,5). Correct me if i am wrong. On Sep 26, 4:02 pm, bharatkumar bagana bagana.bharatku...@gmail.com wrote: for the first instruction : 150+5+120+5+160+5+140=585 ns for the rest of the instructions , though pipeline max(150,120,160,140)=160 (160+5)*999=164835 ns we assume that there will be no extra stalls existed in our system -585 + 164835 =165420 ns =165.4 us... correct me if I'm wrong . On Sun, Sep 25, 2011 at 9:25 AM, siva viknesh sivavikne...@gmail.comwrote: A 4-stage pipeline has the stage delays as 150, 120, 160 and 140 ns (nano seconds) respectively. Registers that are used between the stages have a delay of 5 ns each. Assuming constant clocking rate, the total time taken to process 1000 data items on this pipeline will approximately be a. 120 us (micro seconds) b. 165 us c. 180 us d. 175 us ...plz give detailed explanation for the ans -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- **Regards *BharatKumar Bagana* **http://www.google.com/profiles/bagana.bharatkumarhttp://www.google.com/profiles/bagana.bharatkumar * Mobile +91 8056127652* bagana.bharatku...@gmail.com -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: convert a word into a palindrome with minimum addition of letters to it
@Victor: Instead of 0 and 1, shouldn't it be like DP[i-1][j-1] + 0 and DP[i-1] [j-1] + 1 On Sep 7, 1:10 am, Victor Manuel Grijalva Altamirano kavic1.mar...@gmail.com wrote: Try with DP, a little modicated of Edit Distance algorithm State i=the begin of the word , j=the end of the word DP[i][j]= 0 if i==j 0 if(i+1)==j word[i]==word[j] 1 if(i+1)==j word[i]!=word[j] min(DP[i+1][j]+1,DP[i][j-1]+1) otherwise If you have any question ask!!! Good luck!!! Victor Manuel Grijalva Altamirano Universidad Tecnologica de La Mixteca -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: convert a string into another in minimum number of steps
how to check if intermediate words are dictionary words or not?? Also what would be the approach for that? On Aug 31, 3:10 am, icy` vipe...@gmail.com wrote: Yea, I hope the intermediate words are dictionary words; it's more fun that way. I played such a game before, where you and a friend try to convert some 4-letter word into another, using legal words. BIKE - LAZY BIKE BAKE RAKE RAZE LAZE LAZY On Aug 30, 2:25 am, kARTHIK R k4rth...@gmail.com wrote: So the intermediate words need not be dictionary words? Karthik R, RD Engineer, Tejas Networks. On Tue, Aug 30, 2011 at 11:50 AM, PRATEEK VERMA prateek...@gmail.comwrote: edit distance algorithm -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: How to save a binary search tree space efficiently
Level Order traversal if you are ok with the Nulls being stored. Otherwise its pre order traversal. On Aug 30, 12:34 pm, Vidit Sinha vidit.it...@gmail.com wrote: whats the final answer guys? Level order traversal?? On Tue, Aug 30, 2011 at 12:56 AM, Don dondod...@gmail.com wrote: I'm not sure if this is what you are looking for, but I once came up with a way to save a binary tree in such a way that when it is rebuilt, it will be balanced. You don't get back the exact same tree with all the nodes in the same position, but you do get the same nodes in a balanced configuration. Start by doing an inorder traversal and storing each node sequentially in an array. Then call a recursive function called saveTree(first, last), where first and last are the first and last indices of the array. saveTree does the following if: write middle item of array to the file call saveTree on left half of array call saveTree on right half of array When you rebuild the tree adding the nodes in the order in which they occur in the file, the resulting tree will be balanced. Don On Aug 28, 1:29 am, rohit rajuljain...@gmail.com wrote: How to save a binary search tree space efficiently and built it again , just tell any idea. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: stack using bst
So, in short you are using a BST and a FIFO linked list. Whereas, a Stack is actually a LIFO linked list. Am i right? On Aug 25, 11:37 pm, Don dondod...@gmail.com wrote: You will have to keep two pointers, one to the root of the tree and one to the head of the FIFO linked list. To push, insert the item into both the bst and the linked list. To pop, set a pointer to the first item in the linked list. Delete it from the bst. It will always be a leaf, so deleting should be easy. Follow the parent pointer up one level and set the appropriate left or right pointer to null. Then set the head of the linked list to the next item in the list. Return the pointer to the former first item. push(node n) { tree.insert(n); // O(log n) n-next = stack; stack = n; } node pop() { node result = stack; if (result-parent-left == result) result-parent-left = 0; else result-parent-right = 0; stack = result-next; result-next = 0; return result; } I was mistaken when I said push was O(1). Later on I said O(log n) which is correct. Don On Aug 25, 1:25 pm, shady sinv...@gmail.com wrote: @don how will you keep track of the latest element inserted in a bst ? O(1) for pop ? similarly how to get O(1) for push ? Stack can be implemented with bst but the time complexity will increase. Anyone with different views ? On Thu, Aug 25, 2011 at 11:37 PM, Don dondod...@gmail.com wrote: The only way I see to be able to search in O(log n) and push/pop in O(1) would be to have each node contain 4 pointers: left, right, and parent pointers for the binary search tree and next pointer for the stack linked list. The stack would be a linked list using the next pointer, and the search tree would allow quick searching. Insertion and searching would be O(log n) as with any binary search tree. Pop would be O(1). If you don't need to be able to search, just use the left pointer to make a linked list. Push and pop are both O(1), but you don't have a binary search tree any more. Don On Aug 25, 12:00 pm, prasanth n nprasnt...@gmail.com wrote: how to implement a stack(push and pop) using binary search tree?? -- *prasanth* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: print all paths which sum up to a value.
Any help regarding the preprocessing required for O(1) operation for LCA??? I read the wiki's article involving eulers traversal.. but its nt clear. On Aug 24, 1:12 am, DK divyekap...@gmail.com wrote: Hmm.. Interesting question. *Here's a solution:* There are n elements in the tree and therefore n^2 paths in the tree (duplicate endpoints allowed and 2 endpoints correspond to exactly 1 simple path in the tree). 1. For each node in the BST, store the sum of node values on the path to the root. Can be done using a simple DFS in O(N). Also perform preprocessing for the LCA function (required below). 2. For each pair of nodes (u, v) in the BST: - O(N^2) Cost of path = Cost(u, root) + Cost(v, root) - Cost(LCA(u,v), root) If(Cost of path == value) print_path(u,v) The LCA is the lowest common ancestor of the given nodes (See:http://en.wikipedia.org/wiki/Lowest_common_ancestor) LCA(u,v) is an O(1) operation given O(N) preprocessing of the tree. Therefore, we know for sure that we can do this problem in worst case O(N^2). *Optimizations: *I'm not sure any of these yield worst case bound improvements but here are a few things that you can do: 1. Get rid of symmetry: (u, v) is the same as (v, u). Choose u v always. 2. For a chosen u, skip all children of v if target cost v Time Complexity: O(N^2) Space Complexity: O(N) -- DK http://twitter.com/divyekapoorhttp://www.divye.inhttp://gplus.to/divyekapoor -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Longest palindrome
So, suggest some approach plz for this ques. On Aug 22, 12:59 am, Dave dave_and_da...@juno.com wrote: @Sanjay: Will method 1 really work? What would it return on the string abraxyzarba? The longest common substring between this string and its reverse is abra, but how does that relate to the fact that the longest palindrome is any single letter of the string? What am I missing? Dave On Aug 21, 1:46 pm, anurag saxena anurag.saxen...@gmail.com wrote: method 1) : we can reverse the string and find the longest common substring occurring in reversed string and original string. method 2) : making a generalized suffix tree of string and reversed string and each node should depict whether the suffix belongs to string or reversed string .. then the deepest node having both the marker will be longest palindrome. On 8/21/11, Sanjay Rajpal srn...@gmail.com wrote: i hvn't read about suffix trees. will u plz post a useful link ? Sanju :) On Sun, Aug 21, 2011 at 11:21 AM, MAC macatad...@gmail.com wrote: suffix tree will solve it . On Sun, Aug 21, 2011 at 11:46 PM, priya ramesh love.for.programm...@gmail.com wrote: how abt goimg with brute force?? check starting from first character if first 2 chars frm a palin, then chck if first 3 form a palin... continue until the end of string. Now starting from 2nd char, do the same. keep a var max to store the max value. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- thanks --mac -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.-Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: another q on ds!
first option is correct. Because we are not allowed to use any extra space. On Aug 21, 6:36 pm, priya ramesh love.for.programm...@gmail.com wrote: A Most efficient data structure is designed to optimize the following operations. Pop, Push, Min. The best possible time-complexities with no extra space, respectively would be: · Pick one of the choices O(1), O(1), O(N) O(1), O(N), O(1) O(N), O(1), O(1) O(1), O(1), O(1) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: amazon q
We can't use a heap. Balanced BST is correct because Deletion of the smallest element Insertion of an element if it is not already present in the set - for this we need to search for the element and searching in heap is O(n). On Aug 21, 6:16 pm, priya ramesh love.for.programm...@gmail.com wrote: A data structure is required for storing a set of integers such that each of the following operations can be done in (log n) time, where n is the number of elements in the set. Deletion of the smallest element Insertion of an element if it is not already present in the set Which of the following data structures can be used for this purpose? · Pick one of the choices A heap can be used but not a balanced binary search tree A balanced binary search tree can be used but not a heap Both balanced binary search tree and heap can be used Neither balanced binary search tree nor heap can be used -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Algorithm complexity
Check interpolation which is a substitute for binary search for a uniformly sorted array. It has complexity of this order. On Aug 3, 7:31 pm, Ajai Sathyan ajaisath...@gmail.com wrote: Can you suggest a sample program which has complexity O(log log n)? Regards Ajai Sathyan -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Subsequence with Sum S
Given an array of length n of integer numbers. Output 1 or 0 depending on whether or not there exists a sub sequence in the array with sum S. Suggest a fastest algorithm plz. e.g. say given an array with numbers as 10 20 30 40 50 60 70 Sum = 110 Output is 1 because 20+30+50 is 110. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: interview ques
Use multilevel indexing On Jul 27, 11:07 pm, himanshu kansal himanshukansal...@gmail.com wrote: if u hv say 20 million records and u have to create a b+ tree then you might be storing 20 million pointers at the leaf levelhow can u optimize this(using b+ tree only)??? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MS
check the above solution.. urs is O(logn) mine is O(log k). On Jul 19, 11:35 pm, sagar pareek sagarpar...@gmail.com wrote: well i dont think so...any better solution will be there compare mine one On Wed, Jul 20, 2011 at 12:00 AM, Dumanshu duman...@gmail.com wrote: heres the code http://ideone.com/rX130 On Jul 19, 9:35 pm, swetha rahul swetharahu...@gmail.com wrote: Hi, Find the kth smallest element in O(logk) given 2 sorted arrays. Merging the arrays is not allowed. I can do it in O(k).. How to do in O(logk).. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- **Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Whats the complexity?
Given an infinite length list. u got to find index of an element k. use this approach- initially, take length as 2^x where x increases from 1 to ... while (still not found) { now if arr[2^x-1] k, increment x else binarysearch on length 2^(x-1) to 2^(x) } Please help me to find the complexity of this particular approach... -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Axis Aligned Rectangles
Describe an algorithm that takes an unsorted array of axis‐ aligned rectangles and returns any pair of rectangles that overlaps, if there is such a pair. Axis‐aligned means that all the rectangle sides are either parallel or perpendicular to the x and y‐axis. You can assume that each rectangle object has two variables in it : the x‐y coordinates of the upper‐left corner and the bottom‐right corner. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Whats the complexity?
that is why m confused. how would u rate this algo? in what order? On Jul 20, 10:44 pm, Ankur Khurana ankur.kkhur...@gmail.com wrote: when n is not defined , you want complexity in terms of ? On Wed, Jul 20, 2011 at 3:16 PM, Dumanshu duman...@gmail.com wrote: Given an infinite length list. u got to find index of an element k. use this approach- initially, take length as 2^x where x increases from 1 to ... while (still not found) { now if arr[2^x-1] k, increment x else binarysearch on length 2^(x-1) to 2^(x) } Please help me to find the complexity of this particular approach... -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Ankur Khurana Computer Science Netaji Subhas Institute Of Technology Delhi.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Whats the complexity?
it should involve the exponential increment! somebody plz help On Jul 20, 10:56 pm, Ankur Khurana ankur.kkhur...@gmail.com wrote: in my opinion , it is log(indexof(k)) On Wed, Jul 20, 2011 at 11:23 PM, Dumanshu duman...@gmail.com wrote: that is why m confused. how would u rate this algo? in what order? On Jul 20, 10:44 pm, Ankur Khurana ankur.kkhur...@gmail.com wrote: when n is not defined , you want complexity in terms of ? On Wed, Jul 20, 2011 at 3:16 PM, Dumanshu duman...@gmail.com wrote: Given an infinite length list. u got to find index of an element k. use this approach- initially, take length as 2^x where x increases from 1 to ... while (still not found) { now if arr[2^x-1] k, increment x else binarysearch on length 2^(x-1) to 2^(x) } Please help me to find the complexity of this particular approach... -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Ankur Khurana Computer Science Netaji Subhas Institute Of Technology Delhi.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Ankur Khurana Computer Science Netaji Subhas Institute Of Technology Delhi.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Ever growing sorted linked list
Some please give the TC for exponential + logarithmic search method pointed out by Swathi. On Jul 19, 11:51 am, Ankur Khurana ankur.kkhur...@gmail.com wrote: @sagar: you might have proposed a solution but it ever occured to you that (ptr-next)-next means that you have gone through two diferent nodes. For saving one comparison , we are doing lot of un necessary computations which , IMHO is not good or feasible . Linked list are not made for searching purpose, for that arrays were made. On Tue, Jul 19, 2011 at 12:13 PM, sagar pareek sagarpar...@gmail.comwrote: hey check my algo...i mentioned all the possible cases :) On Tue, Jul 19, 2011 at 11:31 AM, oppilas . jatka.oppimi...@gmail.comwrote: Yes we can avoid integer comparison. But for jumping we will need to check whether the next node is null or not. So, depending upon the jump size, the number of comparison in worst case can be very large if our jump size is increasing exponentially. So, why not just compare with the next node instead of jumping. On Tue, Jul 19, 2011 at 10:47 AM, sunny agrawal sunny816.i...@gmail.comwrote: yes Alok u r right that in any case we will traverse k times if k is the position if the element that need to searched but by jumping we can save the time of avoiding unnecessary comparisions On Tue, Jul 19, 2011 at 10:44 AM, Alok Prakash alok251...@gmail.comwrote: If i am not wrong, in a linked list to move to any number of position, say n, we have to traverse all intermediate nodes of the linked list. So, it does not matter if we are moving pointer by 2,4,8,... positions, we have to scan all intermediate nodes. Is it not a simple searching On Tue, Jul 19, 2011 at 9:17 AM, sumit sumitispar...@gmail.com wrote: Here is the idea I was thinking of: - start from the root: if(root-data k) move to position 2 in the list. - in this way every time move the pointer to the position double the current position and compare th eelement of node with k( moving here is form 1 - 2 , 2-4, 4-8 ,8-16 ..) - suppose you found a certain node at position n whose node-data k , then now u only have to search for k between index n/2 to n (i.e. if you found 16th element k then search for k between 8th and 16ht element).. correct me if any flaws. On Jul 19, 3:38 am, Dumanshu duman...@gmail.com wrote: @Gaurav: Ever Increasing means that you don't know the length of the list. So you have to assume some index n, traverse the list upto that point and check the results. If not found increment the n to some higher value. We are basically trying to improve the complexity by taking higher and higher jumps for n. On Jul 19, 2:03 am, Gaurav Popli abeygau...@gmail.com wrote: yeah...im saying to reach position n at which k is placed we have to trverse n positions from head or not...?? and i didnt understand the use of ever increasing...please elaborate on it.. On Tue, Jul 19, 2011 at 2:08 AM, Dumanshu duman...@gmail.com wrote: @Swathi: plz give the TC of ur algo (exponential plus log) On Jul 18, 10:14 pm, Swathi chukka.swa...@gmail.com wrote: The solution to this problem will be a combination of exponential increase and the binary search.. start = 0; end = 0; index =0; middle = 0; while (1) { if(a[start] == data) return start; if(a[end] == data) return end; if(data end) { start = end; end = pow(start,2); // here we are consider exponential for faster search. // no need to check any boundary conditions as the array is infinite continue; } else { // do binary search between start index and end index because data is inbetween a[start] and a[end] } } Hope this helps... On Mon, Jul 18, 2011 at 10:32 PM, Gaurav Popli gpgaurav.n...@gmail.comwrote: i dont understand it..if k is at position an let supposeso to check at that position dont you have to traverse till that position ...is thr anything else than the head of list...??or i understood wrongly...?? On Mon, Jul 18, 2011 at 9:55 PM, sagar pareek sagarpar...@gmail.com wrote: Update on 2nd line 2. if( ptr2=ptr1-next-next...(5 or 10 times) ) goto 3. else make linear search till NULL encounter and exit with the solution On Mon, Jul 18, 2011 at 7:41 PM, sagar pareek sagarpar...@gmail.com wrote: i have one approach :- first compare root-data and k if k is very much greater than root-data then set next=5or10 ur choice else set next 2or3 ur choice take two pointers ptr1 and ptr2 now lets take k is much greater than root-data then 1. set ptr1=root //initially 2. if( ptr2=ptr1-next-next...(5
[algogeeks] Re: Amazon
What is the answer to the first one? On Jul 19, 10:07 am, sagar pareek sagarpar...@gmail.com wrote: 1. Let's say S=D=N repeat D=(floor)D/2; S=S-D; till D=0 From the above logic, figure out which algorithm is followed by 'S' 2. How can you divide a triangle, to 5 equal triangles? 3. There are players for a chess tournament, having every game as a knock out game (a player will be out of the tournament if he/she loses a game), how many games need to be conducted to determine a winner of the tournament. -- **Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
@Surender: sorry, i had missed a case. We need a 3 way comparison. heres the correct version btree* max_tree(btree *root) { if(root == NULL) return root; btree * current = root; while(current-right != NULL) { current = current-right; } return current; } btree * min_tree(btree *root) { if(root == NULL) return root; btree * current = root; while(current-left != NULL) { current = current-left; } return current; } void binarytreetobst(btree *root) { //base-case tree is empty if(root == NULL) return; else if(root-left == NULL root-right == NULL) //base-case tree of size 1 return; else { binarytreetobst(root-left); binarytreetobst(root-right); btree* max = max_tree(root-left); if(max max-data root-data) { int temp = root-data; root-data = max-data; max-data = temp; binarytreetobst(root-left); } btree* min = min_tree(root-right); if(min min-data root-data) { int temp = root-data; root-data = min-data; min-data = temp; binarytreetobst(root-right); max = max_tree(root-left); if(max max-data root-data) { int temp = root-data; root-data = max-data; max-data = temp; binarytreetobst(root-left); } } } } On Jul 19, 8:38 am, surender sanke surend...@gmail.com wrote: @Damanshu for 1 / \ 2 3 / \ 4 5 / \ 6 7 im ending up at some non BST surender On Tue, Jul 19, 2011 at 4:06 AM, Dumanshu duman...@gmail.com wrote: @Gaurav: The best solution would be to manipulate the given BTree in place and get the BST. We don't need a separate tree but convert the existing one using the same space occupied by nodes of BT already in BST. On Jul 19, 2:06 am, Gaurav Popli gpgaurav.n...@gmail.com wrote: cant we just dotraverse using recursion and instead of printing it just pass to function which is making BST?? and is this right as someone above said first sort it and then make BST... dont we want that root of both Tree to be same or something like that...?? On Tue, Jul 19, 2011 at 2:17 AM, Dumanshu duman...@gmail.com wrote: @Balaji: for third question, were u asked to write the code? On Jul 18, 10:04 pm, Balaji S balaji.ceg...@gmail.com wrote: wats the mistake.. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group athttp:// groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MS
heres the code http://ideone.com/rX130 On Jul 19, 9:35 pm, swetha rahul swetharahu...@gmail.com wrote: Hi, Find the kth smallest element in O(logk) given 2 sorted arrays. Merging the arrays is not allowed. I can do it in O(k).. How to do in O(logk).. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Tournament Tree
Given billions of unsorted numbers and we need to find first 1 million numbers if all the numbers were sorted in non-descending order. approach is to split up the numbers to multiple machines and sort them. Then make a tournament tree in such a way that each machine feeds the lowest number it has, to the tree. After all the machines have fed their lowest number, the tree outputs the minimum number which goes to the output. This number say came from i th machine. Now the next number to the tree is fed by this ith machine only. Repeat the above process for getting the 1st million numbers. Now someone plz explain how to code this? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Find the missing number - Again given 4 billion integers
Hi sry for not being so clear with the problem statement. The list of numbers may have repetitions. Lots of numbers can be missing from the list, you just need to output any one. Regards Dumanshu BITS-Pilani On Jul 18, 5:28 pm, ankit sambyal ankitsamb...@gmail.com wrote: The question says : find the integer that is not there in the list. So it seems that there is only one missing integer. Also the list contains 2^32 integers and if we take all possible integers, assuming an integer takes 4 bytes, we get 2^32 integers. So, if 1 integer is missing in the list, it means that at least 1 no. is repeating in the list. So, the xor method fails in this case. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Missing Number in an array
Quadratic equation solver: just solve it the way we do on paper, take coefficients as input and apply formula x = (-b+sqrt(b^2 - 4*a*c))/ 2*a... anything wrong with this? And for the given problem, it says atleast once and not exactly once. So, Ankit's solution of bit vector is the best one. On Jul 18, 5:48 pm, ankit sambyal ankitsamb...@gmail.com wrote: @Ashish : Cud u plz highlight how to write code to solve a quadratic equation ? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Long string and the first non-repeating character
You are given a long string and you have to print the first non repeating character. Solve it keeping SC and TC in mind. That is present both solutions, one with high SC and low TC and viceversa. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MD5
here it is -http://rosettacode.org/wiki/MD5 It has the C implementation if u need in C. On Jul 18, 6:09 pm, Anuj kumar anonymize...@gmail.com wrote: some body can give me code of MD5 PLZ its Urgent .. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Ever growing sorted linked list
You have a sorted linked list of integers of some length you don't know and it keeps on increasing. You are given a number k. Print the position of the number k. Basically, you have to search for number k in the ever growing sorted list and print its position. Please write the complexity of whatever solution you propose. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
Lets say DLL is sorted. We have to convert the DLL so basically its the manipulation of pointers. Can we do it recursively? //n is the total number of nodes in the list. // next corresponds to right and prev corresponds to left pointer. Node * BuildBalBST(Node *head, int n) { if(!n) return NULL; //no node if(n==1) // single node { head-next = NULL; head-prev = NULL; return head; } Node *temp1,*temp2,*temp3 = head; int i; for(i=0;in/2;i++) temp3= temp3-next; temp1 = BuildBalBST(head,n/2); temp2 = BuildBalBST(temp3-next,n/2 - (n+1)%2); temp3-next = temp2; temp3-prev = temp1; return temp3; } In case, DLL is not sorted, sort it then using any algo and call above. Did i miss any case?? On Jul 18, 5:57 pm, radha krishnan radhakrishnance...@gmail.com wrote: Really MS asking garbage collection questions ? :P 2) if the DLL is not sorted then it takes O(n lgn ) to build the BST but the code looks so bloated if we write the code On Mon, Jul 18, 2011 at 4:59 AM, Balaji S balaji.ceg...@gmail.com wrote: Convert a binary tree to binary search tree inplace.. Convert a DLL to a binary search tree that is balanced. Implement a C++ garbage collector efficiently -- With Regards, Balaji.S -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
1. //BT to BST - function used is to swap values Node* bubbleData(Node *root) { if(!root) return NULL; if(root-right) { if(root-data root-right-data) swap((root-data),(root-right-data)); root-right = bubbleData(root-right); } if(root-left) { if(root-data root-left-data) swap((root-data),(root-left-data)); root-left = bubbleData(root-left); } return bubbleData(root); } any case missing?? 3. Do we have to give an algorithm for garbage collection like Mark sweep algo or Do we have to write a code? if code, plz tell how to write? On Jul 18, 4:59 pm, Balaji S balaji.ceg...@gmail.com wrote: 1. Convert a binary tree to binary search tree inplace.. 2. Convert a DLL to a binary search tree that is balanced. 3. Implement a C++ garbage collector efficiently -- With Regards, Balaji.S -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Long string and the first non-repeating character
heres my solution with TC O(n) and SC O(26) input string str int arr[26] = {0}; traverse the string, character by character and increment the corresponding counter. i.e. arr[str[i]]++; Now traverse the string again and print out the first character encountered whose arr[str[i]] == 1; On Jul 18, 9:20 pm, sagar pareek sagarpar...@gmail.com wrote: Very good solution :- but space complexity = O(26) take integer array arr[0-25] and initialise it with 0 by taking it static logic is that we have only 26 characters so if i want to map character 'a' with 0th position of arr[] then it can be done as atoi('a')-97. so whenever we encounter any character say str[i] (where str is array of given string) then it can be incremented as arr[atoi(str[i])-97]++ so traverse the whole str[] and increment the corresponding values . At the end those characters which never encounter have values 0 in arr , which encounter only once have values 1 and more than once have values1. at the end traverse the whole arr[] and find out the corresponding character as itoa(arr[i]+97) :) :) But we have to do extra work to find the first character which repeats only once On Mon, Jul 18, 2011 at 8:09 PM, hary rathor harry.rat...@gmail.com wrote: can we use bit vector ? because by do it we need just 32 bits of one extra variable . -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- **Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
Sry, I made a mistake in my code for problem 1 to convert BT to BST Ignore it ... its wrong any case missing?? 3. Do we have to give an algorithm for garbage collection like Mark sweep algo or Do we have to write a code? if code, plz tell how to write? On Jul 18, 4:59 pm, Balaji S balaji.ceg...@gmail.com wrote: 1. Convert a binary tree to binary search tree inplace.. 2. Convert a DLL to a binary search tree that is balanced. 3. Implement a C++ garbage collector efficiently -- With Regards, Balaji.S- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
got the code for BT to BST from a site.. btree* max_tree(btree *root) { if(root == NULL) return root; btree * current = root; while(current-right != NULL) { current = current-right; } return current; } btree * min_tree(btree *root) { if(root == NULL) return root; btree * current = root; while(current-left != NULL) { current = current-left; } return current; } void binarytreetobst(btree *root) { //base-case tree is empty if(root == NULL) return; else if(root-left == NULL root-right == NULL) //base-case tree of size 1 return; else { binarytreetobst(root-left); binarytreetobst(root-right); btree* max = max_tree(root-left); if(max max-data root-data) { int temp = root-data; root-data = max-data; max-data = temp; binarytreetobst(root-left); } btree* min = min_tree(root-right); if(min min-data root-data) { int temp = root-data; root-data = min-data; min-data = temp; binarytreetobst(root-right); } } } On Jul 18, 7:24 pm, Dumanshu duman...@gmail.com wrote: 1. //BT to BST - function used is to swap values Node* bubbleData(Node *root) { if(!root) return NULL; if(root-right) { if(root-data root-right-data) swap((root-data),(root-right-data)); root-right = bubbleData(root-right);} if(root-left) { if(root-data root-left-data) swap((root-data),(root-left-data)); root-left = bubbleData(root-left);} return bubbleData(root); } any case missing?? 3. Do we have to give an algorithm for garbage collection like Mark sweep algo or Do we have to write a code? if code, plz tell how to write? On Jul 18, 4:59 pm, Balaji S balaji.ceg...@gmail.com wrote: 1. Convert a binary tree to binary search tree inplace.. 2. Convert a DLL to a binary search tree that is balanced. 3. Implement a C++ garbage collector efficiently -- With Regards, Balaji.S- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Ever growing sorted linked list
@Swathi: plz give the TC of ur algo (exponential plus log) On Jul 18, 10:14 pm, Swathi chukka.swa...@gmail.com wrote: The solution to this problem will be a combination of exponential increase and the binary search.. start = 0; end = 0; index =0; middle = 0; while (1) { if(a[start] == data) return start; if(a[end] == data) return end; if(data end) { start = end; end = pow(start,2); // here we are consider exponential for faster search. // no need to check any boundary conditions as the array is infinite continue; } else { // do binary search between start index and end index because data is inbetween a[start] and a[end] } } Hope this helps... On Mon, Jul 18, 2011 at 10:32 PM, Gaurav Popli gpgaurav.n...@gmail.comwrote: i dont understand it..if k is at position an let supposeso to check at that position dont you have to traverse till that position ...is thr anything else than the head of list...??or i understood wrongly...?? On Mon, Jul 18, 2011 at 9:55 PM, sagar pareek sagarpar...@gmail.com wrote: Update on 2nd line 2. if( ptr2=ptr1-next-next...(5 or 10 times) ) goto 3. else make linear search till NULL encounter and exit with the solution On Mon, Jul 18, 2011 at 7:41 PM, sagar pareek sagarpar...@gmail.com wrote: i have one approach :- first compare root-data and k if k is very much greater than root-data then set next=5or10 ur choice else set next 2or3 ur choice take two pointers ptr1 and ptr2 now lets take k is much greater than root-data then 1. set ptr1=root //initially 2. if( ptr2=ptr1-next-next...(5 or 10 times) ) else make linear search till NULL encounter 3. if ptr2-data==k return its position 4. else if (ptr2-datak) set ptr1=ptr2 goto 2 5. else traverse the ptr1 upto ptr2, if found return its position else return fail if anyone has more efficient solution then pls tell :) On Mon, Jul 18, 2011 at 6:53 PM, Dumanshu duman...@gmail.com wrote: You have a sorted linked list of integers of some length you don't know and it keeps on increasing. You are given a number k. Print the position of the number k. Basically, you have to search for number k in the ever growing sorted list and print its position. Please write the complexity of whatever solution you propose. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
@Surender: it works. check it again using a sample case. i am first generating inplace BST for left and right subtrees. Now on this BSt i call the max node or min node function. Yes you are right, after the swap with root it will cause inconsistencies again. that is why the who recursion process is repeated after a swap. i.e. after u swap a node of left tree using min with the root, you call the binarytreetoBST(root-left) again from top to down. heres the way it works for each side of the tree left and right. first it creates bst for left and right, calls min and max, swaps value with root if required. Now root has the final value, no more changes required. now we call bttoBST(root-left) again if swap took place with left or bttoBST(root-right) if swap took place with right. So, whole process repeats two times. The second time we get the final fixed values for nodes. On Jul 18, 11:12 pm, surender sanke surend...@gmail.com wrote: @Dumanshu it also doesn't works, as min node doesn't satisfies bst conditions, u swap it but it again creates inconsistencies with its left subtree. void binarytreetobst(btree *root) { if(root == NULL) return; else if(root-left == NULL root-right == NULL) //base-case tree of size 1 return; else { binarytreetobst(root-left); binarytreetobst(root-right); btree* max = max_tree(root-left); if(max max-data root-data) { swap(max-data,root-data) binarytreetobst(root); return; } btree* min = min_tree(root-right); if(min min-data root-data) { swap(min-data,root-data) binarytreetobst(root); return; } } } other solutions 1. if O(n) space is allowed. fill array elements with tree elements - sort array - create BST 2. create a new BST from scratch while deleting from BT and inserting node into BST surender On Mon, Jul 18, 2011 at 10:55 PM, Dumanshu duman...@gmail.com wrote: got the code for BT to BST from a site.. btree* max_tree(btree *root) { if(root == NULL) return root; btree * current = root; while(current-right != NULL) { current = current-right; } return current; } btree * min_tree(btree *root) { if(root == NULL) return root; btree * current = root; while(current-left != NULL) { current = current-left; } return current; } void binarytreetobst(btree *root) { //base-case tree is empty if(root == NULL) return; else if(root-left == NULL root-right == NULL) //base-case tree of size 1 return; else { binarytreetobst(root-left); binarytreetobst(root-right); btree* max = max_tree(root-left); if(max max-data root-data) { int temp = root-data; root-data = max-data; max-data = temp; binarytreetobst(root-left); } btree* min = min_tree(root-right); if(min min-data root-data) { int temp = root-data; root-data = min-data; min-data = temp; binarytreetobst(root-right); } } } On Jul 18, 7:24 pm, Dumanshu duman...@gmail.com wrote: 1. //BT to BST - function used is to swap values Node* bubbleData(Node *root) { if(!root) return NULL; if(root-right) { if(root-data root-right-data) swap((root-data),(root-right-data)); root-right = bubbleData(root-right);} if(root-left) { if(root-data root-left-data) swap((root-data),(root-left-data)); root-left = bubbleData(root-left);} return bubbleData(root); } any case missing?? 3. Do we have to give an algorithm for garbage collection like Mark sweep algo or Do we have to write a code? if code, plz tell how to write? On Jul 18, 4:59 pm, Balaji S balaji.ceg...@gmail.com wrote: 1. Convert a binary tree to binary search tree inplace.. 2. Convert a DLL to a binary search tree that is balanced. 3. Implement a C++ garbage collector efficiently -- With Regards, Balaji.S- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com
[algogeeks] Re: MICROSOFT
@Balaji: for third question, were u asked to write the code? On Jul 18, 10:04 pm, Balaji S balaji.ceg...@gmail.com wrote: wats the mistake.. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: is it possible to detect the first repeating number in a 2-D array (n X n) in O(n) time ?
You have to use parallel computing to find the first repeating number in O(n) time if theres nothing special about the 2-D array Use n +1 processes, n processes to scan each row and 1 process to scan the rows last and next rows first element to check for repetition. Each process uses hash table to find the first non repeating number. When we have the results from all the processes, do O(n) scanning, and output the result for minimum row which wud be the first repetition. On Jul 18, 10:42 pm, snehi jain snehijai...@gmail.com wrote: this question was asked in an interview. Regards Snehi -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
@Gaurav: The best solution would be to manipulate the given BTree in place and get the BST. We don't need a separate tree but convert the existing one using the same space occupied by nodes of BT already in BST. On Jul 19, 2:06 am, Gaurav Popli gpgaurav.n...@gmail.com wrote: cant we just dotraverse using recursion and instead of printing it just pass to function which is making BST?? and is this right as someone above said first sort it and then make BST... dont we want that root of both Tree to be same or something like that...?? On Tue, Jul 19, 2011 at 2:17 AM, Dumanshu duman...@gmail.com wrote: @Balaji: for third question, were u asked to write the code? On Jul 18, 10:04 pm, Balaji S balaji.ceg...@gmail.com wrote: wats the mistake.. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Ever growing sorted linked list
@Gaurav: Ever Increasing means that you don't know the length of the list. So you have to assume some index n, traverse the list upto that point and check the results. If not found increment the n to some higher value. We are basically trying to improve the complexity by taking higher and higher jumps for n. On Jul 19, 2:03 am, Gaurav Popli abeygau...@gmail.com wrote: yeah...im saying to reach position n at which k is placed we have to trverse n positions from head or not...?? and i didnt understand the use of ever increasing...please elaborate on it.. On Tue, Jul 19, 2011 at 2:08 AM, Dumanshu duman...@gmail.com wrote: @Swathi: plz give the TC of ur algo (exponential plus log) On Jul 18, 10:14 pm, Swathi chukka.swa...@gmail.com wrote: The solution to this problem will be a combination of exponential increase and the binary search.. start = 0; end = 0; index =0; middle = 0; while (1) { if(a[start] == data) return start; if(a[end] == data) return end; if(data end) { start = end; end = pow(start,2); // here we are consider exponential for faster search. // no need to check any boundary conditions as the array is infinite continue; } else { // do binary search between start index and end index because data is inbetween a[start] and a[end] } } Hope this helps... On Mon, Jul 18, 2011 at 10:32 PM, Gaurav Popli gpgaurav.n...@gmail.comwrote: i dont understand it..if k is at position an let supposeso to check at that position dont you have to traverse till that position ...is thr anything else than the head of list...??or i understood wrongly...?? On Mon, Jul 18, 2011 at 9:55 PM, sagar pareek sagarpar...@gmail.com wrote: Update on 2nd line 2. if( ptr2=ptr1-next-next...(5 or 10 times) ) goto 3. else make linear search till NULL encounter and exit with the solution On Mon, Jul 18, 2011 at 7:41 PM, sagar pareek sagarpar...@gmail.com wrote: i have one approach :- first compare root-data and k if k is very much greater than root-data then set next=5or10 ur choice else set next 2or3 ur choice take two pointers ptr1 and ptr2 now lets take k is much greater than root-data then 1. set ptr1=root //initially 2. if( ptr2=ptr1-next-next...(5 or 10 times) ) else make linear search till NULL encounter 3. if ptr2-data==k return its position 4. else if (ptr2-datak) set ptr1=ptr2 goto 2 5. else traverse the ptr1 upto ptr2, if found return its position else return fail if anyone has more efficient solution then pls tell :) On Mon, Jul 18, 2011 at 6:53 PM, Dumanshu duman...@gmail.com wrote: You have a sorted linked list of integers of some length you don't know and it keeps on increasing. You are given a number k. Print the position of the number k. Basically, you have to search for number k in the ever growing sorted list and print its position. Please write the complexity of whatever solution you propose. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.-Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks
[algogeeks] Re: Tournament Tree
@Ankit: this particular problem can be solved with min heap implementation for tournament tree concept. But i want to code the actual tournament tree, where we can also find out the winner over all and all other contestants that lost to the winner. how to code that? Regards Dumanshu BITS-Pilani On Jul 19, 12:45 am, ankit sambyal ankitsamb...@gmail.com wrote: @Dumanshu : Implement the tournament tree as a Min Heap. Its prety straight forward to do that. If you have any doubt, plz ask ?? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Median of billion numbers - Map Reduce
Given billions of integers in a file. Memory Constraints exist so need to parallelize. One solution can be to split the file over a 100 machines and each machine calculates median of their part using quickselect and sends the result to host machine. Now the host calculates median of these medians and asks the sub machines to discard all the numbers less than this final median. So, map reduce! Now i want to do something like this - Each sub machine has billion/100 integers. It chooses an integer k randomly and divides its integers into two parts, one is less than k and other set is more than k (like quickselect algo). This machine returns to the host basically 3 things, one is k, second and third is number of elements in both sets. After having all that data from submachines, the host does some calculation and asks each machine to discard either the less than set or more than set. then whole thing is repeated with lesser number of elements. Any ideas about how the host machine would do that and on what basis? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Microsoft Interview Qn
plz give some example..sry but what do you mean by child sibling version?? On Jul 16, 3:46 pm, Reynald reynaldsus...@gmail.com wrote: Given a Parent -Child binary tree ,build the child -sibling version of it? Minimize the space requirements wherever possible. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MS question
Well, for the third case, you can elaborate on the implementation. We need to compare the first half with the later half. So, in case of even no. of characters it splits perfectly and in case of odd number of characters, just ignore the middle character. On Jul 17, 8:28 pm, swetha rahul swetharahu...@gmail.com wrote: is this ans sufficient..? any other possible test cases for palindrome ? On Sun, Jul 17, 2011 at 8:53 PM, Nishant mittal.nishan...@gmail.com wrote: 1.If string is NULL then it should return 1 i.e. string is palindrome 2.If there is only one character in string then it is palindrome 3.If reverse of given string is same as string On Jul 17, 8:18 pm, swetha rahul swetharahu...@gmail.com wrote: ya got it..thanks... how abt test cases for program to check whether a given string is palindrome or not..? On Sun, Jul 17, 2011 at 8:35 PM, ankit sambyal ankitsamb...@gmail.com wrote: 1. If u entered nothing and just pressed search, it should display nothing. 2. If u just entered a space and just pressed search, it should display nothing. 3.Verify the results are really related to give word or not 4.Check if proper Result is displayed for key word. 5. Check for the Order of the Result set, whether most relevant results are displayed first. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Median of billion numbers - Map Reduce
it will work. But i want to avoid case of computing the median by sub machines. So as to improve upon the complexity, i want the sub machines to just take a random number k and split their data into two sets and and then pass the information to host machine which after having all the outputs of sub machines tells them to discard or data on basis of something. I want this something. On Jul 17, 8:51 pm, Ashish Goel ashg...@gmail.com wrote: WOULDN'T MEDIAN OF MEDIANS WORK? mappers and reducers using hadoop Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sun, Jul 17, 2011 at 8:57 PM, Dumanshu duman...@gmail.com wrote: Given billions of integers in a file. Memory Constraints exist so need to parallelize. One solution can be to split the file over a 100 machines and each machine calculates median of their part using quickselect and sends the result to host machine. Now the host calculates median of these medians and asks the sub machines to discard all the numbers less than this final median. So, map reduce! Now i want to do something like this - Each sub machine has billion/100 integers. It chooses an integer k randomly and divides its integers into two parts, one is less than k and other set is more than k (like quickselect algo). This machine returns to the host basically 3 things, one is k, second and third is number of elements in both sets. After having all that data from submachines, the host does some calculation and asks each machine to discard either the less than set or more than set. then whole thing is repeated with lesser number of elements. Any ideas about how the host machine would do that and on what basis? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] In-memory dictionary
I want to have an in-memory dictionary. One word can have multiple meanings. 1. If you want to go with hash tables then do Suggest some good choices for hash functions. 2. Also, how can you modify this dictionary so that it can be used to solve a crossword puzzle? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Microsoft Interview Qn
@Ashish: if i got ur algo correct, contrary to all the above examples, u r forming a linked list of level order traversal of the tree. m i right? On Jul 17, 8:49 pm, Ashish Goel ashg...@gmail.com wrote: 1. PUSH ROOT IN Q 2. PUSH DUMMY NODE IN Q, DEFINE PREVIOUS NODE AS NULL 3. WHILE Q IS NOT EMPTY 3A. POP CURRENT NODE 3B. IF CURRENT NODE IS NOT DUMMY 3B1. IF PREVIOUS, PREVIOUS-SIBLING = CURRENT. 3B2. PREVIOUS = CURRENT 3B3. PUSH CURRENT-LEFT, CURRENT-RIGHT TO Q (ONLY IF THE NODES ARE NOT NULL) 3C IF CURRENT NODE IS DUMMY 3C1 IF PREVIOUS, PREVIOUS-SIBLING = NULL; 3C2 PUSH DUMMY ON Q Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sat, Jul 16, 2011 at 4:16 PM, Reynald reynaldsus...@gmail.com wrote: Given a Parent -Child binary tree ,build the child -sibling version of it? Minimize the space requirements wherever possible. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: In-memory dictionary
@Sagar: for 1 i need implementation of hash function please. for 2, any other suggestion? other than trie? because they are memory intensive. On Jul 18, 1:18 am, sagar pareek sagarpar...@gmail.com wrote: 1. you can use hash function with link list. 2. u can use trie with link list. On Mon, Jul 18, 2011 at 1:43 AM, Dumanshu duman...@gmail.com wrote: I want to have an in-memory dictionary. One word can have multiple meanings. 1. If you want to go with hash tables then do Suggest some good choices for hash functions. 2. Also, how can you modify this dictionary so that it can be used to solve a crossword puzzle? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- **Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
are u sure that this code works??? because last time i checked it didn't. On Jul 18, 1:22 am, hary rathor harry.rat...@gmail.com wrote: int dividend,divisor,remainder; int division(int p,int q){ int quotient=1; /*if divisor and diviend are equal then quotient=1*/ if(p==q){ remainder=0; return 1;} /*if dividend is smaller than divisor then remainder=dividend*/ if(pq){ remainder=p; return 0;} /*shift left till divisor dividend*/ while(p=q){ q=1; quotient=1;} /*shift right for one time so that divisor become smaller than dividend*/ q=1; quotient=1; /*again call division recurcively*/ quotient+=division(p-q,divisor); return quotient; } int * demo() { int i; long long long long long int multi=1; for(i=0;ia.len;i++) { multi*=a[i]; } for(i=0;ia.len;i++) { out[i]=mul[i]/a[i]; } }- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Median of billion numbers - Map Reduce
@Ashish: Ok, got it. The way which i wanted has higher complexity than the median of medians algo. So, median of medians would be the best strategy. Thanks. On Jul 17, 8:51 pm, Ashish Goel ashg...@gmail.com wrote: WOULDN'T MEDIAN OF MEDIANS WORK? mappers and reducers using hadoop Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sun, Jul 17, 2011 at 8:57 PM, Dumanshu duman...@gmail.com wrote: Given billions of integers in a file. Memory Constraints exist so need to parallelize. One solution can be to split the file over a 100 machines and each machine calculates median of their part using quickselect and sends the result to host machine. Now the host calculates median of these medians and asks the sub machines to discard all the numbers less than this final median. So, map reduce! Now i want to do something like this - Each sub machine has billion/100 integers. It chooses an integer k randomly and divides its integers into two parts, one is less than k and other set is more than k (like quickselect algo). This machine returns to the host basically 3 things, one is k, second and third is number of elements in both sets. After having all that data from submachines, the host does some calculation and asks each machine to discard either the less than set or more than set. then whole thing is repeated with lesser number of elements. Any ideas about how the host machine would do that and on what basis? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Find the missing number - Again given 4 billion integers
given 4 billion integers i.e 2^32 integers. find the integer that is not there in the list. Memory constraints - 2^16 bytes can be used at max. Does the presence of -ve numbers affect the solution?? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: MICROSOFT
@Hary: thanks, ur code works. Could someone tell me the complexity of the above mentioned code??? heres the working version of the same http://ideone.com/fDTfj Its increasing exponentially.. so can we say log_2(n)??? On Jul 18, 1:57 am, Dumanshu duman...@gmail.com wrote: are u sure that this code works??? because last time i checked it didn't. On Jul 18, 1:22 am, hary rathor harry.rat...@gmail.com wrote: ary: int dividend,divisor,remainder; int division(int p,int q){ int quotient=1; /*if divisor and diviend are equal then quotient=1*/ if(p==q){ remainder=0; return 1;} /*if dividend is smaller than divisor then remainder=dividend*/ if(pq){ remainder=p; return 0;} /*shift left till divisor dividend*/ while(p=q){ q=1; quotient=1;} /*shift right for one time so that divisor become smaller than dividend*/ q=1; quotient=1; /*again call division recurcively*/ quotient+=division(p-q,divisor); return quotient; } int * demo() { int i; long long long long long int multi=1; for(i=0;ia.len;i++) { multi*=a[i]; } for(i=0;ia.len;i++) { out[i]=mul[i]/a[i]; } }- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Google interview question
how about using voters algorithm? TC O(n) and SC O(1) On Jul 16, 6:28 am, Anand Shastri anand.shastr...@gmail.com wrote: Given a file containing 4,300,000,000 integers, how can you *find **one* that *appears* at *least **twice* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: amazon
heres another approach. For given n sets, all permutations have a { in the beginning and } in the end. So, we need to permute the middle string with (n-1) sets. I have generated all the permutations of n sets i.e. say n ==3 then generate all permutations of (n-1==2 sets) {}{} string. Now before printing each permutation, run a check on it. This check will run through the middle permutation with sum initialized to 0 and then sum-- for every } encountered and sum++ for every { encountered for left to right traversal. heres the code: output complies with catalan numbers. http://ideone.com/AgFeH i have a doubt regarding the complexity or lets say the efficiency. Any comments? On Jul 14, 9:10 am, shilpa gupta shilpagupta...@gmail.com wrote: 1. Write the code for this input: 1 output: {} input: 2 output: {}{} {{}} input: 3 output: {}{}{} {{}}{} {}{{}} -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: GOOGLE Q
how about this? take pointer p1 set to end of array A and pointer p2 set to end of array B. while(you get n values) { print A(p1),B(p2) now if( A(p1-1)+B(p2)A(p1)+B(p2-1) ) then print A(p1-1),B(p2) followed by print A(p1),B(p2-1) decrement p2 and p1 both } m i missing something? On Jul 9, 4:24 am, Piyush Sinha ecstasy.piy...@gmail.com wrote: Given two sorted positive integer arrays A(n) and B(n). We define a set S = {(a,b) | a in A and b in B}. Obviously there are n^2 elements in S. The value of such a pair is defined as Val(a,b) = a + b. Now we want to get the n pairs from S with largest values. The tricky part is that we need an O(n) algorithm. -- *Piyush Sinha* *IIIT, Allahabad* *+91-8792136657* *+91-7483122727* *https://www.facebook.com/profile.php?id=10655377926* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: String burn,measure time puzle
@oppilas: light the first string at both ends and in the middle. So it will burn completely in 15 min as the fire is advancing in 4 ways irrespective of the burn rate. when the first string is completely burnt, light the second string at both ends to get another 30 min. So, overall 45 min. On Jul 9, 5:11 am, oppilas . jatka.oppimi...@gmail.com wrote: You have 2 pieces of string of different, unspecified length, and some matches. Each piece of string takes exactly an hour to burn, but the burn rate is not constant. . The strings have different burn rates, and of course you don't know the rates anyway. Using only the matches and the strings, measure 45 minutes. I have thought a lot but unable to find a solution for this problem. Can someone please try it. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: String burn,measure time puzle
The above posted solution is independent of the burning rate. I am not counting the minutes using the half burnt string. Instead, the fire is running and when the whole string is burnt, I am using the time. Say u light up a string from both ends, then irrespective of burn rate it should give 30 min. Because we have the choice to lit the string from either end and it burns completely in 1 hour. On Jul 9, 10:09 pm, oppilas . jatka.oppimi...@gmail.com wrote: Yes, these solution are valid. But for them the burning rate of each string must be constant. Each piece of string takes exactly an hour to burn, but the burn rate is not constant Can't we have a string which take 45 minutes to burn till half length. 0-L/2. And 15 min from L/2 to L. On Sat, Jul 9, 2011 at 8:57 PM, Dumanshu duman...@gmail.com wrote: @oppilas: light the first string at both ends and in the middle. So it will burn completely in 15 min as the fire is advancing in 4 ways irrespective of the burn rate. when the first string is completely burnt, light the second string at both ends to get another 30 min. So, overall 45 min. On Jul 9, 5:11 am, oppilas . jatka.oppimi...@gmail.com wrote: You have 2 pieces of string of different, unspecified length, and some matches. Each piece of string takes exactly an hour to burn, but the burn rate is not constant. . The strings have different burn rates, and of course you don't know the rates anyway. Using only the matches and the strings, measure 45 minutes. I have thought a lot but unable to find a solution for this problem. Can someone please try it. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: String burn,measure time puzle
the string burns in an hour if lit from one end and string burns in 30 min if lit from both ends. This fact is based on - light up the string from either end it burns in 60 min. now heres the solution: Light up the first string from both ends and the second string from one end at the same time. When the first string burns up completely, light up the already burning second string from the other end. When the second string burns up, we have 45 min irrespective of the burn rate. Even if you assume some different values of burn rate to make the overall burn rate variable, you would get 45 min only. On Jul 9, 5:11 am, oppilas . jatka.oppimi...@gmail.com wrote: You have 2 pieces of string of different, unspecified length, and some matches. Each piece of string takes exactly an hour to burn, but the burn rate is not constant. . The strings have different burn rates, and of course you don't know the rates anyway. Using only the matches and the strings, measure 45 minutes. I have thought a lot but unable to find a solution for this problem. Can someone please try it. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Amazon Online question
There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs who have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs, output the moves required to transform from the initial to final configuration. You are required to do the transformations in minimal number of moves. A move consists of picking the topmost disc of any one of the pegs and placing it on top of anyother peg. At anypoint of time, the decreasing radius property of all the pegs must be maintained. Constraints: 1= N=8 3= K=5 Time Limit: 60 seconds. Input Format: N K 2nd line contains N integers, each in the range 1 to K, the i-th integer denotes, the peg to which disc of radius i is present in the initial configuration. 3rd line denotes the final configuration in a format similar to the initial configuration. Output Format: The first line contains M - The minimal number of moves required to complete the transformation. The following M lines describe a move, by a peg number to pick from and a peg number to place on. If there are more than one solutions, it's sufficient to output any one of them. You can assume, there is always a solution with less than 7 moves and the initial confirguration will not be same as the final one. Sample Input #00: 2 3 1 1 2 2 Sample Output #00: 3 1 3 1 2 3 2 Sample Input #01: 6 4 4 2 4 3 1 1 1 1 1 1 1 1 Sample Output #01: 5 3 1 4 3 4 1 2 1 3 1 code would also suffice. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] find the integer
given an array of intergers. find the any integer that occurs only once. Others might occur any no. of times. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: find the integer
no constraints but try to give the optimized solution and plz no hashing. On Jul 8, 7:02 pm, Dumanshu duman...@gmail.com wrote: given an array of intergers. find the any integer that occurs only once. Others might occur any no. of times. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon
how about this? given n milestones 1.find max number from the array and delete it. 2.now find any (x,y) both from the array such that x+y == max. 3. put min(x,y) in the front of output array, delete y from array and if(sizeof(output array)==(n-2)) put x also in front of output array and exit. else goto 1 with max=x. complexity is screwed up. :( any counter case? On Jul 7, 1:23 pm, Akshata Sharma akshatasharm...@gmail.com wrote: There is a straight roads with 'n' number of milestones. You are given an array with the distance between all the pairs of milestones in some random order. Find the position of milestones. Example: Consider a road with 4 milestones(a,b,c,d) : a --- 3Km ---b--- 5Km ---c--- 2Km ---d Distance between a and b = 3 Distance between a and c = 8 Distance between a and d = 10 Distance between b and c = 5 Distance between b and d = 7 Distance between c and d = 2 All the above values are given in a random order say 7, 10, 5, 2, 8, 3. The output must be 3,5,2 or 2,5,3 -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon
Initially we can sort the array in O(nlogn) and then given a max value, find a pair (x,y) in O(n). here n is also of quadratic order if taken in terms of no. of milestones. On Jul 7, 5:52 pm, Dumanshu duman...@gmail.com wrote: how about this? given n milestones 1.find max number from the array and delete it. 2.now find any (x,y) both from the array such that x+y == max. 3. put min(x,y) in the front of output array, delete y from array and if(sizeof(output array)==(n-2)) put x also in front of output array and exit. else goto 1 with max=x. complexity is screwed up. :( any counter case? On Jul 7, 1:23 pm, Akshata Sharma akshatasharm...@gmail.com wrote: There is a straight roads with 'n' number of milestones. You are given an array with the distance between all the pairs of milestones in some random order. Find the position of milestones. Example: Consider a road with 4 milestones(a,b,c,d) : a --- 3Km ---b--- 5Km ---c--- 2Km ---d Distance between a and b = 3 Distance between a and c = 8 Distance between a and d = 10 Distance between b and c = 5 Distance between b and d = 7 Distance between c and d = 2 All the above values are given in a random order say 7, 10, 5, 2, 8, 3. The output must be 3,5,2 or 2,5,3- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Improve upon O(m^2)
given two sorted arrays a[m] b[2*m], each contains m elements only. You need to merge those two arrays into second array b[2*m]. anything better than O(m^2) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Merging Sorted Arrays
@Radha: could u plz elaborate on getting the first n elements??? On Jul 8, 12:55 am, radha krishnan radhakrishnance...@gmail.com wrote: ok ! i got a O(n lgn) finally i don know exact complexity Let N = size of first array Find the first N smallest elements using one pointer in each array now swap the list of elements from index 0 to second-pointer in second array to first array with first_poiner+1 to N in first Array I think this is O(n) On Thu, Jul 7, 2011 at 12:53 PM, Piyush Sinha ecstasy.piy...@gmail.com wrote: @radha...i have an algo but its complexity is O(n^2)...check the following and see if there is any bug as I havent tested for all cases...also suggestions are welcomed:) main() { int a[]= {1,3,77,78,88}; int b[]= {2,5,79,80,81,99}; int i=sizeof(a)/sizeof(a[0]) - 1; int j=sizeof(b)/sizeof(b[0]) - 1; int temp,k,m; while(j=0) { if(a[i]b[j]) { temp = a[i]; k=m=i; while(b[j]a[k-1]) k--; while(i-k) { a[i] = a[i-1]; i--; } a[i] = b[j]; b[j] = temp; i = m; } j--; } for(k=0;ksizeof(a)/sizeof(a[0]);k++) printf(%d ,a[k]); puts(\n); for(k=0;ksizeof(b)/sizeof(b[0]);k++) printf(%d ,b[k]); puts(\n); system(pause); } On 7/8/11, radha krishnan radhakrishnance...@gmail.com wrote: :Given two sorted arrays a[]={1,3,77,78,90} and b[]={2,5,79,81}. Merge these two arrays, no extra spaces are allowed. Output has to be a[]={1,2,3,5,77} and b[]={78,79,81,90}. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- *Piyush Sinha* *IIIT, Allahabad* *+91-8792136657* *+91-7483122727* *https://www.facebook.com/profile.php?id=10655377926* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Some adobe interview questions.
ans1. use counting sort for character array (0 to 255) then check for the second string if same or not. ans2. send 1 and 2, 1 comes back, send 10 and 5, 2 comes back, send 2 and 1 ans3. As vikas said, sum of digits should b 8. In that case the number must have a zero digit because 1st digit the no. of zeroes can't be zero then. right? print all the combinations. need help on this. ans4. a function with string, first index, last index as arguments and return true or false. increment first index and decrement last index for recursive call. ans5. I have no idea. may b we can take help of shunting algorithm. but there ought to be an easier way to do this. Do we have to WAP a general one for these expressions??? ans6. allocate same number of bytes for the reversed string. now traverse the original string from the back. as soon as u encounter a space say at position x or you come to 0 index, call thecurrent = function (x+1,current) or current = function(0,current). this function copies the original string from position x+1 to (while(space or null)) in reversed string at position current and returns current + no. of characters copied. ans8. somebody plz explain this. do we have to find the values for a,b,c,d,e,f so that we can get return change for 100,50,25,10,5? 10, 5,5,20,25,50... how to get change for 5 then? ans9. lets say we have to allocate 100*200 i.e. rows 100 and columns 200 int **a = (int **)malloc(sizeof(int *)*100); a[0] = (int *)malloc(sizeof(int)*100*200); for(i=1;i100;i++) a[i] = (a[i-1]+200); ans10. use hashtables, first traversal get true for all values present. then second traversal, check if (X- arr[i]) is present in hashtable, if yes print. ans11. i think ans to this one is 1 byte. because some information is stored.. may be. m nt sure :P ans12. typedef struct node * NODE; NODE sortlist(NODE head,int n) { if(n==1) return head; if(!n) return null; NODE temp =head; for(int i=0;in\2;i++)temp=temp-next; return merge(sortlist(head,n/2),sortlist(temp,n/2 + (n%2));); } NODE merge(NODE t1,NODE t2) { NODE head=NULL,temp=NULL; int set =0; while(t1 t2) { if(t1-data t2-data) { if(!set){head=t1;temp = head;set=1;t1=t1-next;} else { temp-next = t1; t1=t1-next; } } else { if(!set){head=t2;temp = head;set=1;t2=t2-next;} else { temp-next = t2; t2=t2-next; } } while(t1) { temp-next = t1; t1 = t1-next; } while(t2) { temp-next = t2; t2 = t2-next; } temp-next = NULL; return head; } did i miss any case? On Jul 5, 11:24 am, vikas mehta...@gmail.com wrote: These all were asked cumulatively in five interviews, one after another. Q1 given 2 string A ,B. write a code to find all character of A exists in B or not? Q2. A puzzle athttp://mathforum.org/library/drmath/view/56756.html Q3. Find a 8-digit number, where the first figure defines the count of zeros in this number, the second figure the count of numeral 1 in this number and so on Q4. write a recursive function to find a given string is palindrome or not? Q5. write a code to generate the parse tree like compilers do internally for any given expression e.g. a+(b+c*(e/f)+d)*g Q6. 3 reverse the string word by word e.g. My name is pradeep .. o/p shd be pradeep is name My ...intwer expecting a 1 traversal algo Q7. C++ (What are virtual functions) what happened if I do Base *d = new Derived(); and Derived *d = new Base(); is it error(in which statement) or not if yes what type of error? Q8. you have 6 coins of Indian denomination (a+b+c+d+e+f=115 paisa) ...if user ask for a change of 100,50,25,10,5 paisa then you r not able to give find the value of a,b,c,d,e,f.e.g. lets if user ask for a change of 25 paisa then you r not able to return (10+10+5) or (20+5) Q9. allocate 2D array dynamically with min no. of malloc calls. Q10. given unsorted array and a no. X ,find 2 no. whose sum is Xa[i]+a[j]=X ...do it in O(n) Q11. class A {}; A obj ; what is sizeof(obj)explain ANS Q12. Write a code of Merge sort on Linked list. Lets discuss them -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Some adobe interview questions.
@Azhar: could u plz explain the q8. problem. On Jul 5, 12:13 pm, Azhar Hussain azhar...@gmail.com wrote: Q8 is a DP problem, here is the solution #include stdio.h #define MAX 125 #define VAL 6 int Min[MAX]; int N[VAL] = {5, 10, 20, 25, 50, 100}; int main(void) { int i, j; for (i = 1; i MAX; i++) Min[i] = 100; for (i = 5; i MAX; i += 5) for (j = 0; j VAL; j++) if ((N[j] = i) ((Min[i-N[j]] + 1) Min[i])) Min[i] = Min[i-N[j]] + 1; fprintf(stderr, \n***\n); for (i = 0; i MAX; i += 5) fprintf(stderr, %d = %d\n, i, Min[i]); return 0; } OUTPUT: *** 0 = 0 5 = 1 10 = 1 15 = 2 20 = 1 25 = 1 30 = 2 35 = 2 40 = 2 45 = 2 50 = 1 55 = 2 60 = 2 65 = 3 70 = 2 75 = 2 80 = 3 85 = 3 90 = 3 95 = 3 100 = 1 105 = 2 110 = 2 115 = 3 120 = 2 more information can be found athttp://www.topcoder.com/tc?module=Staticd1=tutorialsd2=dynProg - Azhar. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Some adobe interview questions.
@Saurabh: u can improve upon ur solution. char * outstr; int c =0; void getreversedwords(string str) { index = str.length()-1; //outstr is the answer stack st; while(index) { st.push(str[index]); if(st.top() == ' ') //top is a space st.printstack(); index--; } if(st.isEmpty()==false) st.printstack(); } void printstack() { int set =1; char ch; if(st.top()==' ') { ch = st.pop(); set =0; } while(st.isEmpty()==false) outstr[c++] = st.pop(); if(!set) outstr[c++] = ch; } On Jul 5, 4:10 pm, saurabh singh saurab...@gmail.com wrote: well for question 6 I think calculating size of string will count as one traversal? Correct me if I am wrong? My approach: traverse storing each char in a string.when space encountered push the string in stack I am not sure how my solution is but it doesnt appears gud ryt now. On Tue, Jul 5, 2011 4:34 PM, Dumanshu duman...@gmail.com wrote: ans1. use counting sort for character array (0 to 255) then check for the second string if same or not. ans2. send 1 and 2, 1 comes back, send 10 and 5, 2 comes back, send 2 and 1 ans3. As vikas said, sum of digits should b 8. In that case the number must have a zero digit because 1st digit the no. of zeroes can't be zero then. right? print all the combinations. need help on this. ans4. a function with string, first index, last index as arguments and return true or false. increment first index and decrement last index for recursive call. ans5. I have no idea. may b we can take help of shunting algorithm. but there ought to be an easier way to do this. Do we have to WAP a general one for these expressions??? ans6. allocate same number of bytes for the reversed string. now traverse the original string from the back. as soon as u encounter a space say at position x or you come to 0 index, call the current = function (x+1,current) or current = function(0,current). this function copies the original string from position x+1 to (while(space or null)) in reversed string at position current and returns current + no. of characters copied. ans8. somebody plz explain this. do we have to find the values for a,b,c,d,e,f so that we can get return change for 100,50,25,10,5? 10, 5,5,20,25,50... how to get change for 5 then? ans9. lets say we have to allocate 100*200 i.e. rows 100 and columns 200 int **a = (int **)malloc(sizeof(int *)*100); a[0] = (int *)malloc(sizeof(int)*100*200); for(i=1;i100;i++) a[i] = (a[i-1]+200); ans10. use hashtables, first traversal get true for all values present. then second traversal, check if (X- arr[i]) is present in hashtable, if yes print. ans11. i think ans to this one is 1 byte. because some information is stored.. may be. m nt sure :P ans12. typedef struct node * NODE; NODE sortlist(NODE head,int n) { if(n==1) return head; if(!n) return null; NODE temp =head; for(int i=0;in\2;i++)temp=temp-next; return merge(sortlist(head,n/2),sortlist(temp,n/2 + (n%2));); } NODE merge(NODE t1,NODE t2) { NODE head=NULL,temp=NULL; int set =0; while(t1 t2) { if(t1-data t2-data) { if(!set){head=t1;temp = head;set=1;t1=t1-next;} else { temp-next = t1; t1=t1-next; } } else { if(!set){head=t2;temp = head;set=1;t2=t2-next;} else { temp-next = t2; t2=t2-next; } } while(t1) { temp-next = t1; t1 = t1-next; } while(t2) { temp-next = t2; t2 = t2-next; } temp-next = NULL; return head; } did i miss any case? On Jul 5, 11:24 am, vikas mehta...@gmail.com wrote: These all were asked cumulatively in five interviews, one after another. Q1 given 2 string A ,B. write a code to find all character of A exists in B or not? Q2. A puzzle athttp://mathforum.org/library/drmath/view/56756.html Q3. Find a 8-digit number, where the first figure defines the count of zeros in this number, the second figure the count of numeral 1 in this number and so on Q4. write a recursive function to find a given string is palindrome or not? Q5. write a code to generate the parse tree like compilers do internally for any given expression e.g. a+(b+c*(e/f)+d)*g Q6. 3 reverse the string word by word e.g. My name is pradeep .. o/p shd be pradeep is name My ...intwer expecting a 1 traversal algo Q7. C++ (What are virtual functions) what happened if I do Base *d = new Derived(); and Derived *d = new Base(); is it error(in which statement) or not if yes what type of error? Q8. you have 6 coins of Indian denomination (a+b+c+d+e+f=115 paisa) ...if user ask for a change of 100,50,25,10,5 paisa then you r not able to give find the value of a,b,c,d,e,f.e.g. lets if user ask for a change of 25 paisa then you r not able to return (10+10+5) or (20+5) Q9. allocate 2D array dynamically with min no. of malloc calls. Q10. given unsorted array and a no. X ,find 2 no. whose
[algogeeks] Re: Interview Question
xor all the elements of both arrays ==0 sum of 1st array == sum of 2nd array no. of elements in 1st == no. of elements in 2nd if the above conditions are met, they have the same set. m i missin sth? On Jul 3, 1:23 am, mittal mohitm.1...@gmail.com wrote: Given two arrays of numbers, find if each of the two arrays have the same set of ntegers ? Suggest an algo which can run faster than NlogN without extra space? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon - Longest palindrome in a string in O(n)
@hary: nhin, its some constant multiple of n. O(constant*n) for a string of length n, 2*n-1 positions are there. every time i calculate the next center using prev center value if the value at prev center is a large one then this next center value skips that many positions to compensate.. if the value at prev center is small then next center won't skip positions it may be just the next one. so overall, it gives linear complexity. On Jul 1, 12:35 pm, hary rathor harry.rat...@gmail.com wrote: @Dumanshu; worst case O(n3) hai iska -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Longest substring 0's 1's
@sunny: if a[i]==0 then 0,i is the solution suppose a[i]==a[j] ==0 now 0,i and 0,j is the solution. is i,j also the solution?? On Jul 1, 4:08 pm, sunny agrawal sunny816.i...@gmail.com wrote: String = 1 0 1 1 0 1 1 1. 1. make the array = 1 -1 1 1 -1 1 1 1 2. after second operation array = 1 0 1 2 1 2 3 4 index = 0 1 2 3 4 5 6 7 a[1] = 0 - [0,1] is a solution a[0] = a[2] = a[4] so (0,4],(0,2],(2,4] are also solutions a[3] = a[5] (3,5] is also a solution. This Solution is O(n) in Both TC and SC I have read this Question at many places but never seen a solution with O(n) TC and O(1) space where did you find these constraints ?? On Fri, Jul 1, 2011 at 4:21 PM, Anantha Krishnan ananthakrishnan@gmail.com wrote: Thanks. Can you plz explain for input 1 0 1 1 0 1 1 1. Also I want solution in O(n) TC and O(1) SC. Regards Anantha Krishnan On Fri, Jul 1, 2011 at 4:13 PM, sunny agrawal sunny816.i...@gmail.comwrote: Take an array of size of the length of the string. fill the array positions with one where string contains 1, and -1 where it is 0 Now for each i (1,n-1) perform the following operation a[i] = a[i-1] + a[i] now a[i] will contains sum of values from a[0] to a[i] in original array. Now only thing remains to find is two indexes in this array such that a[i] = a[j] where ever this condition is met ( i,j ] is a solution or if for some i a[i] = 0 in this case [0,i] will be a solution this can be done using hashing hash the values in the array of size 2*n+1. as range of values is -n to n and keep track of max interval. On Fri, Jul 1, 2011 at 3:22 PM, Anantha Krishnan ananthakrishnan@gmail.com wrote: Given a string containing 0's and 1's. One would need to find the longest sub-string such that the count of 0's is equal to the count of 1's in the largest sub string. Regards Anantha Krishnan -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: conditional compilation
@himanshu: preprocessor is a program which is called by the compiler before the actual compilation. see, for the preprocessor directive #if there is a condition that it should be followed by a constant because it is evaluated before compiling the program. Now these constants can be constant say 1,8, etc or it should be define using another preprocessor directive #define. In ur program, #if i==0, the preprocessor first sees if i here is defined using #define because its not a constant. Since u haven't, it takes the by default value for constant i as 0. This i is not ur int i, but it is seen as a #define stuff. see this program for example #includestdio.h #includestdlib.h #define p 1 int main() { #if i==1 printf(doom); #endif int i=1; #if j==0 printf(1234); #endif #if k==5 printf(chaffeur); #endif #if p==1 printf(srikant); #endif return 0; } it prints: 1234 and srikant. the statement int i=1 has no role in preprocessing. I have not defined j or k which is used by #if so it will take it as value 0 and checks for the condition which evaluates to true and false respectively. whereas i have defined constant p as 1 using #define, so this condition will evaluate to true printing srikant. In case u want to see the output just after preprocessor runs i.e. before compilation use gcc -E filename. Hope this clears ur doubt. Regards Doom On Jul 2, 12:08 am, himanshu kansal himanshukansal...@gmail.com wrote: If i do #if i==0 thn it vl cmpl it..means its tkng it as 0..bt why... On 7/1/11, Dumanshu duman...@gmail.com wrote: i think it will take a garbage value for i because these are preprocessor directives. On Jun 30, 4:46 pm, himanshu kansal himanshukansal...@gmail.com wrote: 1 more thngif its possible to eval. variable expression lyk abv... thn... int i=2; #if i==2 printf(hi); #else printf(bye); //prints byei think it shld print hi... #endif even this code prints bye. int i=3; #if i==2 printf(hi); #else printf(bye); #endif On Thu, Jun 30, 2011 at 5:13 PM, himanshu kansal himanshukansal...@gmail.com wrote: somewhere i read dt conditional compilation such as #if and #elif etc cn only use constant expressions and previously defined macrosthey cant use variables cz preprocessng is done b4 compilation... bt whn i try it on gcc and vc both r compiling it with variables abs. fyneven disabling extensions dint work i.e int i; #if i+1 printf(hi); vl print hi...no error occurs #endif thn i read it on net dt it cn eval. varables also nw m confused who is ryt... whtr it cn eval var also or nt... nd if it cn thn hws it possible dt preprocessor knows d val. of var. b4 compiling d prog... -- Regards Himanshu Kansal Msc Comp. sc. (University of Delhi) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sent from my mobile device Regards Himanshu Kansal Msc Comp. sc. (University of Delhi) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: conditional compilation
In ur second code change int i=2 and run it. It would still print bye. check the result of preprocessor using -E flag. On Jun 30, 4:46 pm, himanshu kansal himanshukansal...@gmail.com wrote: 1 more thngif its possible to eval. variable expression lyk abv... thn... int i=2; #if i==2 printf(hi); #else printf(bye); //prints byei think it shld print hi... #endif even this code prints bye. int i=3; #if i==2 printf(hi); #else printf(bye); #endif On Thu, Jun 30, 2011 at 5:13 PM, himanshu kansal himanshukansal...@gmail.com wrote: somewhere i read dt conditional compilation such as #if and #elif etc cn only use constant expressions and previously defined macrosthey cant use variables cz preprocessng is done b4 compilation... bt whn i try it on gcc and vc both r compiling it with variables abs. fyneven disabling extensions dint work i.e int i; #if i+1 printf(hi); vl print hi...no error occurs #endif thn i read it on net dt it cn eval. varables also nw m confused who is ryt... whtr it cn eval var also or nt... nd if it cn thn hws it possible dt preprocessor knows d val. of var. b4 compiling d prog... -- Regards Himanshu Kansal Msc Comp. sc. (University of Delhi) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Google Interview
@Jitendra: where r u using k in the first call? On Jun 30, 8:56 am, Jitendra singh jsinghrath...@gmail.com wrote: kthlargest(a[],b[],lefta,righta,leftb,rightb,k) { mida=lefta+(righta-lefta)/2; midb=leftb+(rightb-leftb)/2; if(a[mida]bmidb]) kthlargest(a[],b[],mida+1,righta,leftb,midb-1); elseif(a[mida]bmidb]) kthlargest(a[],b[],lefta,mida-1,midb+1,rightb,); else return a[mida]; } On 6/25/11, Anantha Krishnan ananthakrishnan@gmail.com wrote: Idea is like this since both the arrays may not be of same length lets divide (k-1) smallest elements proportionally in both the arrays: let i point the array A by i=m/(m+n) * (k-1) [since we have to divide k-1 elements among two] j=(k-1) - i then try to insert A[i] between B[j-1] and B[j] if three are not in asc order try to insert B[j] between A[i-1] and A[i] If any one of the above satisfies we found kth smallest element else, check which one is smallest among A[i] and B[j] its logical that if A[i] is smallest then we can A[0] to A[i] for the next iteration and k becomes k-i-1 also m becomes m-i-1 i.e now we have only m-i-1+n elements out of which we have to find k-i-1th smallest thus the iteration goes on until we find our kth smallest element. Consider 2 arrays A={5,7,9,20}; length of A: m=4 B={10,12,21,27,35,50}; length of B: n=6 let K be 4 i=4/10*3=1; A[1]=7; j=3-1=2; B[2]=21; B[1]=12 A[1]=7 B[2]=21 [not in asc order] A[0]=5 B[2]=21 A[1]=7 [not in asc order] so now, k=k-i-1 =4-1-1=2 m=m-i-1=4-1-1=2 n=6 A={9,20}; length of A: m=2 B={10,12,21,27,35,50}; length of B: n=6 i=2/8*1=0; A[0]=9; j=1-0=1; B[1]=12; (acutally A[-1] is just for understanding) B[0]=10 A[0]=9 B[1]=12 [not in asc order] A[-1]=-INF B[1]=12 A[0]=9 [not in asc order] now, k=k-i-1=2-0-1=1; m=m-i-1=2-0-1=1; n=6; A={20}; length of A: m=1 B={10,12,21,27,35,50}; length of B: n=6 i=1/7*0=0; A[0]=20; j=0-0=0; B[0]=10; (acutally A[-1] and B[-1] are just for understanding) B[-1]=-INF A[0]=20 B[0]=10 [not in asc order] A[-1]=-INF B[0]=10 A[0]=20 [in asc order] We got the Kth(4th) smallest element which is 10. Thanks Regards, Anantha Krishnan On Fri, Jun 24, 2011 at 11:20 PM, Akshata Sharma akshatasharm...@gmail.comwrote: @Anantha can you explain the logic please? On Fri, Jun 24, 2011 at 10:21 PM, Anantha Krishnan ananthakrishnan@gmail.com wrote: Check thishttp://ideone.com/C8fQC http://ideone.com/C8fQCThanks Regards, Anantha Krishnan On Fri, Jun 24, 2011 at 10:18 PM, Decipher ankurseth...@gmail.comwrote: Can anybody please explain how to solve this question with logarithmic time complexity ? Write the code/algorithm to find the k-th Smallest Element in the Union of Two Sorted Arrays . -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon - Longest palindrome in a string in O(n)
heres the code: http://ideone.com/aiG1m using the algo from http://www.akalin.cx/2007/11/28/finding-the-longest-palindromic-substring-in-linear-time/ On Jun 21, 11:31 pm, Swathi chukka.swa...@gmail.com wrote: Does any one know how to return the Longest palindrome in a string in O(n). From googling i found that we can use suffix trees but there is no code. I am looking for logic and also for running code. Thanks, Swathi -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Google Interview
check this code http://ideone.com/rX130 compares k/2 values of each array taking care of extremes. On Jun 24, 9:48 pm, Decipher ankurseth...@gmail.com wrote: Can anybody please explain how to solve this question with logarithmic time complexity ? Write the code/algorithm to find the k-th Smallest Element in the Union of Two Sorted Arrays . -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: conditional compilation
i think it will take a garbage value for i because these are preprocessor directives. On Jun 30, 4:46 pm, himanshu kansal himanshukansal...@gmail.com wrote: 1 more thngif its possible to eval. variable expression lyk abv... thn... int i=2; #if i==2 printf(hi); #else printf(bye); //prints byei think it shld print hi... #endif even this code prints bye. int i=3; #if i==2 printf(hi); #else printf(bye); #endif On Thu, Jun 30, 2011 at 5:13 PM, himanshu kansal himanshukansal...@gmail.com wrote: somewhere i read dt conditional compilation such as #if and #elif etc cn only use constant expressions and previously defined macrosthey cant use variables cz preprocessng is done b4 compilation... bt whn i try it on gcc and vc both r compiling it with variables abs. fyneven disabling extensions dint work i.e int i; #if i+1 printf(hi); vl print hi...no error occurs #endif thn i read it on net dt it cn eval. varables also nw m confused who is ryt... whtr it cn eval var also or nt... nd if it cn thn hws it possible dt preprocessor knows d val. of var. b4 compiling d prog... -- Regards Himanshu Kansal Msc Comp. sc. (University of Delhi) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon - max substring
I have used suffix arrays. Its easier to implement than trees. code here- http://ideone.com/kX8MV In case u find a test case givin wrong output plz do tell. On Jun 29, 9:54 pm, Swathi chukka.swa...@gmail.com wrote: Given a string (assume there no spaces or punctuations), write a code that returns the max. length of the string that has repeated more than once. Thanks, Swathi -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon - max substring
for the input doomdoomdoom output is 4 or 8? On Jun 30, 2:04 am, Dumanshu duman...@gmail.com wrote: I have used suffix arrays. Its easier to implement than trees. code here-http://ideone.com/kX8MV In case u find a test case givin wrong output plz do tell. On Jun 29, 9:54 pm, Swathi chukka.swa...@gmail.com wrote: Given a string (assume there no spaces or punctuations), write a code that returns the max. length of the string that has repeated more than once. Thanks, Swathi -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon - max substring
if the output for the input doomdoomdoom is 8 then refer to http://ideone.com/kX8MV else if the output is 4 then refer to http://ideone.com/3tAKN the second one is having a higher complexity. ny suggestions? On Jun 29, 9:54 pm, Swathi chukka.swa...@gmail.com wrote: Given a string (assume there no spaces or punctuations), write a code that returns the max. length of the string that has repeated more than once. Thanks, Swathi -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Amazon - max substring
O(n^2) and O(n^3) respectively. On Jun 30, 3:47 am, Dumanshu duman...@gmail.com wrote: if the output for the input doomdoomdoom is 8 then refer tohttp://ideone.com/kX8MV else if the output is 4 then refer tohttp://ideone.com/3tAKN the second one is having a higher complexity. ny suggestions? On Jun 29, 9:54 pm, Swathi chukka.swa...@gmail.com wrote: Given a string (assume there no spaces or punctuations), write a code that returns the max. length of the string that has repeated more than once. Thanks, Swathi -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: NEED ALGO IN TIME 0.00
@Rizwan: don't u think for the counting sort part, if u use 11 int array as it is without copying values in the main array, it would run faster? And later on, get the sum from the two 11 int arrays like I did. Although m using a single buffer so m getting 0.01. -http://ideone.com/lsK8n So, by using a buffer of 512, u might b able to get a time of 0.0. What do u think? On Jun 27, 2:05 am, rizwan hudda rizwanhu...@gmail.com wrote: I am able to get this accepted with 0.00 Secondshttp://ideone.com/Eg2wZ But, I am using 512 KB Buffer. Not sure how I do get 0.00 with a smaller buffer size say 8KB On Sun, Jun 26, 2011 at 7:54 AM, Wladimir Tavares wladimir...@gmail.com wrote: sometimes using global static variables may be better to use dynamic variables on the stack! Sometimes! Wladimir Araujo Tavares Federal University of Ceará On Sat, Jun 25, 2011 at 7:32 PM, Dumanshu duman...@gmail.com wrote: finally got it in 0.01 sec using all the optimizations i am aware of including what Wladimir had suggested. Now wat to do for 0.00??? heres my code- http://ideone.com/lsK8n please suggest if any further optimizations possible. On Jun 26, 2:21 am, Dumanshu duman...@gmail.com wrote: Ok, it seems like bucket sort is the best way to do it. I got 0.06 and to go further weneedto use char buffer like the one Wladimir suggested. But still i have a doubt that would it be possible to reduce 0.06 to 0.00 using that buffer? I don't think there can be any possible improvement in logic. wat do u think? On Jun 26, 12:25 am, Dumanshu duman...@gmail.com wrote: see i got 0.07 sec as the time after using counting sort.. because the hotness scale is 0-10 so i used an array of 11 ints to count the no. of occurrences. But i m nt able to further reduce this. ny suggestions? theres a comment on the problem: On an interesting side note: the maximising principle underlying this problem generalises to almost-everywhere finite functions on sigma- finite measure spaces by a theorem of Hardy and Littlewood, see Theorem II.2.2 in C. Bennett, R. Sharpley, Interpolation of Operators ny use??? On Jun 25, 3:08 pm, prathimzn prathi...@gmail.com wrote: somebody answer how to reduce time *- - - - - WITH REGARDS, * * PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* On Sat, Jun 25, 2011 at 12:27 AM, prathimzn prathi...@gmail.com wrote: sorry my time is 0.2 and i use simple int array and sort function of algorithm... *- - - - - WITH REGARDS, * * PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* On Sat, Jun 25, 2011 at 12:04 AM, sunny agrawal sunny816.i...@gmail.comwrote: i am not sure about this but when i solved this problem using simple scanf, printf and sort function of algorithm library, my time was 0.08 so might be reading the values in character buffer and then parsing then in ints may help how did you implemented ? did you implemented your own sort funtion ? which input/output methods you used ? On Fri, Jun 24, 2011 at 11:23 PM, prathimzn prathi...@gmail.com wrote: http://www.spoj.pl/problems/FASHION/ i summit this question and my time is 0.02 as i used sorting and then multiply corresponding index value and sum them to get ans. but best time is 0.00 and 1.6M in C. can anyone tell me what is the bestalgoto solve this problem in 0.00 i.e. bestalgo * - - - - - WITH REGARDS, PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from
[algogeeks] Re: NEED ALGO IN TIME 0.00
@Dan: see, that is not the point. We are just looking for a better solution not just an algorithm which fetches us 0.00 time given the SPOJ conditions. Actually we are not worried about the compiler stuff because its all relative. Some other person on this SPOJ platform has submitted the code which runs in 0.00 sec so we want to think of the optimizations he might have employed which made his code to run faster. Another plus point is- the difference in the time might be covered by exploiting the SPOJ platform but in the meanwhile, we get to think, we might get a new approach to the problem or may be an inprovement in the algorithm being deployed. Thats the whole point. Its just relative. Someone has used some optimizations and got a better time so we are looking for it. People do write their functions to multiply, take modulus etc We skip the printf scanf calls instead switch to fread etc just to achieve the speedup. The purpose is not just to solve the problem but to solve it efficiently. Say we are just sorting an array, the way we do it, the memory we use etc. it all matters and SPOJ helps to measure the same relatively. The problem given might be trivial but the competition among thousands of coders trying to achieve the best time for the same builds the spirit and helps to explore new techniques, new algorithms. It all comes with an urge to make our code run faster. Regarding the 1.6M, I don't really kno what it means but if one is interested, you can look for it on the forums etc and you'll have your answer. Some addicted user must be knowing this stuff actually. At the end, its all about the competition after you discover the logic to solve the problem. When you don't have the urge to improve upon it, you won't discover something new, something efficient. These are my views. I am not an addicted SPOJ user. I might be mistaken but this is what i feel. Doom On Jun 26, 11:59 pm, Dan dant...@aol.com wrote: I found the problem statement on the web page link to be a bit weak. Nothing in the problem statement says that you must do anything other than read in two lines of integers and multiply them in pairs and sum the results ( ie. Dot Product ). People seem to think that you should sort the data first ( an assumption that is NOT in the problem statement ). Does that mean that you get the problem wrong if you don't sort the data first? Also... it looks as if you have to write the code in one of the listed languages. Is that true? You then submit your text code and it is compiled somewhere else? What compiler(s) is/are used and with what settings? This can affect both speed and memory (by large amounts). Also... exactly what does 1.6M for memory mean? For the moment, let's assume that you ARE supposed to sort the data first. Then... at best, all this test really does is check to see if you can read and sort data quickly. Multiplying pairs of numbers is trivial and I doubt many individuals are writing much code to speed up this simple Dot Product calculation. So... unless you write your own integer sorting algorithm that you are very proud of, what's the point of this test (other than it might be worthwhile as a homework assignment to new coders). Have I missed something? Dan :-) On Jun 24, 9:53 am, prathimzn prathi...@gmail.com wrote: http://www.spoj.pl/problems/FASHION/ i summit this question and my time is 0.02 as i used sorting and then multiply corresponding index value and sum them to get ans. but best time is 0.00 and 1.6M in C. can anyone tell me what is the best algo to solve this problem in 0.00 i.e. best algo * - - - - - WITH REGARDS, PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD*- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: puzzle
These type of solutions require to think in binary. First of all leave the last one because if we don't find a poisoned bottle in first 5 then it means the last one is poisoned. So 5 can be expressed using 3 bits. these 3 bits will correspond to mice... 1 indicates the mice drinks and 0 indicates it doesn't from the mentioned bottle. 1 - 001 2- 010 3- 011 4-100 5-101 the number on right is the bottle number for remaining five bottles. The binary number on right tells us which mouse drinks from which bottle. e.g. bottle no. 5 is taken by mice 1 and mice 3 whereas bottle no.3 is consumed by mice 2 and mice 3. Now after 14hrs, the mice which die will tell us which bottle was poisoned. Because the combination is unique. Doom On Jun 26, 11:10 pm, amit the cool amitthecoo...@gmail.com wrote: There are 6 beer bottle nd one is poisoned. we have mice who will die within 14 hrs after drinkin poisned beer. In 24 hrs we have to find poisoned beer bottle. How many no of mice we require to find out poisoned bottle. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: NEED ALGO IN TIME 0.00
see i got 0.07 sec as the time after using counting sort.. because the hotness scale is 0-10 so i used an array of 11 ints to count the no. of occurrences. But i m nt able to further reduce this. ny suggestions? theres a comment on the problem: On an interesting side note: the maximising principle underlying this problem generalises to almost-everywhere finite functions on sigma- finite measure spaces by a theorem of Hardy and Littlewood, see Theorem II.2.2 in C. Bennett, R. Sharpley, Interpolation of Operators ny use??? On Jun 25, 3:08 pm, prathimzn prathi...@gmail.com wrote: somebody answer how to reduce time *- - - - - WITH REGARDS, * * PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* On Sat, Jun 25, 2011 at 12:27 AM, prathimzn prathi...@gmail.com wrote: sorry my time is 0.2 and i use simple int array and sort function of algorithm... *- - - - - WITH REGARDS, * * PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* On Sat, Jun 25, 2011 at 12:04 AM, sunny agrawal sunny816.i...@gmail.comwrote: i am not sure about this but when i solved this problem using simple scanf, printf and sort function of algorithm library, my time was 0.08 so might be reading the values in character buffer and then parsing then in ints may help how did you implemented ? did you implemented your own sort funtion ? which input/output methods you used ? On Fri, Jun 24, 2011 at 11:23 PM, prathimzn prathi...@gmail.com wrote: http://www.spoj.pl/problems/FASHION/ i summit this question and my time is 0.02 as i used sorting and then multiply corresponding index value and sum them to get ans. but best time is 0.00 and 1.6M in C. can anyone tell me what is the best algo to solve this problem in 0.00 i.e. best algo * - - - - - WITH REGARDS, PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: NEED ALGO IN TIME 0.00
Ok, it seems like bucket sort is the best way to do it. I got 0.06 and to go further we need to use char buffer like the one Wladimir suggested. But still i have a doubt that would it be possible to reduce 0.06 to 0.00 using that buffer? I don't think there can be any possible improvement in logic. wat do u think? On Jun 26, 12:25 am, Dumanshu duman...@gmail.com wrote: see i got 0.07 sec as the time after using counting sort.. because the hotness scale is 0-10 so i used an array of 11 ints to count the no. of occurrences. But i m nt able to further reduce this. ny suggestions? theres a comment on the problem: On an interesting side note: the maximising principle underlying this problem generalises to almost-everywhere finite functions on sigma- finite measure spaces by a theorem of Hardy and Littlewood, see Theorem II.2.2 in C. Bennett, R. Sharpley, Interpolation of Operators ny use??? On Jun 25, 3:08 pm, prathimzn prathi...@gmail.com wrote: somebody answer how to reduce time *- - - - - WITH REGARDS, * * PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* On Sat, Jun 25, 2011 at 12:27 AM, prathimzn prathi...@gmail.com wrote: sorry my time is 0.2 and i use simple int array and sort function of algorithm... *- - - - - WITH REGARDS, * * PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* On Sat, Jun 25, 2011 at 12:04 AM, sunny agrawal sunny816.i...@gmail.comwrote: i am not sure about this but when i solved this problem using simple scanf, printf and sort function of algorithm library, my time was 0.08 so might be reading the values in character buffer and then parsing then in ints may help how did you implemented ? did you implemented your own sort funtion ? which input/output methods you used ? On Fri, Jun 24, 2011 at 11:23 PM, prathimzn prathi...@gmail.com wrote: http://www.spoj.pl/problems/FASHION/ i summit this question and my time is 0.02 as i used sorting and then multiply corresponding index value and sum them to get ans. but best time is 0.00 and 1.6M in C. can anyone tell me what is the best algo to solve this problem in 0.00 i.e. best algo * - - - - - WITH REGARDS, PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: NEED ALGO IN TIME 0.00
finally got it in 0.01 sec using all the optimizations i am aware of including what Wladimir had suggested. Now wat to do for 0.00??? heres my code- http://ideone.com/lsK8n please suggest if any further optimizations possible. On Jun 26, 2:21 am, Dumanshu duman...@gmail.com wrote: Ok, it seems like bucket sort is the best way to do it. I got 0.06 and to go further we need to use char buffer like the one Wladimir suggested. But still i have a doubt that would it be possible to reduce 0.06 to 0.00 using that buffer? I don't think there can be any possible improvement in logic. wat do u think? On Jun 26, 12:25 am, Dumanshu duman...@gmail.com wrote: see i got 0.07 sec as the time after using counting sort.. because the hotness scale is 0-10 so i used an array of 11 ints to count the no. of occurrences. But i m nt able to further reduce this. ny suggestions? theres a comment on the problem: On an interesting side note: the maximising principle underlying this problem generalises to almost-everywhere finite functions on sigma- finite measure spaces by a theorem of Hardy and Littlewood, see Theorem II.2.2 in C. Bennett, R. Sharpley, Interpolation of Operators ny use??? On Jun 25, 3:08 pm, prathimzn prathi...@gmail.com wrote: somebody answer how to reduce time *- - - - - WITH REGARDS, * * PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* On Sat, Jun 25, 2011 at 12:27 AM, prathimzn prathi...@gmail.com wrote: sorry my time is 0.2 and i use simple int array and sort function of algorithm... *- - - - - WITH REGARDS, * * PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* On Sat, Jun 25, 2011 at 12:04 AM, sunny agrawal sunny816.i...@gmail.comwrote: i am not sure about this but when i solved this problem using simple scanf, printf and sort function of algorithm library, my time was 0.08 so might be reading the values in character buffer and then parsing then in ints may help how did you implemented ? did you implemented your own sort funtion ? which input/output methods you used ? On Fri, Jun 24, 2011 at 11:23 PM, prathimzn prathi...@gmail.com wrote: http://www.spoj.pl/problems/FASHION/ i summit this question and my time is 0.02 as i used sorting and then multiply corresponding index value and sum them to get ans. but best time is 0.00 and 1.6M in C. can anyone tell me what is the best algo to solve this problem in 0.00 i.e. best algo * - - - - - WITH REGARDS, PRAMENDRA RATHI * ** *B.TECH 2ND YEAR* *COMPUTER SCIENCE AND ENGINEERING* *NIT ALLAHABAD* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Google Question
@Piyush: could u plz post the link to the same? On Jun 22, 2:15 pm, Piyush Sinha ecstasy.piy...@gmail.com wrote: This question has been discussed over here once...It was concluded that this can be solved in O(n) if we know there is a fixed range up to which the elements keep on increasing and decreasing..for example in an array of 12 elements, we know 3 elements keep on increasing monotonically, then 3 elements keep on decreasing monotonically and so on On 6/22/11, chirag ahuja sparkle.chi...@gmail.com wrote: Given an array of size n wherein elements keep on increasing monotonically upto a certain location after which they keep on decreasing monotonically, then again keep on increasing, then decreasing again and so on. Sort the array in O(n) and O(1). I didn't understand the question, any array of n elements will be like this except when first there is a decrese from index 0 to a higher index. Any ideas about how to solve it in given constraints?? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- *Piyush Sinha* *IIIT, Allahabad* *+91-8792136657* *+91-7483122727* *https://www.facebook.com/profile.php?id=10655377926*- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: finding vlaue of nCr
Dynamic programming using memoization is the best one i think. On Jun 21, 3:17 am, PRAMENDRA RATHi rathi prathi...@gmail.com wrote: what is the shortest algo to find the value of nCr if both n,r100... - PRAMENDRA RATHI NIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: finding vlaue of nCr
@Sunny: the value can still overflow because Using the while loop u r dividing the res value whenever possible. so the while checks if the current j divides res or not. What if current res value is not divisible by that particular j ... it will multiply by i for next res value and it will keep doing this until u get a res value divisible by that specific current j. So in the meantime, when the res value is increasing just because j doesn't divide it, it can overflow. On Jun 21, 4:12 pm, sunny agrawal sunny816.i...@gmail.com wrote: i am doing the same thing without using array long long int res = 1; int j = 2; for(int i = n-k+1; i = n; i++){ res *= (long long int)i; while(j = k res%j == 0){ res/=j; j++; } } On Tue, Jun 21, 2011 at 4:25 PM, kartik sachan kartik.sac...@gmail.com wrote: @ sunny i am not getting ur apporach but i am thinking like this.. taking an array from and intilize it to n to n-r a[1000]; int k=0; for(int i=n;i=n-r;i--) a[k++]=i; for(int y=n-r;y1;y--) for(j=0;jk;j++) if(a[j]%y==0) {a[j]=a[j]/y;break;} for example we take 7c3 so first array is intilize to {7,6,5,4} then we check divisibilty by {3,2} so 6 is divisible by 3 so put 6/3 back in array 2 so finally 7*2*5*2; -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: google interview c testing
new and delete are functions available in C++ and not in C. m i right? On Jun 17, 2:34 pm, rohit rajuljain...@gmail.com wrote: how to free memory allocated to an array with new function? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: How to use asm in spoj
Yes SPOJ uses the nasm assembler. On Jun 18, 7:02 am, saurabh singh saurab...@gmail.com wrote: I am using standard gcc 4.3.2 and the code does not requires any flag to be required.I also checked the alias if gcc has been aliased to be used with some option,but that was not the case.My operating system is ubuntu.The error I get is CONT1D and D1ONE already defined. I wonder if spoj has a modified gcc which uses the nasm assembler instead of the standard assembler packed with gcc?I have put the problem on spoj forum few days back but no one has replied since On Sat, Jun 18, 2011 at 3:06 AM, DK divyekap...@gmail.com wrote: What compiler are you using? Version, compile options etc. -- DK -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/Wf2PvNNjTikJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Mutex
@DK: So u mean to say that Mutex and binary semaphores provide completely same functionality. Right? Say u have a resource R and u can provide access to this particular only one at a time (lets say multiple threads are there). Now we can use mutex. One by one the threads will lock it and will use the resource and then unlock it for other threads. We can use binary semaphore. One thread will signal it, value will be 0 and use the resource. Later, it will signal it to value 1 so now other thread can do the same thing. So here we dont have any difference between mutex and binary semaphore. We can use either. Now plz give me some examples where mutex is preferred over binary semaphore and vice-versa. Thanks Dumanshu On Jun 18, 1:58 am, DK divyekap...@gmail.com wrote: @Dumanshu/@Ankit: Of course mutexes can be made to work between processes (it's an implementation detail). But the *concept* of a mutex is Owner + (Lock Key) pair. By adding the concept of Owner to a lock, we can ensure that only the person who locked the lock can open it. This *guarantees* mutual exclusion (which is unlike a semaphore). A semaphore is a signalling mechanism between threads/processes that simply indicates an event that has occured asynchronously to the process/thread's flow of control (for processes/threads other than the event generating process of-course). It is often used to indicate that cooperative progress may be possible on resources under contention or synchronization between states may be required. Any cooperating process/thread can modify the value of a semaphore which is not true of a mutex. Note: Mutex and Semaphore are essentially concepts. Their implementations can vary in different systems and have different implementation constraints but the theory is clear. The two are different and serve different purposes though they may share implementation details. -- DK http://twitter.com/divyekapoorhttp://www.divye.in -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Finding the min gap in 3 arrays
@Ashish: In ur example you have found a min distance for a and c. Now to proceed because we have to find out min distance for two out of three arrays with the third array in between i.e. u need to find occurrence of a c b or a b c or b a c or ... in the merged array. Now quite possible these occurences might not be continuous so how are u going to proceed? Your order of m+n+p is fine for merging but to find the occurrence of a,b,c it would go like O((size of merged array)^3). so 3 for loops. for(i=0;isizeofmergedlist;i++) //for each element { now say a[i] belongs to array a for(j=i+1;jsizeofmergedlist;j++) { keep goin until u find occurrence of some (other array other than a) lets say we have b now for(k=j+1;ksizeofmergedlist;k++) keep going until u find some occurrence of c } now min_dist = max mod(a[i]-a[j],a[i]-a[k],a[k]-a[j]); } So for every element we are updating the min_dist. Is this what you are trying to do here? sorry i dont get your algo. Thanks Dumanshu On Jun 18, 12:06 am, Ashish Goel ashg...@gmail.com wrote: say the arrays are a 6,7,9 b 3,4,5 c 1,2,8 the merged array would be 1c 2c 3a 4b 5b 6a 7a 8c 9a 1c,2c cant be compared as they are from same array..next is 3a this implies 3-2 =1 is min distance P.S: you can merge these in O(m+n+p) [merge from bottom as they are already sorted] Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sat, Jun 18, 2011 at 12:24 AM, Dumanshu duman...@gmail.com wrote: @Ashish: could u plz explain ur algo in detail. walk over the merged list to get adjacent min distance and different tags this would be of the order O(m*n) say we merge A1 A2 of size m and n respectively. Also, now how do u go ahead with the 3rd array? didn't get ur solution. Harshal's solution looks fine. ny bugs? On Jun 17, 9:13 pm, Ashish Goel ashg...@gmail.com wrote: merge two and if required third array keeping array tag with the elements walk over the merged list and see adjacent distance which is minimum with the condition that the tage of the adjacent elements are different Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Fri, Jun 17, 2011 at 9:36 PM, Dumanshu duman...@gmail.com wrote: U have got 3 sorted arrays A1 A2 and A3 having m n and p elements respectively. A gap of 3 arrays is defined to be max distance between 3 nos if they are put on a no line say u pick three 2 12 and 7 then the gap is 10. Now u have to find an efficient way of chosing 3 nos from these 3 seperate arrays (A1, A2, A3) such that their gap is minimum. Of course if a num say 2 occurs in all 3 then gap is 0!!! -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.-Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: How to use asm in spoj
It does matter. suppose u write the c code and the compiler generates assembly level code later u get machine code which runs. here in this process many optimizations can be employed which are not done by gcc. So including asm code in ur c code can actually make ur code to run very fast provided u use the optimizations :) On Jun 18, 2:53 pm, ADITYA KUMAR aditya...@gmail.com wrote: whether u write your code in assembly or c or even in Machine code it will take the same time.. because after-all ,on system ...your executable runs not ur code and which is not in c not in assembly but in machine code On Thu, Jun 16, 2011 at 9:11 AM, saurabh singh saurab...@gmail.com wrote: Hello I have been thinking if I can use asm in c programs to enhance my time... So to give it a try i triedhttp://www.spoj.pl/problems/STREETR/ with the following code: #includestdio.h int gcd( int a, int b ) { int result ; __asm__ __volatile__ ( movl %1, %%eax; movl %2, %%ebx; CONT1D: cmpl $0, %%ebx; je D1ONE; xorl %%edx, %%edx; idivl %%ebx; movl %%ebx, %%eax; movl %%edx, %%ebx; jmp CONT1D; D1ONE: movl %%eax, %0; : =g (result) : g (a), g (b) ); return result ; } int main() { int n,i,min,ans=0; scanf(%d,n); int arr[n],ard[n]; ard[0]=1; if(n0) scanf(%d,arr[0]); for(i=1;in;i++) { scanf(%d,arr[i]); ard[i]=arr[i]-arr[i-1]; if(i==1) min=ard[i]; else min=gcd(min,ard[i]); } for(i=1;in;i++) { ans+=((ard[i]/min)-1); } printf(%d,ans); return 0; } Its according to the i386 arch. it ran fine on my computer but giving complilation error in SPOJ judge. I am yet a novice with asm's so dont know if I made sense at all.But please do help with this, -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Regards Aditya Kumar B-tech 3rd year Computer Science Engg. MNNIT, Allahabad.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Finding the min gap in 3 arrays
U have got 3 sorted arrays A1 A2 and A3 having m n and p elements respectively. A gap of 3 arrays is defined to be max distance between 3 nos if they are put on a no line say u pick three 2 12 and 7 then the gap is 10. Now u have to find an efficient way of chosing 3 nos from these 3 seperate arrays (A1, A2, A3) such that their gap is minimum. Of course if a num say 2 occurs in all 3 then gap is 0!!! -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.