Monday, 3 April 2017

Hacker Rank solution for Apple and Orange in C++

Problem:- apple and orange hackerrank solution or Apple and Orange solution or Apple and Orange solution in C++ or Apple and Orange hacker rank solution in Java or Apple and Orange hacker rank solution c++ or Apple and Orange hacker rank solution c++ or Apple and Orange program in c or Apple and Orange solution in c or Apple and Orange hacker rank solution in python or Apple and Orange in java or Apple and Orange hacker rank solution in c or Hacker Rank Solution For Apple and Orange in C++ or Apple and Orange in C++ or Hacker Rank solution for Apple and Orange Pairs in C++.

Check This- Hacker rank Solution for C++ Sub Domain Introduction, Classes, STL, Inheritance.

Logic:- divisible pairs sum problem has no logic just put the condition as problem requirement, see the below explanation section for full understanding.

Explanation:- Let's take an example of hacker rank and try to solve the problem step by step. Below is a hacker rank input and we are implementing hacker input for better understanding.

7 11
5 15
3 2
-2 2 1
5 -6

So according to our solution just fit the above input in our solution.

s = 7 
t = 11
a = 5
b = 15
m = 3
n = 2

Now remaining last two line input is for an apple falls and an orange falls.

An apple falls(ap) = -2, 2, 1

An orange falls(ora) = 5, -6

Example:- Now put all these inputs in condition

if(a+ap >= s && a+ap <= t)
apcnt++;

1. (5 + (-2) >= 7 and 5 + (-2) <= 11) Condition False
2. (5 + 2 >= 7 and 5 + 2 <= 11) Condition True ====>> apcnt = 1.
3. (5 + 1 >= 7 and 5 + 1 <= 11) Condition False

Here is an answer of an apple falls = 1.

Example:- if condition true then increase an apple count same for an orange count


if(b+ora >= s && b+ora <= t)

orcnt++;

1. (15 + 5 >= 7 and 15 + 5 <= 11) Condition False
2. (15 + (-6) >= 7 and 15 + (-6) <= 11) Condition True =====>> orcnt = 1.

Here is an answer of an orange falls = 1.

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

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen c++ editor in the top right drop down option.

Submit your solution here:- Click here

Solution:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int s,t,a,b,m,n,ap,apcnt = 0,ora,orcnt = 0;
    
    cin >> s >> t >> a >> b >> m >> n;
    
    for(int i = 0;i < m;i++)
{
        cin >> ap;
        if(a+ap >= s && a+ap <= t)apcnt++;
    }
    
    for(int i = 0;i < n;i++)
{
        cin >> ora;
        if(b+ora >= s && b+ora <= t)orcnt++;
    }
    cout << apcnt <<endl<< orcnt << endl;
    return 0;
}

Output:-


apple and orange hackerrank solution in C++

Hacker Rank solution for Apple and Orange in C++


You May Like This:-

1. Hacker Rank Solution for 30 Days of Code

2. Hacker Rank solution for Attribute Parser

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

4. Hacker Rank Solution For Conditional Statements

5. Hacker Rank Solution For Mini-Max Sum

6. Hacker Rank Solution For Mini-Max Sum In C++

7. Hacker Rank Solution For Birthday Cake Candles

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

9. Hacker Rank Solution For Strings

10. Hacker Rank Solution For Conditional Statements


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment