Re: [algogeeks] Facebook Intern F2F Interview

2011-07-28 Thread Nikhil Jindal
Generate a random number from 1 to 100. If it is less than or equal to x, return true, else return false. This will ensure that ur returning true with x/100 probability. Cheers Nikhil Jindal On Thu, Jul 28, 2011 at 4:21 PM, KK kunalkapadi...@gmail.com wrote: bool foo(int x) // Implement

Re: [algogeeks] Google Interview Question

2011-06-05 Thread Nikhil Jindal
Anshu here has given a Perfect soln! Sunny's code is also correct but is a bit less efficient than anshu's. Cheers Nikhil Jindal http://sites.google.com/site/aboutnikhiljindal/ On Fri, May 27, 2011 at 9:11 PM, anshu mishra anshumishra6...@gmail.comwrote: @all go through this code

Re: [algogeeks] Google Interview Question

2011-06-05 Thread Nikhil Jindal
, not just the first digit but the complete number should be appended. Hence, to get correct result, you should change the array to : {6868, 6867}. Hope this makes things clear for you. Cheers Nikhil Jindal http://sites.google.com/site/aboutnikhiljindal/ On Mon, May 30, 2011 at 3:28 PM, Bhavesh agrawal

Re: [algogeeks] Google Interview Question

2011-06-05 Thread Nikhil Jindal
@Naveen: Here's a counter case: 162, 16 The next digit(2) is not greater than the last equal digit(6), still 162 comes before 16. Here, as is done in ashu's algo, the next digit (2) should be compared with first digit(1) and not the last equal digit(6). Cheers Nikhil Jindal http

Re: [algogeeks] Re: Facebook Interview Question....Heavy Number

2011-04-06 Thread Nikhil Jindal
Ok. Here's a possible O(n) solution. Assuming last digit of a is 0. for(n=a;n=b;n+=10) { Calculate the sum of digits, leaving the last digit. Find the minimum value of last digit for it to be a heavy number. Increment count by 10-that number. } So here, complexity will be: O(n/10*(d-1)) where, d

Re: [algogeeks] Crazy Question, Wants Creative Answer

2011-03-15 Thread Nikhil Jindal
. Nikhil Jindal https://sites.google.com/site/aboutnikhiljindal/ On Tue, Mar 15, 2011 at 4:17 PM, bittu shashank7andr...@gmail.com wrote: U r watching an i cricket match.Suddenly the tv goes blank. Write the steps u ll take to find the fault purpose of this question is not to spamming but to taste

Re: [algogeeks] SPoj maximum sum subseuence

2011-03-15 Thread Nikhil Jindal
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. This shld help. Cheers Nikhil Jindal https://sites.google.com/site/aboutnikhiljindal/ Please

Re: [algogeeks] [brain teaser ] 28february

2011-03-01 Thread Nikhil Jindal
He tells the truth on tuesday. First day is sunday. Nice question. Took me some time to get it right. On Mon, Feb 28, 2011 at 6:47 PM, nidhi jain nidhi.jain311...@gmail.comwrote: Sunday,Saturday -- You received this message because you are subscribed to the Google Groups Algorithm Geeks

Re: [algogeeks] Puzzle For Puzzled Minds - Robots

2011-02-22 Thread Nikhil Jindal
way, it doubles its speed to 2 moves per 3 second. Hence, eventually both the robots will clash. Cheers Nikhil Jindal http://www.fundoonick.blogspot.com/ On Thu, Feb 17, 2011 at 12:23 AM, bittu shashank7andr...@gmail.com wrote: Two robots are placed at different points on a straight line

Re: [algogeeks] Re:

2011-02-14 Thread Nikhil Jindal
First Question: Nt sure but shldnt t1 be greater than t2? Second: Since, Q is a subset of P. P intersection Q would be Q itself. Would be great if you can share some more questions On Mon, Feb 14, 2011 at 7:52 PM, sankalp srivastava richi.sankalp1...@gmail.com wrote: First question mode

