Plus Minus

Sort by

recency

|

673 Discussions

|

  • + 0 comments

    This is my Java solution in video: https://www.youtube.com/watch?v=YRht_UcDIK4

    Time complexity is
    Space complexity is

    Please let me know what you think.

  • + 0 comments

    JavaScript Solution:

    function plusMinus(arr) { const n = arr.length;

    if(n === 0){ console.log("0.000000"); console.log("0.000000"); console.log("0.000000"); return; }

    //count positive, negative, and zero elements

    const positiveCount = arr.filter(x => x > 0).length; const negativeCount = arr.filter(x => x < 0).length; const zeroCount = arr.filter(x => x === 0).length;

    //calculate ratios const positiveRatio = positiveCount / n; const negativeRatio = negativeCount / n; const zeroRatio = zeroCount / n;

    //print results with six decimal places

    console.log(positiveRatio.toFixed(6)); console.log(negativeRatio.toFixed(6)); console.log(zeroRatio.toFixed(6));

    }

  • + 0 comments

    Javascript solution:

    function plusMinus(arr) {
       if (arr.length > 100 || Math.min(...arr) < -100 || Math.max(...arr) > 100) return
    
       let positiveCount = 0
       let negativeCount= 0
       let zeroCount =0
        
       arr.forEach(int => {
        if (Math.sign(int) === 1) ++positiveCount;
        else if (Math.sign(int) === -1 ) ++negativeCount;
        else if (Math.sign(int) === 0) ++zeroCount;
       })
       
        console.log((positiveCount / arr.length).toFixed(6))
        console.log((negativeCount / arr.length).toFixed(6))
        console.log((zeroCount / arr.length).toFixed(6))
    }
    
  • + 0 comments

    my c++ solution showing the solution code only

    *
     * Complete the 'plusMinus' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    void plusMinus(vector<int> arr) {
    int negCount{0},zeroCount{0},posCount{0};
     for (int number : arr ){
        number >0? posCount++:number<0?negCount++:zeroCount++;
     }
     double negRatio  = static_cast<double>(negCount)/arr.size();
     double posRatio  = static_cast<double>(posCount)/arr.size();
     double zeroRatio = static_cast<double>(zeroCount)/arr.size();
     cout<<std::fixed<<setprecision(6);
     cout << posRatio<<"\n"<<negRatio<<"\n"<<zeroRatio<<endl;
    
    }
    
  • + 0 comments

    My PHP Solution:

    <?php
    
    /*
     * Complete the 'plusMinus' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    function plusMinus($arr) {
        $positivi = 0;
        $negativi = 0;
        $base0 = 0;
        $dimensione = count($arr);
        
        foreach($arr as $item){
            switch(true){
                case $item > 0:
                $positivi++;
                break;
                case $item === 0:
                $base0++;
                break;
                case $item < 0:
                $negativi++;
                break;
            }
        }
    
        echo sprintf("%.6f", $positivi / $dimensione) . "\n";
        echo sprintf("%.6f", $negativi / $dimensione) . "\n";
        echo sprintf("%.6f", $base0 / $dimensione) . "\n";
    
    }
    
    $n = intval(trim(fgets(STDIN)));
    
    $arr_temp = rtrim(fgets(STDIN));
    
    $arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
    
    plusMinus($arr);