Mini-Max Sum Solution in Java | HackerRank | Mini-Max Sum HackerRank Solution


Mini-Max Sum Solution in Java | HackerRank | Mini-Max Sum HackerRank Solution

HackerRank Algorithm's Solution


Mini-Max Sum Solution in Java
Mini-Max Sum HackerRank Solution in Java


Problem:

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

Function Description

Complete the miniMaxSum function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of  of  elements.

miniMaxSum has the following parameter(s):

  • arr: an array of  integers

Input Format

A single line of five space-separated integers.

Constraints

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

Our initial numbers are , and . We can calculate the following sums using four of the five integers:

  1. If we sum everything except , our sum is .
  2. If we sum everything except , our sum is .
  3. If we sum everything except , our sum is .
  4. If we sum everything except , our sum is .
  5. If we sum everything except , our sum is .

Hints: Beware of integer overflow! Use 64-bit Integer.


Need help to get started? Try the Solve Me First problem


https://www.hackerrank.com/challenges/mini-max-sum/problem

Mini-Max Sum HackerRank Solution in Java 
Mini-Max Sum Solution in Java


import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { static void miniMaxSum(int[] arr) { long min=0,max=0; int temp; int n=arr.length; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(arr[i]>arr[j]){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } for(int i=1;i<n;i++){ max+=arr[i]; } for(int i=0;i<n-1;i++){ min+=arr[i]; } System.out.println(min+" "+max); } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int[] arr = new int[5]; String[] arrItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int i = 0; i < 5; i++) { int arrItem = Integer.parseInt(arrItems[i]); arr[i] = arrItem; } miniMaxSum(arr); scanner.close(); } }

Post a Comment

0 Comments