We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I did not consider that the arrey is sorted. I could write it this way :
// I have to check all five member of the array with loop// to find max and min valuelongmin_sum=0;longmax_sum=0;for(inti=0;i<5;i++){if(sum-arr[i]<min_sum){min_sum=sum-arr[i];}if(sum-arr[i]>max_sum){max_sum=sum-arr[i];}}// but I can initialized max and min with sum-arr[4] instead of //0 so I dont need to check last member becaus I already did at// first time by initialization.longmin_sum=sum-arr[4];longmax_sum=sum-arr[4];for(inti=0;i<4;i++){if(sum-arr[i]<min_sum){min_sum=sum-arr[i];}if(sum-arr[i]>max_sum){max_sum=sum-arr[i];}}// both work with not sorted array.
If we have sorted array we don't need to use For, and it is easier to find max and min like this:
max= sum-arr[0];// because arr[0] is amallest element so sum-arr[0] is max.
and mim=sum-arr[4]; because arr[4] is biggest value of array so sum-arr[4] is min.
Mini-Max Sum
You are viewing a single comment's thread. Return to all comments →
I did not consider that the arrey is sorted. I could write it this way :
If we have sorted array we don't need to use For, and it is easier to find max and min like this: max= sum-arr[0];// because arr[0] is amallest element so sum-arr[0] is max. and mim=sum-arr[4]; because arr[4] is biggest value of array so sum-arr[4] is min.