based on minmax in minimum number of comparisons..

struct pa
{
 int max;
 int secmax;
};

struct pa  getmm(int arr[], int low, int high)
{
 struct pa  mm = {-1,-1}, mml, mmr;  int mid;

 /* If there is only on element */
 if(low == high)
 {
  mm.max = arr[low];
  mm.secmax = arr[low];
  return mm;
 }

 /* If there are two elements */
 if(high == low + 1)
 {
  if(arr[low] > arr[high])
  {
   mm.max = arr[low];
   mm.secmax = arr[high];
  }
  else
  {
   mm.max = arr[high];
   mm.secmax = arr[low];
  }
  return mm;
 }

 /* If there are more than 2 elements */
 mid = (low + high)/2;
 mml = getmm(arr, low, mid);
 mmr = getmm(arr, mid+1, high);

 /* compare maximums of two parts*/
 if(mml.max > mmr.max)
 {
  int temp = mm.max;
  mm.max = mml.max;
  if(mmr.max>temp)
   mm.secmax = mmr.max;
  else
   mm.secmax = temp;
 }
 else
 {
  int temp = mm.max;
  mm.max = mmr.max;

  if(mml.max>temp)
   mm.secmax = mml.max;
  else
   mm.secmax = temp;


 }

  return mm;
}

surender
On Tue, Sep 20, 2011 at 7:48 AM, Sandy <[email protected]> wrote:

> @Utkarsh:  In Yogesh's code assigning a[1] to largest and second_largest in
> the beginning, -VE case will be handled.
>
>
> On Mon, Sep 19, 2011 at 12:10 PM, asdqwe <[email protected]> wrote:
>
>> @Yogesh:fails for negative numbers..
>>
>> (though I am also confused with the ques)
>>
>> On Sep 19, 10:38 am, Yogesh Yadav <[email protected]> wrote:
>> > current position is index n (say)
>> > largest=0;
>> > second_largest=0;
>> >
>> > for(i=1;i<=n;i++)
>> > {
>> > if(a[i]>a[n])
>> > {
>> > if(a[i]>largest)
>> > {
>> > second_largest=largest;
>> > largest=a[i];}
>> >
>> > elseif(a[i]>second_largest)
>> > second_largest=a[i];
>> >
>> > }
>> > }
>> >
>> > ....
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > On Mon, Sep 19, 2011 at 2:15 AM, sagar pareek <[email protected]>
>> wrote:
>> > > Give some examples ,i m not getting ur question
>> >
>> > > On Mon, Sep 19, 2011 at 1:45 AM, UTKARSH SRIVASTAV <
>> > > [email protected]> wrote:
>> >
>> > >> how to find second largest element than current element in an array
>> given
>> > >> condition that we have to find that second largest element from index
>> 1 to
>> > >> the index of the current element
>> >
>> > >> --
>> > >> *UTKARSH SRIVASTAV
>> > >> CSE-3
>> > >> B-Tech 3rd Year
>> > >> @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 [email protected].
>> > >> To unsubscribe from this group, send email to
>> > >> [email protected].
>> > >> 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 [email protected].
>> > > To unsubscribe from this group, send email to
>> > > [email protected].
>> > > 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 [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>>
>
>
> --
>
> *Sandeep Kumar,*
>  ( Mobile +91-9866507368
>
> *“I believe in smart work, Believe Me”*
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to