- Prepare
- Algorithms
- Warmup
- Mini-Max Sum
- Discussions
Mini-Max Sum
Mini-Max Sum
+ 62 comments This is my code in Python. Should be easy to understand even for beginners.
lst = map(int,raw_input().strip().split(' ')) x = sum(lst) print (x-(max(lst))), (x-(min(lst)))
+ 11 comments Hello friends,
In this video tutorial, I have explained hackerrank mini-max sum solution algorithm. hackerrank mini-max sum problem can be solved by using one for loop. The complexity of mini-max sum hackerrank solution is O (n).
If interested to know more about the generic algorithm in details-
click here for the video explanation of generic algorithm with complexity analysis.
or you can click on the image too to follow youtube tutorial.
Here is the working solution:-
source code :
static void miniMaxSum(int[] arr) { long min = 0, max = 0, sum = 0; min = arr[0]; max = min; sum = min; for (int i = 1; i < arr.length; i++) { sum += arr[i]; if (arr[i] < min) { min = arr[i]; } if (arr[i] > max) { max = arr[i]; } } System.out.print((sum - max) + " " + (sum - min)); }
Would really appreciate your feedback like, dislike , comment etc. on my video.
Do not forget to upvote, if you find it useful.
+ 23 comments Java solution, in linear time. The idea is that always the max sum will be (totalSum - the min number) and min Sum will be (totalSum - maxNum)
public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); long[] nums = new long[5]; long max = 0, min= 0 , sum =0; nums[0] = max = min = sum = in.nextLong(); //Read the first value outside the loop, to handle min calculation for (int i = 1; i < 5; i++) { nums[i] = in.nextLong(); if(nums[i]>max) max = nums[i]; if(nums[i]<min) min = nums[i]; sum += nums[i]; } System.out.println( (sum - max) + " " + (sum - min)); } }
+ 13 comments Python is the best
x = sum(arr) minValue = x - max(arr) maxValue = x - min(arr) print(minValue, maxValue)
+ 15 comments My solution in c# :D
string[] arr_temp = Console.ReadLine().Split(' '); long[] arr = Array.ConvertAll(arr_temp, Int64.Parse); long result1 = 0; long result2 = 0; long max = arr.Max(); long min = arr.Min(); for (int i = 0; i < arr.Length; i++) { result1 += arr[i]; result2 += arr[i]; } Console.WriteLine("{0} {1}",result1 - max,result2 - min);
Sort 4699 Discussions, By:
Please Login in order to post a comment