--> "Plus Minus" HackerRank Solution » Using Java

Plus Minus – Java HackerRank Solution

plus minus pic

Objective:

This plus minus problem involves basic counting and arithmetic operations to determine the proportions of different types of elements in an array. Calculate the fractions of positive, negative, and zero elements in an array.

Problem Statement:

You need to write a function that takes an array of integers and prints the fractions of its elements that are positive, negative, and zeros with six decimal places.

Input Format:

  • The first line contains an integer n, the number of elements in the array.
  • The second line contains n space-separated integers representing the elements of the array.

Output Format:

  • Print three lines, each to six decimal places:
    1. The fraction of positive numbers in the array.
    2. The fraction of negative numbers in the array.
    3. The fraction of zeroes in the array.
Example 1:
  1. Input:
    • n = 6
    • array = [-4, 3, -9, 0, 4, 1]
  2. Output:
    0.500000
    0.333333
    0.166667
    • Explanation: There are 3 positive numbers (3, 4, 1), 2 negative numbers (-4, -9), and 1 zero (0). The fractions are calculated as follows:
      • Positive fraction: 3/6 = 0.500000
      • Negative fraction: 2/6 = 0.333333
      • Zero fraction: 1/6 = 0.166667
Example 2:
  1. Input:
    • n = 4
    • array = [1, 2, 3, -1]
  2. Output:
    0.750000
    0.250000
    0.000000
    • Explanation: There are 3 positive numbers (1, 2, 3), 1 negative number (-1), and no zeros. The fractions are:
      • Positive fraction: 3/4 = 0.750000
      • Negative fraction: 1/4 = 0.250000
      • Zero fraction: 0/4 = 0.000000
Example 3:
  1. Input:
    • n = 3
    • array = [0, 0, 0]
  2. Output:
    0.000000
    0.000000
    1.000000
    • Explanation: All elements are zero. The fractions are:
      • Positive fraction: 0/3 = 0.000000
      • Negative fraction: 0/3 = 0.000000
      • Zero fraction: 3/3 = 1.000000

Steps to Solve:

  1. Read the Input: Capture the number of elements and the array itself.
  2. Initialize Counters: Set counters for positive, negative, and zero values to 0.
  3. Iterate and Count: Traverse the array and update the counters based on the element values.
  4. Calculate Fractions: Compute the fractions by dividing the counters by the total number of elements.
  5. Print Results: Output the fractions to six decimal places.
plus minus page 2
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'plusMinus' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static void plusMinus(List<Integer> arr) {
    // Write your code here
        int positive = 0, negative = 0, zero = 0, length = arr.size();
        for(int i : arr){
            if(i > 0) positive++;
            else if(i < 0) negative++;
            else zero++;
        }
        System.out.println((double)positive/length);
        System.out.println((double)negative/length);
        System.out.println((double)zero/length);
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        Result.plusMinus(arr);

        bufferedReader.close();
    }
}
The Problem Solution

Leave a Reply

Your email address will not be published. Required fields are marked *