# HackerRank solution for Plus Minus in C++

Problem:- Plus Minus solution in C++ or  Plus Minus hacker rank solution in Java or Plus Minus hacker rank solution c++ or plus-minus hacker rank solution c++ or Plus Minus program in c or Plus Minus solution in c or Plus Minus hacker rank solution in python or Plus-Minus in java or Plus Minus hacker rank solution in c or Hacker Rank Solution For Plus-Minus in C++ or Plus-Minus in C++ or Hacker Rank solution for Plus Minus in C++.

Logic:- Plus Minus is a very simple problem we have to just count positive number, Negative number, and Number that is equal to zero, so we can do this by using an array or vector and put the three condition if the number is greater than zero increase a count and if the number is less than zero increase second count variable and if both conditions are not true that increase the third count variable. At the end of solution divide all three count variable by a total number of a variable taken from input time in an array or vector. See the explanation with an example for better understanding.

Explanation:- First put the condition if the number is greater than zero than increase the count1 variable by 1 each time.

if(arr[arr_i]>0)
{
c1++;
}

Now put the second condition if the number is less than zero than increase a count2 variable by 1 each time.

if(arr[arr_i]<0)
{
c2++;
}

And if both the conditions are not true than increase the third count or count3 variable by 1 each time.

if(arr[arr_i]==0)
{
c3++;
}

Now we got the all three count value or we also know the total number of value in an array so just print the given below statement. Here n is a total number of an element in an array.

print c1 / n
print c2 / n
print c3 / n

Hence we did it we got a solution.

Check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.

Solution:-

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n,c1=0,c2=0,c3=0;
cin >> n;

vector<int> arr(n);

for(int arr_i = 0;arr_i < n;arr_i++)
{
cin >> arr[arr_i];

if(arr[arr_i]>0)
{
c1++;
}
if(arr[arr_i]<0)
{
c2++;
}
if(arr[arr_i]==0)
{
c3++;
}
cout<<setprecision(6)<<(float)c1/n<<endl;
cout<<setprecision(6)<<(float)c2/n<<endl;
cout<<setprecision(6)<<(float)c3/n<<endl;
return 0;
}

Output:-

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.