Plus Minus

Sort by

recency

|

222 Discussions

|

  • + 1 comment

    void plusMinus(vector arr) { int positive = 0, negative = 0, zero = 0; int n = arr.size();

    for (int num : arr) {
        if (num > 0) positive++;
        else if (num < 0) negative++;
        else zero++;
    }
    
    cout << fixed << setprecision(6);
    cout << (double)positive / n << endl;
    cout << (double)negative / n << endl;
    cout << (double)zero / n << endl;
    

    }

  • + 0 comments

    JavaScript Solution

    function plusMinus(arr) {
        let [pos, neg, zer] = [0,0,0];
        for(let i = 0; i < arr.length; i++){
            if(arr[i] > 0)pos++;
            else if(arr[i] < 0)neg++;
            else if(arr[i] === 0) zer++;
        }
        let ratios = [pos, neg, zer];
        ratios.forEach(val => {
            console.log((val/arr.length).toFixed(6));
        })
    }
    
  • + 0 comments

    here is a solutions with java

    int zeros = 0; int positives = 0; int negatives = 0;

    for(int numbers : arr){
        if(numbers == 0){
            positives ++;
        }else if(numbers < 0){
            negatives ++;
        }else{
            zeros++;
        }
    }
    int total = arr.size();
    
    double proportionPositives = (double) positives / total;
    double proportionNegatives = (double) negatives/ total;
    double proportionzeros = (double) zeros / total;
    
    System.out.printf("%.6f\n", proportionzeros);
    System.out.printf("%.6f\n", proportionNegatives);
    System.out.printf("%.6f\n", proportionPositives);
    
  • + 1 comment

    **here is python code for this problem ** def plusMinus(arr): # Write your code here lent = len(arr) zer,pos,neg = [],[],[] for i in arr: if i > 0: pos.append(i) elif i<0: neg.append(i) else: zer.append(i) p=float(len(pos))/lent n=float(len(neg))/lent z=float(len(zer))/lent

    print(f"{p:6f}")   
    print(f"{n:6f}")   
    print(f"{z:6f}") 
    
  • + 0 comments
    function plusMinus(arr) {
        let n=arr.length;
        let max=0,min=0,zero=0,i;
        for(i=0; i<n; i++){
            if(0<arr[i]){
                max++;
            }
            else if(0>arr[i]){
                min++;
            }
            else if (arr[i]===0){
                zero++;
            }
        }
        
            console.log((max / n).toFixed(6));
            console.log((min/n).toFixed(6));
            console.log((zero/n).toFixed(6));
    
    }