# Plus Minus Hackerrank Solution C++ | Hackerrank Solutions

Plus Minus Hackerrank Solution C++ or Hackerrank plus minus solution or Plus minus Hackerrank Solution in CPP or Hackerrank C++ solutions. Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal.

Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with an absolute error of up to (10pow-4) are acceptable.

Function Description

Complete the plusMinus function in the editor below.

plusMinus has the following parameter(s):

int arr[n]: an array of integers

Print

Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with 6 digits after the decimal. The function should not return a value.

Input Format

The first line contains an integer,n, the size of the array.
The second line contains n space-separated integers that describe arr[n].

Hence we did it we got a solution.

## Plus Minus Hackerrank Solution C++

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

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
*Complete the 'plusMinus' function below.
*
*The function accepts INTEGER_ARRAY arr as parameter.
*/

void plusMinus(vector<int> arr)
{
/*Code Start From Here*/

double sp, sn, sz, pa = 1.0 / arr.size();
sp = sn = sz = 0;
for (int i = 0; i < arr.size(); i++)
{
if (arr[i] > 0) sp += pa;
else if (arr[i] < 0) sn += pa;
else sz += pa;
}

cout << setprecision(6) << fixed;
cout << sp << endl;
cout << sn << endl;
cout << sz << endl;
}

/*Code End From Here*/

int main()
{
string n_temp;
getline(cin, n_temp);

int n = stoi(ltrim(rtrim(n_temp)));

string arr_temp_temp;
getline(cin, arr_temp_temp);

vector<string> arr_temp = split(rtrim(arr_temp_temp));

vector<int> arr(n);

for (int i = 0; i < n; i++)
{
int arr_item = stoi(arr_temp[i]);

arr[i] = arr_item;
}

plusMinus(arr);

return 0;
}

string ltrim(const string &str)
{
string s(str);

s.erase(		s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int> (isspace)))
);

return s;
}

string rtrim(const string &str)
{
string s(str);

s.erase(		find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int> (isspace))).base(),
s.end()
);

return s;
}

vector<string> split(const string &str)
{
vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos)
{
tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));

}``````

### Hackerrank Plus Minus Solution in C++

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 putting 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 the solution divide all three count variables by a total number of a variable taken from input time in an array or vector. See the explanation with an example for a better understanding.

First, put the condition if the number is greater than zero then 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 then increase a count2 variable by 1 each time.

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

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

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

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

``````#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;
}``````

### The Output Plus Minus Hackerrank Solution

Now we have all three count values or we also know the total number of values in an array, so just print the below statement. Here n is a total number of an element in an array. Check C++ Homework Help.
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.