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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Warmup
  4. Mini-Max Sum
  5. Discussions

Mini-Max Sum

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 4699 Discussions, By:

votes

Please Login in order to post a comment

  • olivierliu
    6 years ago+ 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)))
    
    267|
    Permalink
    View more Comments..
  • Kanahaiya
    3 years ago+ 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.

    148|
    Permalink
    View more Comments..
  • bajal
    5 years ago+ 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));
           
        }
    }
    
    83|
    Permalink
    View more Comments..
  • suraneti_rod
    3 years ago+ 13 comments

    Python is the best

    x = sum(arr)
    
    minValue = x - max(arr)
    maxValue = x - min(arr)
    
    print(minValue, maxValue)
    
    68|
    Permalink
    View more Comments..
  • luumanhlapdi
    5 years ago+ 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);
    
    18|
    Permalink
    View more Comments..
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature