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.
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)
publicclassSolution{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);long[]nums=newlong[5];longmax=0,min=0,sum=0;nums[0]=max=min=sum=in.nextLong();//Read the first value outside the loop, to handle min calculationfor(inti=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));}}
Mini-Max Sum
You are viewing a single comment's thread. Return to all 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)