Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Plus Minus in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Algorithm
The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.
Advantages of Algorithms:
- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.
Link for the Problem – Plus Minus – Hacker Rank Solution
Plus Minus– Hacker Rank Solution
Problem:
Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.
Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to are acceptable.
Example
![Plus Minus in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 image 41](https://technorj.com/wp-content/uploads/2021/12/image-41.png)
0.400000 0.400000 0.200000
Function Description
Complete the plusMinus function in the editor below.
plusMinus has the following parameter(s):
- int arr[n]: an array of integers
Print
Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with digits after the decimal. The function should not return a value.
Input Format
The first line contains an integer, , the size of the array.
The second line contains space-separated integers that describe .
Constraints
![Plus Minus in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 3 image 43](https://technorj.com/wp-content/uploads/2021/12/image-43.png)
Output Format
Print the following lines, each to decimals:
- proportion of positive values
- proportion of negative values
- proportion of zeros
Sample Input
STDIN Function ----- -------- 6 arr[] size n = 6 -4 3 -9 0 4 1 arr = [-4, 3, -9, 0, 4, 1]
Sample Output
0.500000 0.333333 0.166667
Explanation
![Plus Minus in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 4 image 42](https://technorj.com/wp-content/uploads/2021/12/image-42.png)
Plus Minus – Hacker Rank Solution
import java.util.Scanner; /** * * @author Techno-RJ * */ public class PlusMinus { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int arr[] = new int[n]; float countPositive = 0; float countNegetive = 0; float countZero = 0; for (int arr_i = 0; arr_i < n; arr_i++) { arr[arr_i] = in.nextInt(); if (arr[arr_i] < 0) { countNegetive++; } if (arr[arr_i] > 0) { countPositive++; } if (arr[arr_i] == 0) { countZero++; } } System.out.printf("%1.6f \n", countPositive / n); System.out.printf("%1.6f \n", countNegetive / n); System.out.printf("%1.6f \n", countZero / n); in.close(); } }