HackerRank Plus Minus problem solution | 1 Week Prepartion kit

In this HackerRank Plus Minus (1 Week preparation kit) problem solution we have Given an array of integers, and calculated the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal.

HackerRank Plus Minus problem solution | 1 Week Prepartion kit


Problem solution in Python.

#!/bin/python3

import math
import os
import random
import re
import sys

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

def plusMinus(arr):
    # Write your code here
    pos = 0
    neg = 0
    zer = 0
    for i in arr:
        if i > 0:
            pos += 1
        if i < 0:
            neg += 1
        if i == 0:
            zer += 1
    print(round(pos/len(arr),6))
    print(round(neg/len(arr),6))
    print(round(zer/len(arr),6))
    
if __name__ == '__main__':
    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    plusMinus(arr)



Problem solution in Java.

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 pos = 0;
    int neg = 0;
    int zero = 0;
    int n = arr.size();
for(int number = 0; number < n; number++){
    if(arr.get(number) == 0){
        zero++;
    }else if(arr.get(number) > 0){
        pos++;
    }else{
        neg++;
    }
}
    double posValue = (double)pos/n;
    double negValue = (double)neg/n;
    double zeroValue = (double)zero/n;
 
    System.out.println(posValue);
    System.out.println(negValue);
    System.out.println(zeroValue);

    }

}

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();
    }
}



Problem solution in C++.

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

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

void plusMinus(vector<int> arr)
{
    int positive = 0;
    int negative = 0;
    int zero = 0;
    
    for(int i : arr)
    {
        if(i < 0)
            negative++;
        else if(i > 0)
            positive++;
        else
            zero++;
    }
    
    float arrSize = static_cast<float>(arr.size());
        
    std::cout
    << std::fixed
    << std::setprecision(6)
    << static_cast<float>(positive) / arrSize << std::endl
    << static_cast<float>(negative) / arrSize << std::endl
    << static_cast<float>(zero) / arrSize;
}

int main()
{
    string n_temp;
    getline(cin, n_temp);

    int n = stoi(ltrim(rtrim(n_temp)));

    string arr_temp_temp;
    getline(cin, arr_temp_temp);

    vector<string> arr_temp = split(rtrim(arr_temp_temp));

    vector<int> arr(n);

    for (int i = 0; i < n; i++) {
        int arr_item = stoi(arr_temp[i]);

        arr[i] = arr_item;
    }

    plusMinus(arr);

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}

vector<string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}



Problem solution in C.

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void plusMinus(int , int []);
int main()
{
     int n, arr[100];
     scanf("%d",&n);
     for(int i=0;i<n;i++)
     {
         scanf("%d",&arr[i]);
     }
     plusMinus(n,arr);
     
 return 0;
}
void plusMinus(int n, int arr[]){
     int pos=0,neg=0,zero=0,i;
     
     float posr,negr,zeror;
     for( i=0;i<n;i++){
         if(arr[i]>0)
         pos++;
         else if(arr[i]<0)
         neg++;
         else 
         zero++;
        
     }
     posr=(float)pos/n;
     negr=(float)neg/n;
     zeror=(float)zero/n;
     printf("%f\n%f\n%f",posr,negr,zeror);
 }


Post a Comment

0 Comments