# 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.

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

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.