You are solving the wrong problem. The problem is not asking for the
difference between the two minimum elements, but for the minimum
difference between any pair of elements. In the case

    int array[10]={99,35,45,33,88,9098,112,33455,678,3};

the minimum difference is |35-33| = 2.

Dave

On Sep 24, 5:02 am, Rishi Agrawal <rishi.b.agra...@gmail.com> wrote:
>  Printing the Array:
>  99  35  45  33  88  9098  112  33455  678  3
>
> Min elements are 1: 3 2: 33
>  Absolute difference between two minimum elements are 30
>
>
>
>
>
> On Fri, Sep 24, 2010 at 9:39 AM, Dave <dave_and_da...@juno.com> wrote:
> > @Rishi: Try it on the original data with a[1] changed from 12 to 35:
>
> >    int array[10]={99,35,45,33,88,9098,112,33455,678,3};
>
> > What do you get as a result?
>
> > Dave
>
> > On Sep 23, 12:36 pm, Rishi Agrawal <rishi.b.agra...@gmail.com> wrote:
> > > @Dave, I check for the values you suggested but the code worked fine.
> > There
> > > were other errors in the code. I have rectified them now.
>
> > > The following code seems to be working fine and in O(n) time.
>
> > > #include <stdio.h>
> > > #include <stdlib.h>
> > > void find_two_mins(int array[], int size)
> > > {
> > >     int i,t;
> > >     int min1, min2;
> > >     if(array[0] <= array[1]) {
> > >         min1=array[0];
> > >         min2=array[1];
> > >     } else {
> > >             min2=array[0];
> > >         min1=array[1];
> > >     }
> > >     for (i=2; i<size; i++){
> > >         t=array[i];
> > >         if(t<=min1) {
> > >             min2=min1;
> > >             min1=t;
> > >             continue;
> > >         }
> > >     if(t<=min2) {
> > >         min2=t;
> > >     }
> > >     }
> > >     printf("\nMin elements are 1: %d 2: %d", min1,min2);
> > >     printf("\n Absolute difference between two minimum elements are
> > %d\n\n",
> > > abs(min1-min2));
>
> > > }
>
> > > int main()
> > > {
> > >     //int array[10]={ 9,2,3,4,1,7,5,6,8,0};
> > >     //
> > >     //int array[10]={3,3,1,33,88,9098,112,33455,678,1};
> > >     //int array[10]={5,3,1,33,88,9098,112,33455,678,1};
> > >     //int array[10]={2,3,21,33,88,9098,112,33455,678,12};
> > >     int array[10]={3,2,21,33,88,9098,112,33455,678,12};
>
> > >     find_two_mins(array,10);
> > >     return 0;
>
> > > }- Hide quoted text -
>
> > > - Show quoted text -
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to algoge...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups­.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
>
> --
> Regards,
> Rishi Agrawal- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@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.

Reply via email to