#HackerRank solution being weak Mini-Max Sum

22 messages · Page 1 of 1 (latest)

toxic juniper
#

I submitted my solution but it only work in 5/15 cases. I cant see errors or anything. Anyone has idea how to improve my code or why is it so bad overall?

class Result {

    /*
     * Complete the 'miniMaxSum' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static void miniMaxSum(List<Integer> arr) {
    // Write your code here
   Collections.sort(arr);
   
   List<Integer> minArray = new ArrayList<>(arr);
   List<Integer> maxArray = new ArrayList<>(arr);
   int minSum = 0;
   int maxSum = 0;
  
minArray.remove(minArray.size() - 1);
maxArray.remove(0);

for(int i = 0; i < minArray.size(); i++){
    minSum = minSum + minArray.get(i);
}

for(int i = 0; i < maxArray.size(); i++){
    maxSum = maxSum + maxArray.get(i);
}

System.out.println(minSum + " " + maxSum);


}
}

That's my result and link to the task is here if anyone wants to have a look: https://www.hackerrank.com/challenges/one-week-preparation-kit-mini-max-sum/problem?h_l=interview&isFullScreen=true&playlist_slugs[][]=preparation-kits&playlist_slugs[][]=one-week-preparation-kit&playlist_slugs[][]=one-week-day-one

mystic thistleBOT
#

This post has been reserved for your question.

Hey @toxic juniper! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

muted umbra
#

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

toxic juniper
muted umbra
#

uh. for everything?

#

well i suppose not on the lists since those are based on the input which is strictly int

#

but on the sums

#

(that's not the only issue though)

toxic juniper
#

I assume in this part

for(int i = 0; i < minArray.size(); i++){
    minSum = minSum + minArray.get(i);
}

Once I get the number it can be up too long perhaps. But what can I do to that number if it is, let's say, 99999999

muted umbra
#

make your variable able to hold that

toxic juniper
#

But you say it is not just the sums making a problem

muted umbra
#

sorry if that was confusing, that's not what i meant

#

the results, at the very least, must be able to handle the large values

#

the input is already int so you can't change that, so that's fine

toxic juniper
#

Yep, I made sums to be long and all tests passed. No worries, without your help I wouldnt figure it out. Big thanks sir

muted umbra
#

you don't need separate minArray/maxArray btw, they have the same contents as arr and they're never changed

toxic juniper
#

They are changed later on. They need to be different. I removed different numbers from them

#

Check line 30 & 31

muted umbra
#

that's a bit of a different topic though, this is good as is

toxic juniper
#

I believe so. Happy that it works at least for now