Mini-Max Sum Hacker Rank

2
 package solution;
 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) {
    int suma = 0;
    int num = 0;
for(int i = 0; i < arr.length; i++){
    num = arr[i];
    suma = suma + num;
}
    Arrays.sort(arr);
    System.out.println((suma-arr[arr.length-1]) + " " + (suma-arr[0]));
}

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();
 }
}

That is the complete code of the exercise already with my solution in the.

But if these values are set: 256741038 623958417 467905213 714532089 938071625 .

In the last sum 2063136757 + 938071625 me da = -1293758914 !!!! I do not understand why, I already debug it and everything goes well but adding those amounts gives me that result !!!

The weird thing is that with the minimum value 256741038 , when you rest -1293758914 - 256741038 , if you give me the correct value of the exercise that is 2063136757 . But with the maximum value it gives me -1550499952 .

Expected outputs are assumed to be 2063136757 2744467344

    
asked by L.N. Ricardo Sanchez Santos 11.08.2018 в 20:03
source

1 answer

0

It's because 2,744.467.344 is a very large value to store it in the bytes of a data type int .

You can solve it simply by using the primitive data type long instead of the int .

tipo        tam. bytes       rango

byte        1 byte           -128 a 127
short       2 bytes          -32,768 a 32,767
int         4 bytes          -2,147.483.648 a 2,147.483.647
long        8 bytes          -9,223,372,036,854,775,808 a 9,223,372,036,854,775,807
float       4 bytes          ±3.40282347E+38F (Aproximadamente)
double      8 bytes          ±1.79769313486231570E+308 (Aproximadamente)
char        2 bytes          0 a 65,536
boolean     (1 bit)          true o false
    
answered by 12.08.2018 в 01:25