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.
Submit your solution here: Click here
Tip: Add setprecision(6) before printing the output on-screen cause we have to print our solution up to 6 decimal digits. Before copying the solution I recommended please read this full article, this will help you to build your own logic.
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));
return tokens;
}
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++;
}
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
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
Similar to Plus Minus
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
- For Loop Hackerrank Solution in C++
- Functions in C++ Hackerrank Solution
- Pointer Hackerrank Solution in C++
- Arrays Introduction Hackerrank Solution in C++
- Strings Hackerrank Solution in C++
0 Comments: