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

    You are viewing a single comment's thread. Return to all 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));
           
        }
    }
    
    81|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature