Print Max and Min Value in Java

Alok :

I have just started practicing on HackerRank to improve my coding skills. I'm mostly using Java as my preferred language. I have got this question, and I have tried my level best to give the solution but didn't clear all the test cases. I have cleared 5 out 15 test cases, but still 10 cases are left to be done. Those who are on the hackerrank can see the question by following this link : Min-Max Sum

I'm anyway giving the brief description of the question :

PROBLEM STATEMENT

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,arr=[1,3,5,7,9]. Our minimum sum is 1+3+5+7=16 and our maximum sum is 3+5+7+9=24. We would print 16 24

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 1, 2, 3, 4, and 5. We can calculate the following sums using four of the five integers:

If we sum everything except 1, our sum is 2+3+4+5=14.
If we sum everything except 2, our sum is 1+3+4+5=13.
If we sum everything except 3, our sum is 1+2+4+5=12.
If we sum everything except 4, our sum is 1+2+3+5=11.
If we sum everything except 5, our sum is 1+2+3+4=10.

My Algo

  for(i=0; i<arr.length; i++){
    totSum += arr[i];
  }

  sumOne = totSum - arr[0];
  sumTwo = totSum - arr[1];
  sumThree = totSum - arr[2];
  sumFour = totSum - arr[3];
  sumFive = totSum - arr[4];

  int[] num = {sumOne, sumTwo, sumThree, sumFour, sumFive};
  int temp = 0;
  for(i=0;i<num.length;i++){
    for(int j=1;j<(num.length-i);j++){
        if(num[j-1] > num[j]){  
            //swap elements  
            temp = num[j-1];  
            num[j-1] = num[j];  
            num[j] = temp;  
        }
    }
  }

  System.out.print(num[0] + " " + num[4]);

We can also do that by iterating through the num array, and finding the max and min value.

But somehow after all doing this, I don't clear this module.

Please Note : The number of elements in arr is fixed that is 5 only.

I have got to know that amongst the 10 fails, I got to know about one test case, which is like this :

Input(stdin) 256741038 623958417 467905213 714532089 938071625

Expected Output 2063136757 2744467344

Mureinik :

You have the right idea (although sorting the array is a bit of an overkill, since you just need its maximum and minimum values), but when you sum these large integers, you overflow the sumtot variable and get a wrong answer. Using longs instead should solve the issue:

long totSum = 0;
for(int i=0; i<arr.length; i++){
    totSum += arr[i];
}

long sumOne = totSum - arr[0];
long sumTwo = totSum - arr[1];
long sumThree = totSum - arr[2];
long sumFour = totSum - arr[3];
long sumFive = totSum - arr[4];

long[] num = {sumOne, sumTwo, sumThree, sumFour, sumFive};
long temp = 0;
for(int i=0;i<num.length;i++){
    for(int j=1;j<(num.length-i);j++){
        if(num[j-1] > num[j]){
            //swap elements
            temp = num[j-1];
            num[j-1] = num[j];
            num[j] = temp;
        }
    }
}

System.out.print(num[0] + " " + num[4]);

Note, BTW, that using Java 8's streams you can implement the same logic and save a lot of the boilerplate code, as well of the O(nlog(n)) sorting:

IntSummaryStatistics stats = Arrays.stream(arr).summaryStatistics();

System.out.println
    ((stats.getSum() - stats.getMax()) + " " + (stats.getSum() - stats.getMin()));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=93267&siteId=1