Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero.

2 0
Read Time:2 Minute, 15 Second

Given an array of integers, calculate the ratios of its elements that are positivenegative, 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

There are  elements, two positive, two negative and one zero. Their ratios are ,  and . Results are printed as:

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

Output Format

Print the following  lines, each to  decimals:

  1. proportion of positive values
  2. proportion of negative values
  3. 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

-----------------------------------------xxxxxxxx-----------------------------

Java Code

2021
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) {
   // porpostion  of positive numbers 
        // porpostion of negative  numbers
        // zeros
        int positive_numbers=0;
        int negative_numbers=0;
        int zero_numbers=0;
        int total_numbers=arr.size();
        
     for(Integer var: arr) {
             if(var ==0) {
                 zero_numbers+=1;
             }
             else if(var <0) {
                 negative_numbers+=1;
             }else {
                 positive_numbers+=1;
             }
         }
     double positive=(double)positive_numbers/total_numbers; 
     double negative=(double)negative_numbers/total_numbers; 
     double zeros=(double)zero_numbers/total_numbers; 
     
     System.out.println(String.format("%.6f",positive));
     System.out.println(String.format("%.6f",negative));
     System.out.println(String.format("%.6f",zeros));
    

    }

}

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();
    }
}
Happy
Happy
30 %
Sad
Sad
20 %
Excited
Excited
10 %
Sleepy
Sleepy
10 %
Angry
Angry
30 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
50%
4 Star
25%
3 Star
0%
2 Star
25%
1 Star
0%

84 thoughts on “Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero.

  1. After I originally commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I recieve four emails with the exact same comment. Is there a means you are able to remove me from that service? Cheers!

  2. We are a group of volunteers and opening a new scheme in our community. Your site offered us with valuable info to work on. You have done a formidable job and our whole community will be thankful to you.

  3. I visit each day a few sites and blogs to read content, but this blog offers quality based writing. I like the valuable information you provide in your articles. I’ll bookmark your weblog and check again here frequently. I’m quite certain I will learn lots of new stuff right here! Best of luck for the next!

  4. Hi there! I could have sworn I’ve been to this website before but after checking through some of the post I realized it’s new to me. Anyhow, I’m definitely delighted I found it and I’ll be book-marking and checking back often!

  5. You can definitely see your enthusiasm within the paintings you write. The arena hopes for even more passionate writers like you who are not afraid to say how they believe. At all times go after your heart.

  6. Thanks for the different tips shared on this weblog. I have seen that many insurance firms offer customers generous special discounts if they favor to insure many cars together. A significant variety of households have several cars or trucks these days, in particular those with older teenage kids still located at home, as well as the savings upon policies may soon mount up. So it will pay to look for a good deal.

  7. Thanks for another magnificent article. Where else could anybody get that type of info in such an ideal way of writing? I have a presentation next week, and I’m on the look for such info.

  8. Excellent weblog here! Additionally your website a lot up very fast! What web host are you the use of? Can I get your affiliate link for your host? I want my website loaded up as quickly as yours lol

  9. I’m not sure why but this web site is loading very slow for me. Is anyone else having this problem or is it a issue on my end? I’ll check back later on and see if the problem still exists.

  10. Great ?I should certainly pronounce, impressed with your website. I had no trouble navigating through all the tabs as well as related info ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Quite unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your customer to communicate. Excellent task..

  11. Hey there, I think your site might be having browser compatibility issues. When I look at your website in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, amazing blog!

  12. Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards. Putlocker

  13. I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business. FMovies

  14. I do not know whether it’s just me or if everybody
    else experiencing issues with your website. It seems like some of the text in your posts are running
    off the screen. Can somebody else please comment and let me know if this is happening to them as well?
    This may be a issue with my internet browser because I’ve had
    this happen before. Thanks

  15. Appreciating the hard work you put into your website and in depth information you offer.
    It’s good to come across a blog every once in a while that isn’t the same
    old rehashed material. Excellent read! I’ve saved your site
    and I’m adding your RSS feeds to my Google account.

  16. Somebody essentially help to make seriously posts I would state. This is the very first time I frequented your web page and thus far? I surprised with the research you made to make this particular publish extraordinary. Magnificent job!

Leave a Reply

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