0

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();
    }
}
Anurag
  • 95
  • 4
  • 17

3 Answers3

1

First of all you should write a generic code while solving a problem so that it can perform all kinds of test cases for the problem. Eg. What if array size is more?

And I also saw, in some answers to your question, they have first sorted the array, making the code slow. You never need sorting in this case.

Here is the solution, which solves the problem in O(n) and is faster

static void minMaxSum(int[] arr) {
        
    int min = arr[0];
    int max = arr[0];
    long totalSum = arr[0];

    for (int i = 1; i < arr.length; i++) {
        if (min > arr[i]) {
            min = arr[i];
        } else if (max < arr[i]) {
            max = arr[i];
        }
        totalSum += arr[i];
    }
    
    long minSum = totalSum - max;
    long maxSum = totalSum - min;
    
    System.out.println(minSum + " " + maxSum);
}

Now coming to your original question that why the int data is not converting into long.

This is happening because when you add multiple integers in languages like C, C++, Java or C#, the result calculated will always be an integer, since all the operands were integers. And since the result is an integer, it calculated a -ve integer value, then assigned it long. So long did not receive the original value.

To interpret the addition as long, you should perform explicit type casting on one of four operands. See, int + int => int But, long + int => long

So you should write, (casting first operand to long)

ar[0] = (long)arr[1] + arr[2] + arr[3] + arr[4]

And this will give correct result.

  • You are correct I tend to write only generic code, but in the problem statement itself they have given only 5 elements that's why I have hard coded the elements. Anyway thanks for the solution it helped a lot. – Anurag Aug 30 '20 at 15:48
0

Always write code in generic way, You hard-coded everything(fixed 5 elements).

Use the below solution, It will pass all your test cases:

static void miniMaxSum(int[] arr) {

  int n =arr.length;
  Arrays.sort(arr);
  long min = 0;
  long max = 0;
    
  for(int i = 0; i <= n - 2; i++){
        min += arr[i];
  }
  for (int i = 1; i <= n - 1; i++){
        max += arr[i];
  }

  System.out.println(min + " " + max);
}
0

There's no problem in output, it is already in long. However your logic makes no sense you've created 5 arrays ar each having different sum. The for loop stores ar[4] in a[i]. Sorting is done on a single element and max and min have arbitrary values assigned.
You just need to sort the array in ascending order, find the sum of first and last 4 elements and display them.

static void miniMaxSum(int[] arr) 
{
    long sum1 = 0, sum2 = 0;

    Arrays.sort(arr);             //Ascending Sort
    for (int i = 0; i < 4; i++) 
        sum1 +=  arr[i];          //First 4 sum

    for (int j = arr.length - 1; j > 0; j--) 
        sum2 +=  arr[j];          //Last 4 sum

    System.out.println(sum1 + " " + sum2);
}
Ankit
  • 682
  • 1
  • 6
  • 14
  • Your's is short I liked it, but your code also not going to provide the exact answer, because you have missed the type casting. It should be like this -> long sum1 = (long)arr[0] + arr[1] + arr[2] + arr[3]; – Anurag Aug 30 '20 at 15:51