DEV Community

Discussion on: Min-maxSum

Collapse
 
suchitra_13 profile image
Suchitra

yeah! sure..
Here i am just design a function for MinimumSum and MaximumSum but keep in you mind that in this task the given constraints are: 1. 1<=arr[i]<1000000000(10^9)

  1. we have to sum of only n-1 integers(max and mini), here n is a No. of elements in the array.

Then here ,1st you can see that output requirement in long data type right?
So, i declare my variable in long.
and here i initialize max=-1 and min=1000000000
bcoz we know the constraint range of the array elements which is 1<=arr[i]<1000000000
then i use for loop for extract elements from array and with the help of if condition
i check..this way
1st if(arr[i] This condition used for finding minimum element in the array.
2nd condition if(arr[i]>max) if true the max=arr[i]. This is used for finding max element in the array.
after finding max and min elements in array our task is to calculate sum of elements in array
so, i used another for loop for finding sum of array elements.
like this
for(int i=0;i<arr.length;i++)
{
sum=sum+arr[i];
}
finally we have sum of all the elements and also max element and mini element.
So, we can easily find the sum of minimum/maximum n-1 elements :
By subtracting from sum to max we will find miniSum and by subtracting from to min we will find miniSum.
like..
miniS=sum-max;
maxS=sum-mini;
then print them.
I hope got my point!
HappyCoding❤️