Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread sukran dhawan
where n is the size of array On Sat, Sep 10, 2011 at 9:16 PM, sukran dhawan wrote: > > > large1 = a[0]; > large2 = a[1]; > > > if(large1 < large2) > swap(large1,large2) > > while(i < n) > { > if(a[i] > large1) >{ > large2 = large1 > large1 = a[i] > } > else if(a[i] > large2) > l

Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread sukran dhawan
large1 = a[0]; large2 = a[1]; if(large1 < large2) swap(large1,large2) while(i < n) { if(a[i] > large1) { large2 = large1 large1 = a[i] } else if(a[i] > large2) large2 = a[i] } // test the case if no of elements is 1 :) On Sat, Sep 10, 2011 at 8:54 PM, Dave wrote: > @Abhinav:

Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread Gaurav Menghani
Well you can avoid that condition by comparing the number by: 1. Keeping two numbers, largest and second largest. 2. Comparing with the second largest. If it is greater than the second largest, set second_largest = num. Else continue. 3. If second_largest > largest, swap(largest,second_largest). O

Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread abhinav gupta
sort it in quicksort (descending order)...den take arr[1] -->second largest On Sat, Sep 10, 2011 at 8:34 AM, Akhilesh Vedhera wrote: > Then the complexity will be nlogn not n and if it is the worst case > then it would be O(n^2)... > > On Sat, Sep 10, 2011 at 8:58 PM, abhinav gupta > wrote:

Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread Akhilesh Vedhera
Then the complexity will be nlogn not n and if it is the worst case then it would be O(n^2)... On Sat, Sep 10, 2011 at 8:58 PM, abhinav gupta wrote: > Oops ..no u hav to quicksort it. > > > On Sat, Sep 10, 2011 at 8:24 AM, Dave wrote: > >> @Abhinav: Does it work correctly on {1, 3, 2}, or, f

Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread abhinav gupta
Oops ..no u hav to quicksort it. On Sat, Sep 10, 2011 at 8:24 AM, Dave wrote: > @Abhinav: Does it work correctly on {1, 3, 2}, or, for that matter, on > any array where the second largest comes after the largest? > > Dave > > On Sep 10, 10:16 am, abhinav gupta wrote: > > temp2 is second largest

[algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread Dave
@Abhinav: Does it work correctly on {1, 3, 2}, or, for that matter, on any array where the second largest comes after the largest? Dave On Sep 10, 10:16 am, abhinav gupta wrote: > temp2 is second largest element. > > On Sat, Sep 10, 2011 at 8:00 AM, abhinav gupta > wrote: > > > > > > > I can so

Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread abhinav gupta
temp2 is second largest element. On Sat, Sep 10, 2011 at 8:00 AM, abhinav gupta wrote: > I can solve this problem in O(n) > i=0; > temp1=arr[0]; > > while(i != len) > { > if(arr[i] > temp1) > { > temp2=temp1; > temp1=arr[i] > } > i++; > } > > On Sat, Sep 10, 2011 at 7:42 AM, Dave wrote: > >> @Re

Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread abhinav gupta
I can solve this problem in O(n) i=0; temp1=arr[0]; while(i != len) { if(arr[i] > temp1) { temp2=temp1; temp1=arr[i] } i++; } On Sat, Sep 10, 2011 at 7:42 AM, Dave wrote: > @Replying to my own posting: remove the words "one of the numbers that > lost to", so that the explanation reads > > The q

[algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread Dave
@Replying to my own posting: remove the words "one of the numbers that lost to", so that the explanation reads The question should be "How can we find the second largest element in an array in n + ceiling(log_2(n)) - 2 comparisons?" The answer is to use a tournament to select the largest number. T

Re: [algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread Ishan Aggarwal
+1. On Sat, Sep 10, 2011 at 7:58 PM, Dave wrote: > @Praveen: The question should be "How can we find the second largest > element in an array in n + ceiling(log_2(n)) - 2 comparisons?" The > answer is to use a tournament to select the largest number. The second > largest number will have lost to

[algogeeks] Re: How can we find second largest element in an array... in O(n+logn-2)... give me proof.....

2011-09-10 Thread Dave
@Praveen: The question should be "How can we find the second largest element in an array in n + ceiling(log_2(n)) - 2 comparisons?" The answer is to use a tournament to select the largest number. The second largest number will have lost to one of the numbers that lost to the largest. It takes n - 1