[algogeeks] Re: O(n)

2010-06-27 Thread Jagadish M
In the general case, we can reduce Element-Distinctness(ED) problem to this problem. Since ED has a lower bound of Omega(n log n), we cannot get an O(n) algorithm for this problem. http://en.wikipedia.org/wiki/Element_distinctness_problem -Jagadish http://www.cse.iitb.ac.in/~jagadish On Jun 2

[algogeeks] Re: There is an array of odd and even numbers. Now, sort them in such a way that the top portion of the array contains odd numbers, bottom portion contains even numbers

2010-06-23 Thread Jagadish M
>Why not just change the definition of when one number is bigger than another >and do normal sort ? >I guess that is better and simpler. Normal sort takes O(n log n), while Anurag's algo is O(n). Regards, Jagadish http://www.cse.iitb.ac.in/~jagadish On Jun 20, 2:18 pm, Rohit Saraf wrote:

[algogeeks] Re: another google telephone interview question

2010-05-23 Thread Jagadish M
> Further to my previous post, one question. > Can the median be found in O(n) time without using extra memory? > I am not familiar with the algorithms that find the median though I > know the > median can be found in O(n) time. > This is important! I don't see how the standard algorithm for media

[algogeeks] Re: another google telephone interview question

2010-05-20 Thread Jagadish M
> 3) Whenever a number repeats instead of storing the count store modify the > number to (number + ROOT Value) ie for 2 which is repeated twice 2 + 3 +3  = > 8 instead of 2:3 as you give in your example. > > 4) Since in a heap no number can be greater than root value whenever a > number greater tha

[algogeeks] Re: another google telephone interview question

2010-05-18 Thread Jagadish M
:23:2 and so on... Since, there are only k distinct keys the heap size will at most be k; so each search/insert/increment operation takes O(log k) time. Jagadish http://www.cse.iitb.ac.in/~jagadish > > On 2010-5-17 22:38, Jagadish M wrote: > > > > > The best algorithm

[algogeeks] Re: partion of array

2010-05-17 Thread Jagadish M
On May 17, 5:36 pm, Rohit Saraf wrote: > @divya :  descending order sorting works. BRILLIANT !! Not really. Try this input: 8 6 5 5 5 1 The best partition is [8 6 1] [5 5 5] but Divya's algorithm gives [8 5 1] [6 5 5]. -Jagadish http://www.cse.iitb.ac.in/~jagadish > On 5/17/10, divya

[algogeeks] Re: another google telephone interview question

2010-05-17 Thread Jagadish M
The best algorithm I can think of is to maintain a heap of k elements. Takes O(n log k) time. Is anything told about the values of the keys? If the keys have values less than N, then it is possible to do what you say, i.e sort them in place. -Jagadish http://www.cse.iitb.ac.in/~jagadish On May

[algogeeks] Re: Zig-zag subsequence

2009-05-13 Thread Jagadish M
> > > Try dynamic programming. Complexity of the algorithm would be O(n^3). > Won't it just be O(n^2)? Suppose A is the input. Let F(i,+) denote the maximum sum you can accumulate with the sequence ending at A[i] and the last difference being positive. Define F(i,-) accordingly. Computing each F