Plus Minus

Sort by

recency

|

221 Discussions

|

  • + 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));
    
    }
    
  • + 0 comments

    function plusMinus(arr) {

        const total = arr.length;
    
        const totalArray = [0, 0, 0];
    
            arr.forEach((item) => {
    
                if (item === 0) {
    
                totalArray[2]++;
    
            } else if (item > 0) {
    
      totalArray[0]++;
    
    } else {
    
      totalArray[1]++;
    
    }
    

    });

    totalArray.forEach((count) => {

    const newItem = count / total;
    
    console.log(newItem.toFixed(6));
    

    plusMinus([-4, 3, -9, 0, 4, 1]);