How To Solve Mini-Max Sum HackerRank Problem [Trick Revealed]

How To Solve Mini-Max Sum HackerRank Problem [Trick Revealed]

35,287 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Mark
Mark - 09.02.2019 09:10

Hi, I followed the link you sent me in the Hackerrank discussion section. Great video!
I always like better optimized code that is legible.

While I'm talking about optimization even though I'm very much a noob, would it not save a calculation to use "else if" in your loop as by the time you have finished the comparison
if ( arr[i] < min ), the second comparison (arr[i] > max) becomes an impossibility, or is this change just very insignificant in the scheme of things?

Lastly, good luck with your channel. I hope to see more "thought-process" hacker rank tutorials like this that don't just dive into solving the problem but explaining.

I might recommend you add some narration if its not too taxing as I think that would greatly increase the quality of the videos.

Ответить
edward alexander arroyave agudelo
edward alexander arroyave agudelo - 20.12.2022 05:55

typeScript solution
function miniMaxSum(arr: number[]): void {

const orderValues:number[] = arr.sort()
const minValue:number = orderValues[0];
const maxValue:number = orderValues[orderValues.length - 1];
let sumValues:number = arr.reduce((fristValue,secondValue)=>fristValue + secondValue, 0)
console.log((sumValues - maxValue), (sumValues-minValue));

}

Ответить
edward alexander arroyave agudelo
edward alexander arroyave agudelo - 20.12.2022 05:45

gracias que pro.

Ответить
Esha Kumari
Esha Kumari - 04.07.2022 18:18

good explaination

Ответить
Chandradhar Rao
Chandradhar Rao - 01.06.2021 17:23

This requires O(nlogn),we can do it O(n)

Ответить
Nitish Singh
Nitish Singh - 12.04.2021 06:28

why you subtract min and max element from sum logic is not clear

Ответить
Sai-
Sai- - 09.02.2021 18:20

Why do we have to declare min, max, sum with long type? I really dont get it!

Ответить
sachin patil
sachin patil - 13.01.2021 16:36

import java.util.*;
import java.util.Arrays.*;


class Solution{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n =sc.nextInt();
int[] a =new int[n];

for (int i = 0; i <n; i++) {
a[i] =sc.nextInt();
}
Arrays.sort(a);
int max = 0,min =0;
for (int i = 0; i <n-1; i++) {

min = min +a[i];
}
for (int i = 1; i <n; i++) {

max = max +a[i];
}
System.out.println(min);
System.out.println(max);


}
}

Ответить
Khaing Myel Khant
Khaing Myel Khant - 09.11.2020 18:09

thanks, i learnt how to think from you

Ответить
Raj Gupta
Raj Gupta - 09.10.2020 17:35

This is what i did (Note: this is not at all an optimal solution )
static void miniMaxSum(int[] arr) {
long max=0,min=0,sum=0; int count=0;
for(int j=0;j<arr.length;j++)
{
for(int i=0;i<arr.length;i++)
{
if(i!=count)
sum+=arr[i];
}
if(j==0) min=sum;
count++;
if(sum>max)max=sum;
if(sum<min)min=sum;
sum=0;
}
System.out.print(min+" "+max);

}

Ответить
Malumi
Malumi - 01.10.2020 13:53

tank you, i am so bad in the problems :,v

Ответить
samir moukhliss
samir moukhliss - 12.09.2020 19:56

Nice one, i need to change my way of coding , Thanks

Ответить
C R
C R - 08.06.2020 01:10

Thank you so much

Ответить
Nicolas
Nicolas - 04.05.2020 22:18

helpful video

Ответить
Abhishek
Abhishek - 23.04.2020 21:35

is it good approach or not?

Ответить
Manav Malhotra
Manav Malhotra - 10.01.2020 17:28

great
thnx buddy

Ответить
Sachin Kumbar
Sachin Kumbar - 28.12.2019 19:03

nice logic. (no need to initialize min, max, sum to zero)

Ответить
Mike
Mike - 10.11.2019 12:55

Really Useful Trick!

Ответить
Mohamed Abshan
Mohamed Abshan - 27.10.2019 23:22

This method does the same job, but still, 11/15 test cases failed :(


static void miniMaxSum(int[] arr) {

int[] sum = new int[arr.length];
for(int i = 0; i < arr.length; i++){
int current = arr[i];
sum[i] = Arrays.stream(arr).filter(s -> s != current).sum();
}
int max = Arrays.stream(sum).max().orElse(0);
int min = Arrays.stream(sum).min().orElse(0);

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

Ответить
VS
VS - 03.09.2019 14:01

Sir may I know what IDE are you using

Ответить
AbnormalBat
AbnormalBat - 29.06.2019 01:15

This Tut Solved My Problem !! Thanks!

Ответить
ABHISHEK SONKAR
ABHISHEK SONKAR - 25.03.2019 05:33

Hi , I just followed your link you sent me in Hackerrank . Thank You for this solution :)

Ответить
SUBHENDRA RANASINGH
SUBHENDRA RANASINGH - 02.03.2019 13:05

gud solution ..complexity decreased, great work

Ответить
JAVAAID - Coding Interview Preparation
JAVAAID - Coding Interview Preparation - 02.02.2019 16:11

Hello Coding Lover,

This is the updated version of mini-max sum hackerrank problem tutorial with subtitle with better sound.

Ответить