Re: [algogeeks] Delightfully Puzzle

2011-02-04 Thread Nikhil Jindal
The puzzle has recently been discussed in another thread. On Fri, Feb 4, 2011 at 2:20 PM, bittu shashank7andr...@gmail.com wrote: A blind man is handed a deck of 52 cards and told that exactly 10 of these cards are facing up. How can he divide the cards into two piles, not necessarily of

Re: [algogeeks] Re: Google Question

2011-01-28 Thread Nikhil Jindal
*dp(i-3)) { buff = dp(i-3);}* *}* @saikat: for n=10, this gives dp(10) = 20 :D An O(n) soln. Cheers Nikhil Jindal Delhi College of Engineering (DCE), Delhi. On Wed, Jan 19, 2011 at 10:05 PM, nishaanth nishaant...@gmail.com wrote: How about the following dynamic programming solution. Let dp[i

Re: [algogeeks] Modular Arithmetic

2011-01-13 Thread Nikhil Jindal
(a/b)mod m = (amodm)*(Bmodm) where B is the multiplicative inverse of b i.e (b*B)modM = 1 or B = 1/b Look into the wiki page of Multiplicative inverse for more. Hope this helps Cheers Nikhil Jindal On Wed, Jan 12, 2011 at 7:06 AM, mittal mohitm.1...@gmail.com wrote: Somehelp with (a/b)modm

Re: [algogeeks] Re: Modular Arithmetic

2011-01-13 Thread Nikhil Jindal
On Thu, Jan 13, 2011 at 12:06 AM, Aviral Gupta aviral@gmail.com wrote: we can do it when hcf(b,m)=1 , in that case find inverse of b by extended euclidean mod m and then multiply it by a Yes. And when m is prime, B(mulitplicative inverse of b) = b^(m-2) As b^(m-1)mod m = 1 if m is prime.

Re: [algogeeks] Re: Amazon Analytical Puzzle

2011-01-13 Thread Nikhil Jindal
Cut each slice into 8 equal pieces. Total 24 pieces. One part consists of 3 pieces. Total 8 parts. On Wed, Jan 12, 2011 at 6:14 PM, bittu shashank7andr...@gmail.com wrote: 2nd puzzle You have a birthday cake and have exactly 3 slices to cut it into 8 equal pieces. How do you do it? Thanks

[algogeeks] Number of ways for generating n pairs of valid parenthesis

2010-09-30 Thread Nikhil Jindal
()()(), ((())), (()()), ()(()), (())() Cheers Nikhil Jindal Please access the attached hyperlink for an important electronic communications disclaimer: http://dce.edu/web/Sections/Standalone/Email_Disclaimer.php -- You received this message because you are subscribed to the Google Groups Algorithm

Re: [algogeeks] search a 2d sorted array in less than O(n)

2010-09-26 Thread Nikhil Jindal
On Tue, Sep 21, 2010 at 6:05 PM, saurabh singh saurabh.n...@gmail.comwrote: solution 1: use concept of quad-tree and do binary search in that tree solution 2: do binary search on major diagonal. ultimately u will narrow down to search for element in 2 rows. in these two rows again do

Re: [algogeeks] search a 2d sorted array in less than O(n)

2010-09-26 Thread Nikhil Jindal
be in the present column after(below) the element we are on. At max, 3n moves = O(n). On Sun, Sep 26, 2010 at 7:34 PM, Nikhil Jindal fundoon...@yahoo.co.inwrote: On Tue, Sep 21, 2010 at 6:05 PM, saurabh singh saurabh.n...@gmail.comwrote: solution 1: use concept of quad-tree and do binary search

Re: [algogeeks] search a 2d sorted array in less than O(n)

2010-09-26 Thread Nikhil Jindal
, int item) { if (i0 || j0 || in-1 || jn-1) { printf(Not Found\n); return; } if( a[i][j] == item) printf(Found: %d %d\n, i, j); elseif( a[i][j] item) 2dsearch(i, j-1, item); else 2dsearch(i+1, j, item); } Call this function as 2dsearch(0, n-1, item); Cheers Nikhil Jindal Delhi

Re: [algogeeks] Re: Adobe Question

2010-09-25 Thread Nikhil Jindal
The answer would be: (log1+1) + (log2+1) + (log3+1) + (log4+1) + ... + (log(n-1)+1) which is equal to: (log1+log2+log3+...+log(n-1)) + (n-1) == *log((n-1)!) + (n-1)* where, log everywhere is assumed to be in base 2 *This according to me will be the final answer!* * * *Cheers* *Nikhil Jindal

Re: [algogeeks] Re: Yahoo Question

2010-09-17 Thread Nikhil Jindal
@vikas: Total number of elements are not n*k. Total number of elements are n, which are divided into k lists. @Rahul Singal: +1 for ur answer. On Fri, Sep 17, 2010 at 12:58 PM, vikas kumar vikas.kumar...@gmail.comwrote: @Bittu I am confused about one point you need to process atleast n*k

Re: [algogeeks] Re: Amazon Question

2010-09-17 Thread Nikhil Jindal
Keep an augmented balanced BST. Augmented data: number of elements in the right and left subtrees . Insertion: logn Find Median: logn Cheers Nikhil Jindal Delhi College of Engineering On Fri, Sep 17, 2010 at 12:09 PM, vikas kumar vikas.kumar...@gmail.comwrote: struct list { median

Re: [algogeeks] Re: Subsequence

2010-09-01 Thread Nikhil Jindal
Very Nice soln Dhritiman. The solution to the standard LCS problem only needs O(n^2) time and O(n) space. And you have very intelligently solved its variation also in the same time and space complexity. I fell this is the correct solution. Nikhil Jindal On Tue, Aug 31, 2010 at 2:13 AM

Re: [algogeeks] Re: Sorting when n-√n numbers are already sorted

2010-08-28 Thread Nikhil Jindal
@Wan: Storing the elements as link list rather than array requires additional space. If we are taking extra O(n) space, then the usual approach of merging two sorted arrays in O(n) time and memory will suffice. On Fri, Aug 27, 2010 at 5:18 PM, Yan Wang wangyanadam1...@gmail.com wrote: Also,

Re: [algogeeks] Sorting when n-√n numbers are alre ady sorted

2010-08-26 Thread Nikhil Jindal
Hi Sourav, I will first inplace sort the last √n elements in O(n) and then merge the two sorted arrays in O(n). The only problem: O(n) merging will not be inplace. On Thu, Aug 26, 2010 at 5:25 PM, sourav souravs...@gmail.com wrote: Let A[1..n] be an array such that the first (n − √n) elements

Re: [algogeeks] Counting number of rectangles

2010-08-23 Thread Nikhil Jindal
Here's my try: The following function returns the rectangle number given the dimensions of the rectangle. int FindRectangleNumber(int row, int colm) { if (row == colm) return row; if (row == 1) return colm; if (row%2 == 0 colm%2 == 0) return 2*FindRectangleNumber(row/2, colm/2); if

Re: [algogeeks] Longest Palindromic Substring

2010-08-22 Thread Nikhil Jindal
://mc/compose?to=cho...@gmail.com wrote: I definitely meant a suffix Tree and not a trie. Apologize for that. :) On Fri, Aug 20, 2010 at 8:47 PM, Nikhil Jindal fundoon...@yahoo.co.inhttp://mc/compose?to=fundoon...@yahoo.co.in wrote: @chonku As i understand, a trie is used when we have

Re: [algogeeks] Longest Palindromic Substring

2010-08-21 Thread Nikhil Jindal
; } } tempMax-=1; } return 0; } On Sat, Aug 21, 2010 at 12:12 PM, Chonku cho...@gmail.com wrote: I definitely meant a suffix Tree and not a trie. Apologize for that. :) On Fri, Aug 20, 2010 at 8:47 PM, Nikhil Jindal fundoon

