07
Jan
2020

Hackerrank Plus Minus (C++)

C++ :

void plusMinus(vector<int> arr) {
    int pos = 0, neg = 0, zero = 0;
    int size = arr.size();
    for(int i = 0; i < size; i++){
        if(arr[i] < 0){
            neg++;
        }else if(arr[i] > 0){
            pos++;
        }else{
            zero++;
        }
    }
    
    cout << ((double)pos/size) << endl << ((double)neg/size) << endl << ((double)zero/size) << endl;
}

Explanation:

pos is the number of positive integers in the vector, neg is the number of negative integers in the vector, and zero is the number of zeros in the vector. For the entire size of the vector, add each number accordingly by 1 if the number is positive, negative or zero. When printing, cast pos, neg and zero to double so that the output is not an integer division.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *