How To Solve Plus Minus HackerRank Problem in Java
Aug 15, 2023
How To Solve Plus Minus HackerRank Problem in Java
Plus minus hackerrank problem can be solved easily by using one for loop. The complexity of plus minus hackerrank solution is O (n). This hackerrank problem is a part of Practice | Algorithms | Warmup | Plus minus hackerrank challenge. This video explains how to solve plus minus hackerrank problem in java. Here I have explained the algorithm to solve hackerrank plus minus problem separately and then implemented the same algorithm using java. For simplicity, I have divided this hackerrank tutorial into 3 parts. 1) Understanding the problem statement. 2) Building the logic to solve the problem. 3) Coding the logic using java (you can use your own preferred programming language). 🔴 Subscribe for more hackerrank tutorials: / @javaaidtutorials 👍 Like us on Facebook: / hackerranksolutiontutorials 💎Share this video with a YouTuber friend: • How To Solve Plus Minus HackerRank Pr… ✚ Join our community ► 👉 Coding interview preparation group: / codingip 👉 Telegram link: https://t.me/hackerranksolutions 📖 Resources ► 👉 Problem statement: https://www.hackerrank.com/challenges … 👉 Source code: https://goo.gl/vcU9Zg ✅ Recommended playlists ► 👉 All hackerrank solutions: • How To Master In Data Structures And … 👋 Let’s Connect ► Git Hub: https://github.com/kanahaiya Twitter: / kanahaiyagupta Linked in: / kanahaiya-gupta Facebook: / coolkanahaiya Instagram: / coolkanahaiya #JAVAAID #HackerRankSolutions #HackerRankTutorials #HackerRank #JavaAidTutorials #Programming #DataStructures #algorithms #coding #competitiveprogramming #JavaAidTutorials #Java #codinginterview #problemsolving #KanahaiyaGupta #hackerrankchallenges #warmup #plusminus
Content
0.03 -> Hello guys, welcome to my youtube channel
and today we are going to solve
6.62 -> plus minus problem. So in this problem we
have given an array of integers and we
15.21 -> have to calculate which fraction of its
element are positives, which fraction of
20.189 -> its element or negative and which
fraction of its elements are zeros.
25.38 -> respectively, We have to print the
decimal value of each of the fraction on a
30.48 -> new line. So if you see an input format
we are getting
36.32 -> it contains first line contains N
38.25 -> denoting the size of the array
and second line we are getting this
42.45 -> space separated integers and in output we have
to print three lines first line
49.92 -> representing the fraction of the
positive number in an array compared to its
55.14 -> size the second line representing of the
fraction of the negative numbers in the array
60.859 -> compared to its size and third line we
have to print fraction of zeros in the array
66.479 -> compared to its size. So if you see in
input this is the N, which is equals to
72.99 -> 6 which is an array length and we have a
6 element over here and output we have
79.2 -> to print positive number fraction, negative number fraction, zeros number fraction so if you
85.38 -> see the explanation there are three
positive numbers so this is a positive
91.619 -> number
these are the positive number which is a three
95.25 -> and we have two negative numbers
you see -4 , -9 are the negative
100.619 -> numbers and we have one 0 in this array which is one times 0. So to calculate the fraction there
108.63 -> is a formula like we have to divide it by
total size and Array we have size is 6
116.579 -> so they are dividing the positive number
count by Array size which is 6, 3/6
124.74 -> which is equals to this and for
calculating the fraction for negative
129.569 -> count and dividing the negative count
divide by 6
133.03 -> length of the array and for zeros count
also, they were dividing zeros
138.97 -> count how many zeros we have one divide by
total length so we are getting this. So I
145.99 -> hope this problem is clear to you guys
if not, I suggest you just go through
151 -> once, so it will help you to understand in a better way.
So we have to basically calculate how
159.58 -> many positive counts we have, how many
negative counts we have, in an array and
165.49 -> how many zeros count we have in an arrays we have to divide it with the size
170.68 -> of the array. I hope, it's clear now let's
start coding. yep let me change the
179.53 -> format so this is a array, first we'll take
a variable len and store the array's length
187.57 -> Now there is a one
important thing that which we have to
196.72 -> take care we have to print the output in
this format after decimal there is 6 digit which
204.25 -> they have already mentioned clearly
their test cases are scaled up to six
209.14 -> decimal places, so you have to take care
of this and we have to print the decimal
215.02 -> So definitely we are going to take
variable the decimal or we can take float
218.98 -> So I think float is fine, so we will take
float positive count
228.9 -> so we have a variable called
231.78 -> positive count which will calculate, how
many positive numbers are there in an
235.24 -> array we have a number called negative
count which will calculate the negative
242.02 -> count in an array.
We have a zeros count. we will calculate
247.96 -> how many zeros are there in the array. We will be having
one for loop to traverse the array
257.01 -> guys please make sure like array
always start with 0 index. So we are
262.21 -> starting with zero and it will go up to length
271.09 -> We are extracting the element from the array.
So every time, if i equals to zero it will extract the
283.39 -> first element and will copy it over here, if
I equals 1, element will give you the
289.27 -> second element and we have to compare
this element whether it's greater than
298.57 -> zero, less than zero and equals to zero. if
it's greater than zero then we are going
303.43 -> to increase the positive count. This is
the unary operator guys which means or
311.86 -> you can write like this also whichever
is convenient to you, you can use but
326.02 -> mostly people use this. So you have to
check, if element is less than zero we
336.31 -> have to increment the negative count and
if element is equals to zero we have to
352.15 -> increment zeros count. So once we have
calculated individual counts how many
358.69 -> positive ,how many negative and how many
zeros count we have now what we have to do now
364.54 -> we have to just divide first, what we
have done we have just calculated the
368.44 -> individual count so after that you can
see what they are doing there is
372.97 -> dividing by total length. once this for
loop is ended we will be having all the
380.2 -> counts in the respective variables I
mean it will just print it after
386.17 -> dividing
393.18 -> one thing guys this unit take care, here
we have to print the output in such a format so I
399.37 -> am using printf for formatting and let me
tell you what it does? it's
414.91 -> print the first 'f' of floating
floating number. '1' before decimal
421.39 -> It means, it will print one digit before decimal and 6 digit after
426.22 -> decimal and this '\n' is for new line
after printing it will go to the new
431.47 -> line so first we need to print how many
positive fractions are there
436.15 -> so take the positive count divided by
total. total we have length is len
444 -> and same we have to do for negative
count
453.99 -> let me format this yeah, so here we have
to use negative count here we have to
466.48 -> use zeroCount. So we have to print positive count fraction, negative fraction and
473.65 -> zero fraction. so let's run this.
482.56 -> so its getting failed unknown format
486.94 -> So I think in formatting some error is there.
497.11 -> okay right there is a space. ya now it's fine.
So test case got passed now we'll try to
511.85 -> submit our solution.
522.02 -> you can see guys
solution is accepted that's all for today
527.68 -> thanks for watching and if you really
liked this video. please do subscribe my channel
Source: https://www.youtube.com/watch?v=aLS4HYPfzUw