4/02/2017

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.


Submit your solution here:- Click here

Tip:- Add setprecision(6) before printing the output in screen cause we have to print our solution up to 6 decimal digit. Before copy the solution I recommended please read this full article, this will help you to build your own logic. 

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:-

Hacker Rank solution for Plus Minus in C++


You May Also Like:-

1. C Program For Find A Grade Of Given Marks Using Switch Case

2. C++ Program For Store Employee Information And Display Using Structure

3. C Program For Finding Radius Circumference Using Switch Case

4. C Program For Calculator Using Switch Case

5. Java Program For Find The Gross Salary Of An Employee

6. Hacker Rank Solution For Birthday Cake Candles

7. C++ Program For Calculate Percentage Of 5 Subjects

8. Hacker Rank Solution For Strings

9. Hacker Rank Solution For Conditional Statements

10. Hacker Rank solution for 30 Days Of Code

No comments:

Post a Comment