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.
Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples: 1 / \ 2 3 / \ / \ 4 5 6 7 The tree has 5 vertical lines Vertical-Line-1 has only one node 4 => vertical sum is 4 Vertical-Line-2: has only one node 2=> vertical sum is 2 Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 Vertical-Line-4: has only one node 3 => vertical sum is 3 Vertical-Line-5: has only one node 7 => vertical sum is 7 So expected output is 4, 2, 12, 3 and 7
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.