Plus minus -HackerRank solution in c,c++,python,java

 plus minus hackerrank solution in c,c++,python , java 


Problem statement - 

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

Explanation

There are  positive numbers,  negative numbers, and  zero in the array.
The proportions of occurrence are positive: , negative:  and zeros: .


Solution :

Code in c :

#include<stdio.h>

int main ()
{
    int n;
    scanf("%d",&n);
    int i,arr[n],p=0,neg=0,z=o;
    for ( i = 0i < ni++)
    {
        scanf("%d",&arr[i]);
        if ( arr[i]>0 )
        {
            p++;
        }
        else if ( arr[i] < 0 )
        {
            neg++;
        }
        else if ( arr[i]== 0)
        {
            z++;
        }
        
    }
    float pos,ne,zer;
    pos=(float)p/n;
    ne=(float)neg/n;
    zer=(float)z/n;
   printf("%f \n %f \n %f",pos,ne,zer);
}

Code in c++ :

#include<iostream>
#include<iomanip>
using namespace std ;
int main ()
{
    int n;
    cin>>n;
    int i,arr[n],p=0,neg=0,z=0;
    for ( i = 0i < ni++)
    {
        cin>>arr[i];
        if ( arr[i]>0 )
        {
            p++;
        }
        else if ( arr[i] < 0 )
        {
            neg++;
        }
        else if ( arr[i]== 0)
        {
            z++;
        }
        
    }
    float pos,ne,zer;
    pos=(float)p/n;
    ne=(float)neg/n;
    zer=(float)z/n;
    cout<<fixed<<setprecision(6)<<pos<<endl;
    cout<<fixed<<setprecision(6)<<ne<<endl;
    cout<<fixed<<setprecision(6)<<zer<<endl;
}

Code in python :

def plusminus(n):
    k=map(int,input().split())
    p=0
    ne=0
    z=0
    for i in k:
        if i>0:
            p+=1
        elif i==0:
            z+=1
        elif i<0:
            ne+=1
    print (round((p/n),3))
    print (round((ne/n),3))
    print (round((z/n),3))
    
n=int(input())
plusminus(n)


Code in java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int pos = 0;
        int zero = 0;
        int neg = 0;
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
            if (x > 0) {
                pos++;
            } else if (x == 0) {
                zero++;
            } else {
                neg++;
            }
        }
        System.out.println(pos / (double) n);
        System.out.println(neg / (double) n);
        System.out.println(zero / (double) n);
    }
}





Please comment if you have any doubt or for any other problem .
Thanks

Post a Comment

0 Comments