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.
@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.
Implement a function getbits, that returns the(right adjusted) n bits that begin at position p of an integer. Assume bit position 0 is at the right end and that n and p are sensible positive values.
You are given n real numbers in an array. A number in the array is called a decimal dominant if it occurs more than n/10 times in the array. Give an O(n) time algorithm to determine if the given array has a decimal dominant.
I dint get your question...what does less comparisons mean??can you pls explain the question?
ReplyDelete@kamakshi for finding maximum and minimum value in an array you must do some kind of comparison,so we have to optimize that.
ReplyDelete@kamakshi eg arr[]={5,2,3,6,9,8,0};
ReplyDeletemin_value = 0;
max_value = 9;
how do you find this.
sorry can u delete the above post...
ReplyDeletemax=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.
@kamakshi now you got the question correct.Now you have to optimize number of comparisons to find min and max value.
ReplyDeleteNOTE: Don't ever use min and max value as any fixed value.You can initialize min and max as INT_MIN and INT_MAX.