简体   繁体   中英

HackerRank Task “Mini Max Sum” solution not passing 3 of the 13 test cases, can someone tell me what i'm doing wrong

This is for the "Mini Max Sum" problem on HackerRank, I can't see why it doesn't have a check mark on all of the test cases. Can someone tell me where my problem lies at. The question is:

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 32 bit integer.)

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long[] arr = new long[5];
        long total = 0, max = 0, min = 0;
        for(int arr_i=0; arr_i < 5; arr_i++){
            arr[arr_i] = in.nextLong();
            min = arr[0];
            total += arr[arr_i];
            if (arr[arr_i] > max)
                max = arr[arr_i];
            if (arr[arr_i] <= min)
                min = arr[arr_i];
        }
        System.out.println((total - max) + " " + (total - min));
    }
}

The problem are the initial values of both min and max .

  1. min is being reset to the first values inside the loop, so it will probably only work if the first or the last value is the minimum one;

  2. max starts with zero, so if all values are negative, max will stay at zero (instead of one of the input values).

Hints: set min and max on the first iteration ( i == 0 ) or, as suggested, use Integer.MAX_VALUE and Integer.MIN_VALUE respectively as initial value (actually long is not needed for min and max , neither is the array)

This also worked. Thanks!!

static void miniMaxSum(int[] arr) {
    List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
    Collections.sort(list); 
    long x=0, y=0;
    for(int j=0;j<(list.size()-1);j++){
            x = x + list.get(j);
            y = y + list.get(j+1);
    }
    System.out.println(x +" "+y);
}
static void miniMaxSum(int[] arr) {
       int temp = 0;
      for (int i = 0; i < arr.length; i++) 
        {
            for (int j = i + 1; j < arr.length; j++) { 
                if (arr[i] > arr[j]) 
                {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        long minSum = 0;
        long maxSum = 0;
      for(int i = 1; i< arr.length; i++){
         maxSum = maxSum + arr[i];
      }
      for(int i = 0; i< arr.length-1; i++){
         minSum = minSum + arr[i];
      }
      System.out.print(minSum+ " " +maxSum);
    }

This worked for me.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.stream.LongStream;

public class Solution {

// Complete the miniMaxSum function below.
static void miniMaxSum(int[] arr) {
long[] longs = Arrays.stream(arr).asLongStream().toArray();

Arrays.sort(longs);
long sum = LongStream.of(longs).sum();

long min = sum - longs[4];
long max = sum - longs[0];

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


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

Here's a hint in the question: minimum and maximum values that can be calculated by summing exactly four of the five integers .

Just sort the array first, assuming the array is not sorted. Take two for loop because we want to keep the complexity up to O(n) ie Linear.

  1. from 1st index to n - 1 . Assuming index starts from 0 . This will give you sum of all the element except the smallest element which will be the largest sum.
  2. from 0th index to n - 2 . Assuming index starts from 0 . This will give you sum of all the element except the largest element which will be the least sum.

Let's say, 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.
public static void minmaxsum(int[] ar1) {
    long a, b, c, d, e;

    a = (long) ar1[1] + (long) ar1[2] + (long) ar1[3] + (long) ar1[4];
    System.out.println(a);

    b = (long) ar1[0] + (long) ar1[2] + (long) ar1[3] + (long) ar1[4];
    System.out.println(b);

    c = (long) ar1[0] + (long) ar1[1] + (long) ar1[3] + (long) ar1[4];
    System.out.println(c);

    d = (long) ar1[0] + (long) ar1[1] + (long) ar1[2] + (long) ar1[4];
    System.out.println(d);

    e = (long) ar1[0] + (long) ar1[1] + (long) ar1[2] + (long) ar1[3];
    System.out.println(e);

    long[] df = new long[] { a, b, c, d, e };

    long max = df[0];
    for (int i = 0; i < df.length; i++) {
        if (df[i] > max) {
            max = df[i];
        }
    }

    long min = df[0];
    for (int i = 0; i < df.length; i++) {
        if (df[i] < min) {
            min = df[i];
        }
    }
    System.out.println(min + " " + max);

}

Worked for me on Python3!

def miniMaxSum(arr):

    total = sum(arr)
    low = total - min(arr)
    high = total - max(arr)

    print(high, low)

    return
import math
import os
import random
import re
import sys

def miniMaxSum(arr):
   arr.sort()
   m = sum(arr)
   
   max_num = m - arr[-1]
   min_num = m - arr[0]
   
   print(max_num, min_num)

if __name__ == '__main__':

    arr = list(map(int, input().rstrip().split()))

    miniMaxSum(arr)
static void miniMaxSum(int[] arr) {
    long[] longs = Arrays.stream(arr).asLongStream().toArray();

    Arrays.sort(longs);
    long sum = LongStream.of(longs).sum();

    long min = sum - longs[4];
    long max = sum - longs[0];

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

This answer is in PYTHON language. I am a beginner and any improvements are welcome

n = input().split(" ")
n=list(n)
n1 = list(map(int,n))
n2 = list(map(int,n))
n1.sort()
n1.pop()
min =0
max=0
for i in n1:
min+=i
n2.sort()
n2.reverse()
n2.pop()
for j in n2:
max+=j
print(min, max)

This is what worked for me in JavaScript:

var sum = 0;

var arry = [];

function miniMaxSum(arr) {

    for (var i = 0; i < arr.length; i++) {

        for (var j = 0; j < arr.length; j++) {

            if (arr[j] !== arr[i]) {
                sum += arr[j];
            }

        }
        arry.push(sum);

        sum = 0;

    }


    var min = Math.min(...arry);
    var max = Math.max(...arry);

    console.log(min, max);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM