Reputation: 25
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));
}
}
Upvotes: 2
Views: 12606
Reputation: 11
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)
Upvotes: 1
Reputation: 21
Worked for me on Python3!
def miniMaxSum(arr):
total = sum(arr)
low = total - min(arr)
high = total - max(arr)
print(high, low)
return
Upvotes: 1
Reputation: 11
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);
}
Upvotes: 0
Reputation: 1939
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);
}
Upvotes: 1
Reputation: 2582
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) i.e. Linear.
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.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:
Upvotes: 0
Reputation: 66
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);
}
Upvotes: 1
Reputation: 1
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)
Upvotes: -1
Reputation: 671
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();
}
}
Upvotes: 0
Reputation: 21
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);
}
Upvotes: 2
Reputation: 29680
The problem are the initial values of both min
and max
.
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;
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)
Upvotes: 1