How To Solve Mini-Max Sum HackerRank Problem [Trick Revealed]
Aug 15, 2023
How To Solve Mini-Max Sum HackerRank Problem [Trick Revealed]
In this video, I have explained hackerrank mini-max sum solution algorithm. hackerrank mini-max sum problem can be solved by using one for loop. The complexity of mini-max sum hackerrank solution is O (n). This hackerrank problem is a part of Practice | Algorithms | Warmup | Mini-max sum hackerrank challenge. For simplicity, I have divided this hackerrank tutorial into 3 parts. [00:00] Understanding the problem statement. [01:55] Building the logic to solve the problem. [08:00] Coding the logic using java (you can use your own preferred programming language). 🔴 DONT CLICK THIS: https://bit.ly/2G4cEuc 👍 Like us on Facebook: https://www.facebook.com/HackerRankSo … 💎Share this video with a YouTuber friend:    • How To Solve Mini-Max Sum HackerRank …  ✚ Join our community ► 👉 Coding interview preparation group: https://www.facebook.com/groups/codingip 👉 Telegram link: https://t.me/hackerranksolutions 📖 Resources ► 🙋 Problem statement: https://www.hackerrank.com/challenges … 💡 Source code: https://github.com/Java-aid/Hackerran … ✅ Recommended playlists ► 👉 All hackerrank solutions:    • How To Master In Data Structures And …  👋 Let’s Connect ► Git Hub: https://github.com/kanahaiya Twitter: https://twitter.com/Kanahaiyagupta Linked in: https://www.linkedin.com/in/kanahaiya … Facebook: https://www.facebook.com/coolkanahaiya Instagram: https://www.instagram.com/coolkanahaiya #JAVAAID #miniMaxSum #HackerRankSolutions #HackerRankTutorials #HackerRank #JavaAidTutorials #programming #DataStructures #algorithms #coding #competitiveprogramming #java #codinginterview #problemsolving #KanahaiyaGupta #hackerrankchallenges #warmup
Content
3.379 -> Hello everyone welcomes to my YouTube channel
7.06 -> today we are going to discuss minimax hackerrank problem.
12.3 -> In this problem we have Given five positive integers,
18.8 -> and we have to find the minimum and maximum values
22.58 -> that can be calculated by summing exactly four of the five integers.
30.4 -> Then print the respective minimum and maximum values as a single line of two space-separated long integers.
40.35 -> so here in the simple example, we have an
array of five elements as mentioned above
48.8 -> and our minimum sum is 16 and our maximum
sum is 24.
54.79 -> so, we have to print both the sum minsum and
maxsum via space separated.
63.98 -> Before understanding the logic guys, we have
to just check the constraint also.
69.1 -> here we can see the constraint is array element
can have range from 1 to 10^9 .
79.369 -> It means, if we are going to add two integers.
84.7 -> it will overflow, it will exceed the integer range
88.61 -> that's why we have already mentioned
that we have to use the long integers.
95.929 -> so if you are working in a different language, you
should use the data type accordingly.
103.189 -> Like here I am working with Java, so I will
use long data type over here.
108.95 -> let's start guys, see the logic behind this
114.4 -> So in minimax we have two approaches over
117.799 -> here to solve this problem.
120.57 -> let's start the approach 1, so in this approach
1 we can sort the array in ascending order
130.81 -> so that after sorting it will arrange the
elements in increasing order then we will
141.62 -> calculate the sum of the first 4 elements
say minimum and we store the result first
151.69 -> four elements in the minsum and after that
we will calculate the sum of the last four
157.58 -> elements let’s say maxsum and store this
result in a maxsum and finally will print
167.03 -> the minsum and maxsum.
169.04 -> let's see guys this algorithm in action now.
172.98 -> for example we have an array of five elements
represented by A[].
180.74 -> Now will follow each and every step and we
see the outcome.
187.95 -> first, we are going to sort array so after
sorting we will get this output.
195.58 -> Now second step to calculate the sum of first
four elements so we will calculate the sum
202.29 -> of first 4 elements and assign it to minsum
so you can see the calculating the sum of
209.82 -> four elements we will get 10 and we are storing
inside minsum.
215.38 -> Now we will go to step 3 where we calculate
the sum of last four elements and assign it
225.54 -> to maxsum so you can see we have calculated
the sum and sum is equal to 14 and we have
233.42 -> assigned it to maxsum and finally we will
print both minsum and maxsum so this is the
242.86 -> answer this is one approach guys to solve
this problem but if you see this approach,
252.37 -> the complexity of these approache is O(nlogn)
.In a first step we are sorting this array,
262.43 -> guys this problem is defined that we will
be having only 5 elements but in general way
271.14 -> array length is suppose n then sorting the
array in any order will take around O(nlogn)
280.71 -> time and rest of the operations or steps will
take O(n) time so if you see the total/ overall
289.01 -> complexity of the solution would be O(nlogn)
which is not efficient enough to solve this
297.69 -> problem.but the same problem can be solved
in a much more efficient way let's see the
305.65 -> approach second.so here in approach 2nd we
are going to find a minimum element in the
315.43 -> array In the second step we will find the
maximum element in the array and in the third
322.85 -> step we are going to calculate the sum of
all the elements in the array and fourth one
330.02 -> we are calculating the minsum by subtracting
max value from total sum in the array.
340.63 -> In the fifth step we are calculating the maxsum
by subtracting min value from the overall sum.
349.07 -> And finally, we are going to print minsum
and maxsum. now we are going to see this algorithm in action
357.05 -> we have the same example guys over here. now
we are not going to do any sorting. we will
364.44 -> follow all the steps one by one and we'll
see the result.first step find the minimum
371.8 -> element in the array so minum element in the
array equal to 1.
377.71 -> In the next step, we are going to find maximum
element in the array which is 5. in the third
386.63 -> step we are going to sum of all the elements
in the array and assign it to sum.
393.58 -> so after doing sum of all the elements we
will get 15. now we will move to the fourth
402.06 -> step so here the minsum will be equals to
sum minus max value because from this array
411.05 -> we are removing any max value which is the
part of the sum, it means the remaining sum
418.75 -> will be minsum. so 15 minus 5 equal to 10.
426.63 -> so the minsum will be equal to 10.In the same
way, we are going to calculate the maxsum,
433.75 -> so we are subtracting minimum from the sum
so we will get maxsum.and finally we will
444.169 -> print minsum and maxsum. and if you see guys
here we have taken O(n) time to complete all
456.05 -> the above three operations and rest operation
are constant so we have to run up a loop which
463.3 -> will go till the end and we can calculate
all the stuff very easily.so the complexity
472.259 -> of this approach is O(n) . so now we will
implement the solution guys.
478.3 -> let's have a look.we have a method called
miniMaxsum and input will be an array here
487.39 -> will take long variable min ,max and sum as
we already discussed that we have to take
495 -> long datatype over here otherwise if you take
int datatype it will exceed the maximum range
504.02 -> and we are assigning arrays first element
to min and same element we are assigning to
511.21 -> max and same elements we are assigning to
sum also.
516.58 -> because you never know the array’s first
element whether it will be min, max or what?
523.279 -> so we are assigning the first element to both
the variable and sum also.
528.65 -> suppose we only one element in the array then
sum equals to that element.
535.64 -> Now we will have one for loop which will run
from 1 to array length minus 1.
544.69 -> Why it starts from first because we have already
assigned the first element of the array.so
551.37 -> it has to start from the next element then
we are going to calculate the overall sum,
562.08 -> in sum variable and here this logic to find
out the minimum from the array you can see
573.23 -> we are finding any element which is less than
minimum then we are assigning that element
581.76 -> to the min variable. in the same way, if we
are finding any element which is greater than
589.14 -> maximum then we are assigning that element
to the max variable.
593.89 -> Once this loop will run completely, we will
have total in the sum variable, minimum will
602.339 -> be stored in the min variable and maximum
will be stored in a max variable.
607.44 -> And at the end, we will just print the sum
minus max which will represent the minsum
617.56 -> and sum minus min which will represent the
minsum so this is the solution guys now let's
623.58 -> put the same code and see in the hackerrank
whether it will pass the test case or not.
633.86 -> here we are going to paste the same code and
try to run this.
644.54 -> we can see the sample test case got passed
648.92 -> now I am submitting this code.
654.56 -> you can see guys all test case got accepted.
659.42 -> I hope it's clear to you guys how we are solved
this minimax hackerrank problem.
666.28 -> Guys, I have committed the code in git hub and the link
is mentioned in the description box so you
672.61 -> can download the code from there.
676.26 -> if you really like this video please don't forget to subscribe
680.24 -> my YouTube channel thanks for watching
Source: https://www.youtube.com/watch?v=CNL1WzrtxGY