The problem is from : https://www.hackerrank.com/challenges/mini-max-sum/problem
Problem Description : 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.
Issue : To some of the test cases I am getting the output -ve, which I understood its because of there is limit to the storing in "int" size, that's why I am getting the -ve values. I don't understand where I am making the mistake.
My solution :
static void miniMaxSum(int[] arr) {
int n = arr.length;
long[] ar = new long[n];
ar[0] = arr[1] + arr[2] + arr[3] + arr[4];
ar[1] = arr[0] + arr[2] + arr[3] + arr[4];
ar[2] = arr[0] + arr[1] + arr[3] + arr[4];
ar[3] = arr[0] + arr[1] + arr[2] + arr[4];
ar[4] = arr[0] + arr[1] + arr[2] + arr[3];
long[] a = new long[n];
for(int i=0; i<n; i++){
a[i]=ar[i];
}
Arrays.sort(a);
long max =a[4];
long min =a[0];
System.out.print(min+" "+max);
}
Pre written code provided by the HackerRank :
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 {
// Complete the miniMaxSum function below.
static void miniMaxSum(int[] arr) {
}
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();
}
}