Plus Minus

Sort by

recency

|

210 Discussions

|

  • + 1 comment

    My code seems to work perfectly well on my machine for the same arrays as used in the test cases, but on Hackerrank they return erros. Anybody faced this before?

    def plusMinus(arr): n = len(arr) pos = 0 neg = 0 zero = 0

    for i in range(0,n):
        if arr[i]>0:
            pos +=1
        elif arr[i]<0:
            neg +=1
        elif arr[i]==0:
            zero +=1
    pos_ratio = pos/n
    neg_ratio = neg/n
    zero_ratio = zero/n
    print('positive ratio:', f'{pos_ratio:.6f}')
    print('negative ratio:', f'{neg_ratio:.6f}')
    print('zeros ratio:', f'{zero_ratio:.6f}')
    
  • + 0 comments
    #include <bits/stdc++.h>
    using namespace std;
    
    void plusMinus(vector<int> arr) {
        int n = arr.size(), pos = 0, neg = 0, zero = 0;
        for (int x : arr) pos += x > 0, neg += x < 0, zero += x == 0;
        cout << fixed << setprecision(6) << (double)pos / n << endl << (double)neg / n << endl << (double)zero / n << endl;
    }
    
    int main() {
        int n; cin >> n;
        vector<int> arr(n);
        for (int i = 0; i < n; i++) cin >> arr[i];
        plusMinus(arr);
        return 0;
    }
    
  • + 2 comments

    how to solve this plus minus problem

  • + 0 comments

    typescript/javascript

    console.log((arr.filter(el => el > 0).length/arr.length).toFixed(6)); console.log((arr.filter(el => el < 0).length/arr.length).toFixed(6)); console.log((arr.filter(el => el === 0).length/arr.length).toFixed(6));

  • + 3 comments
    public static void plusMinus(List<int> arr)
        {
            int n = arr.Count;
            if (n > 0 && n <= 100)
            {
                int positives = arr.Count(x => x > 0);
                int negatives = arr.Count(x => x < 0);
                int zeros = arr.Count(x => x == 0);
                
                // Calculate and print ratios with 6 decimal places
                Console.WriteLine("{0:F6}", (decimal)positives / n);
                Console.WriteLine("{0:F6}", (decimal)negatives / n);
                Console.WriteLine("{0:F6}", (decimal)zeros / n);
            }
        }