FROm hackerrank

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
For example, . Our minimum sum is  and our maximum sum is . We would print

16 24

Solution in C# -

class Solution {
    static void miniMaxSum(int[] arr) {
        Array.Sort(arr);
        long min = 0;
        long max = 0;
        for(int i=0; i<4; i++)
        min += arr[i];
        for(int i=4; i>0; i--)
        max += arr[i];
        Console.WriteLine(min+" "+max);
    }
    static void Main(string[] args) {
        int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp));
        miniMaxSum(arr);
    }
}

Post a Comment

Previous Post Next Post