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

2010-09-21 Thread jagadish
Hi all, Given a 2d array which is sorted row wise and column wise as well, find a specific element in it in LESS THAN O(n). PS: an O(n) solution would involve skipping a column or a row each time from the search and moving accordingly. Solution less than O(n) is desirable! -- You received this me

[algogeeks] Re: point lies inside or outside of triangle..

2010-09-21 Thread jagadish
Here is the Simplest working solution :) bool check(int x[],int y[],int n) { c=0; for(int i=0;iy) != (y[j]>y)) && (x-x[i]/x[j]-x[i] < y-y[i]/y[j]-y[i]) c=!c; } return c; } On Sep 20, 7:46 pm, umesh kewat wrote: > Initially we have given  three point A , B, C in plane represent three nodes > of

[algogeeks] Re: Finding max for all positions of a moving window

2010-09-21 Thread jagadish
@Minotauraus: ur approach is flawed! the BEST solution would be to use a maxheap as navin said! :) On Sep 21, 1:55 pm, Naveen Agrawal wrote: > @Minotauraus > > Your algo is wrong > Consider this case: > 1    2  3   4   5   6  7   8   9  10 11 12 13  14 15 16  17 18 19 20 > 99 98 97 96 95 94 93 92

[algogeeks] recursion to remove duplicate characters in a string

2010-09-18 Thread jagadish
You are given a string which is sorted.. Write a recursive function to remove the duplicate characters in the string. eg: potatoeos output: potaes NO extraspace like hash/ bitmaps.. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to t

[algogeeks] Re: number of inversion pairs

2010-09-18 Thread jagadish
Hi Raj, I think there is an nlogn solution to this problem which can be done by modifying merge sort.. the key lies in the merge routine, when the left array elements are greater than the right array, the right array elements are copied to the tmp array .. hence , this adds (n-i) to the inversion

[algogeeks] Re: Given an array, find out if there exists a pair of nos. that adds up to a sum 'x'

2010-09-10 Thread jagadish
Hi, O(n) solution is possible if the array is presorted! Else i think it is NOT. 1.Have two ptrs (one from first and other to track the array from last) 2.s=a[left]+a[right] 3.if(ssum) right--; else print "sum found" On Sep 10, 10:19 pm, bittu wrote: > Q Given an array, find out if there exis

[algogeeks] Re: majority element in array in O(n)

2010-09-10 Thread jagadish
Hi Bittu, Your algorithm uses Linear extra space. which is proportional to the input used. There is a better algorithm which is linear in time and does alot better . int Majoritycount(int [] A) { int ctr=1; //counter is a variable to indicate the relative importance of the element. as successive

[algogeeks] Re: Simple Factorial Problem

2010-09-04 Thread jagadish
Further more you are not printing the factorial itself! On Sep 4, 7:02 pm, jagadish wrote: > @Nuan: > Hi Nuan, > > The problem states that > > "An integer t, 1<=t<=100, denoting the number of testcases, followed > by t lines, each containing a single integer n,

[algogeeks] Re: Simple Factorial Problem

2010-09-04 Thread jagadish
@Nuan: Hi Nuan, The problem states that "An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100." N lies between 1 to 100.. Such a naive approach like yours would fail for larger numbers! That is why you are getting a wrong

[algogeeks] Re: Amazon intern Question

2010-09-01 Thread jagadish
HI Arun, This is the edit distance problem which can be solved using DP. Calculate the cost for each product on the fly and return the top products with the least edit cost! On Sep 1, 5:15 pm, Arun wrote: > You are given the amazon.com database which consists of names of > millions of products.

[algogeeks] Re: Help with Increment Operators in C!

2010-08-29 Thread jagadish
@Dave: Thanks alot for enlightening us! @Manju: ya.. you are right. The same was stated by me in the my prev reply! :-) On Aug 29, 10:50 pm, Manjunath Manohar wrote: > it is compiler dependant da..the evaluation of this kind of expressions -- You received this message because you are subscribe

[algogeeks] Re: Amazon interview Question (Array yet again!)

2010-08-29 Thread jagadish
@Rahul Patil: I ran your code on some basic test cases and i found it to be correct! Can you please explain the logic you used to arrive at the solution? Thanks :-) On Aug 29, 12:25 pm, rahul patil wrote: > check out this solution.I think this works correct > will explain logic if u find it c

