maximum and minimum element in an array

You are provided with an array. Find the maximum and minimum numbers in less number of comparisons.

Comments

  1. I dint get your question...what does less comparisons mean??can you pls explain the question?

    ReplyDelete
  2. @kamakshi for finding maximum and minimum value in an array you must do some kind of comparison,so we have to optimize that.

    ReplyDelete
  3. @kamakshi eg arr[]={5,2,3,6,9,8,0};
    min_value = 0;
    max_value = 9;
    how do you find this.

    ReplyDelete
  4. sorry can u delete the above post...
    max=0;
    min=999;
    for(i=0;i,n;i++)
    {if(a[i]>max)
    max=a[i];
    if(a[i]<min)
    min=a[i];
    }
    but this requires 2n comparisons.

    ReplyDelete
  5. @kamakshi now you got the question correct.Now you have to optimize number of comparisons to find min and max value.
    NOTE: Don't ever use min and max value as any fixed value.You can initialize min and max as INT_MIN and INT_MAX.

    ReplyDelete

Post a Comment

Popular posts from this blog