Re: [algogeeks] Re: Longest Common Subsequence

2010-08-21 Thread Nikhil Jindal
longest common substring in the given string. If i get it right. You need two strings to find a common subsequence. We use DP for it. 2010/8/18 ♪ ѕяiηivαѕαη ♪ 21.sr...@gmail.com Example: If my sequence is ABC..the longest common subsequence is AC,BC,AB. It is a very common problem... On

Re: [algogeeks] Re: Array Problem

2010-08-21 Thread Nikhil Jindal
dave_and_da...@juno.com wrote: @Nikhil Jindal: What you say is true for 2 numbers, but not for more than 2. 1 + 6 + 6 = 2 + 2 + 9 = 13, and 1 * 6 * 6 = 2 * 2 * 9 = 36. Dave On Aug 19, 6:00 am, Nikhil Jindal fundoon...@yahoo.co.in wrote: Nikhil's algo is correct

Re: [algogeeks] Longest Palindromic Substring

2010-08-20 Thread Nikhil Jindal
: Can we use a trie here. Make first pass from left to right and construct the trie. Make second pass from right to left and look for the trie branch with maximum nodes that match the characters. On Thu, Aug 19, 2010 at 7:46 PM, Nikhil Jindal fundoon...@yahoo.co.inwrote: Hi All, Givan a string

[algogeeks] Longest Palindromic Substring

2010-08-19 Thread Nikhil Jindal
Hi All, Givan a string, you have to find the longest palindromic substring. For ex: Longest Palindromic substring for abclevelabc is level. What is the most optimised solution possible? Please access the attached hyperlink for an important electronic communications disclaimer:

Re: [algogeeks] Re: Array Problem

2010-08-19 Thread Nikhil Jindal
Nikhil's algo is correct if the following is always true: Given: x + y = S, x * y = M and x' + y' = S', x' * y' = M' If: S' = S and M' = M, then x = x' and y = y' i.e for given sum and product, the elements are unique. On Thu, Aug 19, 2010 at 12:54 PM, Nikhil Agarwal

Re: [algogeeks] Permutation of Array.

2010-08-19 Thread Nikhil Jindal
The critical thing here is how to define your comparator function to be used for sorting. Sorting using the normal comparator function will return the following: 31 33 55 312 Using insertion sort O(n^2) such that the resultant concatenation is smallest, should do it. The steps for [55,31,312,33]

Re: [algogeeks] 0's and 1's yet again!!!

2010-08-19 Thread Nikhil Jindal
@ram: This doesnt work for: arr[] = {1,0,0,0} Here, then number of 1's and 0's are not same. So the array should be left untouched. On Thu, Aug 19, 2010 at 7:02 PM, ram das ramnaraya...@gmail.com wrote: { int odd=1,even=0; while(odd = size even =size) { if (

Re: [algogeeks] Re: Shuffling a deck of cards

2010-08-18 Thread Nikhil Jindal
Use Knuth Shuffle algo. On Sun, Aug 15, 2010 at 8:34 PM, Rahul Singhal nitk.ra...@gmail.com wrote: Let X1, X2…. XN (In this case N=52) be the set of N numbers to be shuffled. 1. Set j to N 2. Generate a random number R. (uniformly distributed between 0 and 1) 3. Set k to (jR+1). k

Re: [algogeeks]

2010-08-17 Thread Nikhil Jindal
Use hash maps... On Mon, Aug 16, 2010 at 10:06 PM, ashita dadlani ash@gmail.com wrote: You have a string say foobarfoo in which fo and oo aree repeated twice.You have to find all such repeated pairs in O(n) time,The string can only have alphanumeric elements in it. -- You received this

Re: [algogeeks] Dynamic Programming Problem on Strings

2010-08-08 Thread Nikhil Jindal
. Hope this helps Cheers Nikhil Jindal Delhi College of Engineering On Thu, Aug 5, 2010 at 9:56 PM, Ashish Goel ashg...@gmail.com wrote: can u explain how is this number reached at? (2n)!/((n+1)!(n!)) Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652

Re: [algogeeks] Re: amezan interview.........

2010-08-07 Thread Nikhil Jindal
) { swap(a[k],a[n+k/2]); } else { swap(a[k],a[n+k/2-1]); } } *Done!* Nikhil Jindal Delhi College of Engineering On Sat, Aug 7, 2010 at 10:17 AM, Dave dave_and_da...@juno.com wrote: Here's a solution that I am pretty sure is less than O(n^2). The data are moved only once, but timing

Re: [algogeeks] Longest Bitstring with equal 0s and 1s

2010-08-01 Thread Nikhil Jindal
Here's a possible O(n) soln: for all i, I calculate a[i].diff as number of zeroes - number of ones ones from a[0] to a[i]. I do this in O(n). Also, I make a list of all the indexes that have a difference d(for all possible d, which is n). Now, for it to be possible that the number of ones and

Re: [algogeeks] minimum window containing charecters

2010-08-01 Thread Nikhil Jindal
can be missing can be found if a row in this 2-d array remains unmodified Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sat, Jul 31, 2010 at 10:22 PM, Nikhil Jindal fundoon...@yahoo.co.in wrote: At the moment, I can only think of an O

Re: [algogeeks] minimum window containing charecters

2010-07-31 Thread Nikhil Jindal
At the moment, I can only think of an O(n^3) algo. Maybe if you can find a hash function which computes the hash value depending on the unique characters the string contains, you can reduce it to O(n^2). On Sat, Jul 31, 2010 at 7:09 PM, srikanth sg srikanthini...@gmail.comwrote: given two

Re: [algogeeks] Merging Companies Problem

2010-07-31 Thread Nikhil Jindal
For merging n companies, F(n) = n*F(n-1) for n 3. Base case, F(3) = 3. On Sat, Jul 31, 2010 at 6:59 PM, sourav souravs...@gmail.com wrote: Suppose we start with n companies that eventually merge into one big company. How many different ways are there for them to merge? With three companies

Re: [algogeeks] Re: What is the time complexity of multiplying two n-digit numbers base b

2010-07-31 Thread Nikhil Jindal
Multiplying two n digit numbers, where multiplication of two 1 digit numbers is O(1), is : O(n^2). On Sat, Jul 31, 2010 at 9:12 PM, Dave dave_and_da...@juno.com wrote: If by repeated addition method, you mean m + m + m + ... + m (where m occurs k times) for forming the product k*m, then the

Re: [algogeeks] Amazon Placement Question

2010-07-31 Thread Nikhil Jindal
@Ram Kumar: Yes. Simple and affective. Just at each node: node-left-side=node-right node-right-side=node-side-left i.e at each node you are setting the side of each of your child. Go on and just do it for all nodes. Done. On Sat, Jul 31, 2010 at 9:39 AM, Ram Kumar

Re: [algogeeks] Re: impossible microsoft puzzle

2010-07-04 Thread Nikhil Jindal
guess the same number every time. For the given puzzle, all men guess the same number and at least one of them will be correct. :) Nikhil Jindal Department of Computer Engineering Delhi College of Engineering http://www.dce.edu, Delhi My Blog: http://fundoonick.blogspot.com My LinkedIn Profile: http

Re: [algogeeks] 0 and 1 again :)

2010-07-04 Thread Nikhil Jindal
of length 4). PS: I am assuming by maximum subsequence, you meant longest. Nikhil Jindal On Sun, Jul 4, 2010 at 3:21 PM, jalaj jaiswal jalaj.jaiswa...@gmail.comwrote: You are given an array ' containing 0s and 1s. Find O(n) time and O(1) space algorithm to find the maximum sub sequence which has equal