+ 1

This problem is asking for minimum and maximum values that can be calculated by summing exactly four of the five integers.

I have solved it in C# but my code failed in the testcases of the submission. class Solution { static void miniMaxSum(int[] arr) { long max = 0 ,sum= arr[0]+arr[1]+arr[2]+arr[3]+arr[4] , min =arr[0]; for(int i = 0 ; i < 5 ; i++) { if (max < arr[i]) max= arr[i]; if (min > arr[i]) min= arr[i]; } Console.Write((sum-max)+" "+ (sum-min)); } static void Main(string[] args) { int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp)) ; miniMaxSum(arr); } }

13th Dec 2020, 10:01 PM
Esraa Hesham
Esraa Hesham - avatar
3 Respostas
+ 2
I don't know whether this makes any sense or will suit your code purpose, but I was thinking to sort the array in ascending order. Sum from 0th to 4th to get the minimum, sum from the 4th to the 0th to get maximum.
14th Dec 2020, 12:47 AM
Ipang
+ 2
Esraa Hesham No need to calculate sum using hard code value. This is not a proper way because if there is more than 10 items then? So you need to calculate sum inside loop. max = arr[0]; min = arr[0]; for (int i = 0; i < 5; i++) { if (max < arr[i]) max = arr[i]; if (min > arr[i]) min = arr[i]; sum += arr[i]; } And also you can get min and max value after sorting the array as Ipang said.
14th Dec 2020, 1:47 AM
A͢J
A͢J - avatar
+ 1
Ipang and I Am Groot ! Thank you so much!! It works!
14th Dec 2020, 9:33 PM
Esraa Hesham
Esraa Hesham - avatar