[algogeeks] Re: Binary tree to LL

2010-08-28 Thread jagadish
I think a solution to convert a Binary Tree to a Doubly Linked List has been discussed! On Aug 27, 9:32 pm, balashankar sundar wrote: > head=new node;//head node to list,T is root to the tree > join=head;//global variable > > inorder(T) > { > if(T==0) > return; > inorder(t->l)

[algogeeks] Re: Help with Increment Operators in C!

2010-08-28 Thread jagadish
Ya after some reading i got to know that it was implementation dependent.. And that the answer is undefined! On Aug 28, 5:07 pm, Chi wrote: > In php it is 19. > > $x=5; > printf("%d",($x++ + ++$x + $x++)); > ?> > > On Aug 28, 1:35 pm, jagadish wrote: >

[algogeeks] Help with Increment Operators in C!

2010-08-28 Thread jagadish
I ran this code.. int main() { int x=5; printf("%d",(x++ + ++x + x++)); } The output printed was 18 instead of 19.. Should it not be 19? -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegr

[algogeeks] Re: Amazon interview Question (Array yet again!)

2010-08-27 Thread jagadish
@Gene: It would be helpful if you could throw some light on the sub structure of the problem! And how to formulate it.. Thanks for your time :) On Aug 28, 8:36 am, Gene wrote: > Yes.  Exactly.  DP will get it. > > On Aug 27, 11:34 pm, jagadish wrote: > > > Ya it fails..

[algogeeks] Re: Amazon interview Question (Array yet again!)

2010-08-27 Thread jagadish
Should it be done using dynamic programming then? On Aug 28, 8:32 am, jagadish wrote: > @Aravind: I think the solution given by Gene works!. > The answer for 8 2 2 2 2 2.. is six.. > > On Aug 28, 8:26 am, jagadish wrote: > > > @Gene: Thanks alot! :-) your solution works lik

[algogeeks] Re: Amazon interview Question (Array yet again!)

2010-08-27 Thread jagadish
Ya it fails.. Should it then be Dynamic Programming then?? On Aug 28, 8:32 am, jagadish wrote: > @Aravind: I think the solution given by Gene works!. > The answer for 8 2 2 2 2 2.. is six.. > > On Aug 28, 8:26 am, jagadish wrote: > > > @Gene: Thanks alot! :-) your solu

[algogeeks] Re: Amazon interview Question (Array yet again!)

2010-08-27 Thread jagadish
@Aravind: I think the solution given by Gene works!. The answer for 8 2 2 2 2 2.. is six.. On Aug 28, 8:26 am, jagadish wrote: > @Gene: Thanks alot! :-) your solution works like charm! > > On Aug 28, 7:09 am, Gene wrote: > > > This is a nice problem.  It looks like a tri

[algogeeks] Re: Amazon interview Question (Array yet again!)

2010-08-27 Thread jagadish
heaper.  Do it. >         total_cost += a[i + 1]; >         for (j = i + 1; j < n - 1; j++) >           a[j] = a[j + 1]; >         --n; >       } >     } >   } >   return total_cost; > > } > > int main(void) > { >   int a[] = { 14, 15, 16, 13, 11, 18 }; >

[algogeeks] Re: Amazon interview Question (Array yet again!)

2010-08-27 Thread jagadish
@Rahul: Yea that is admissible.. Each decrement will add one to the cost! On Aug 27, 9:36 pm, Rahul Singal wrote: > can u decrement d same element more then once ?? -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, sen

[algogeeks] Amazon interview Question (Array yet again!)

2010-08-27 Thread jagadish
You are given an array of positive integers. Convert it to a sorted array with minimum cost. Only valid operation are 1) Decrement -> cost = 1 2) Delete an element completely from the array -> cost = value of element For example: 4,3,5,6, -> cost 1 10,3,11,12 -> cost 3 -- You received this messa

[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

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

[algogeeks] Re: another google telephone interview question

2010-05-23 Thread Jagadish M
standard algorithm for median finding can be tweaked to use only constant extra space. @Bharath: It's a nice algorithm, nevertheless :) -Jagadish http://www.cse.iitb.ac.in/~jagadish -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group

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

[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