# Apple And Orange Hackerrank Solution | Hackerrank Algorithms

Apple And Orange Hackerrank Solution in C++. There are m = 3 apples and n = 3 oranges. Apples are thrown apples = [2, 3, -4] units distance from a. Sam's house has an apple tree and an orange tree that yield an abundance of fruit. Using the information given below, determine the number of apples and oranges that land on Sam's house.

In the diagram below:

• The red region denotes the house, where s is the start point, and t is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right.
• Assume the trees are located on a single point, where the apple tree is at point a, and the orange tree is at point b.
• When a fruit falls from its tree, it lands d units of distance from its tree of origin along the x-axis. *A negative value of d means the fruit fell d units to the tree's left, and a positive value of d means it falls d units to the tree's right. *

Given the value of d for m apples and n oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range )?

For example, Sam's house is between s - 7 and t = 10. The apple tree is located at a = 4 and the orange at b = 12. There are m = 3 apples and n = 3 oranges. Apples are thrown apples = [2, 3, -4] units distance from a, and oranges = [3, -2, -4] units distance. Adding each apple distance to the position of the tree, they land at [4 + 2, 4 + 3, 4 + -4] = [6, 7, 0]. Oranges land at [12 + 3, 12 + -2, 12 + -4] = [15, 10, 8]. One apple and two oranges land in the inclusive range of 7 - 10 so we print

1
2

Function Description

Complete the countApplesAndOranges function in the editor below. It should print the number of apples and oranges that land on Sam's house, each on a separate line.

countApplesAndOranges has the following parameter(s):

• s: integer, starting point of Sam's house location.
• t: integer, ending location of Sam's house location.
• a: integer, location of the Apple tree.
• b: integer, location of the Orange tree.
• apples: integer array, distances at which each apple falls from the tree.
• oranges: integer array, distances at which each orange falls from the tree.

Input Format

The first line contains two space-separated integers denoting the respective values of s and t.
The second line contains two space-separated integers denoting the respective values of a and b.
The third line contains two space-separated integers denoting the respective values of m and n.
The fourth line contains m space-separated integers denoting the respective distances that each apple falls from point a.
The fifth line contains n space-separated integers denoting the respective distances that each orange falls from point b.

Constraints

1 <= s, t, a, b, m, n <= 10pow5
-10pow5 <= d <= 10pow5
a < s < t < b

Output Format

Print two integers on two different lines:

The first integer: The number of apples that fall on Sam's house.
The second integer: The number of oranges that fall on Sam's house.

Sample Input 0

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

Sample Output 0

1
1

Explanation 0

The first apple falls at position 5 - 3 = 3.
The second apple falls at position 5 + 2 = 7.
The third apple falls at position 5 + 1 = 6.
The first orange falls at position 15 + 5 = 20.
The second orange falls at position 15 - 6 = 9.
Only one fruit (the second apple) falls within the region between 7 and 11, so we print 1 as our first line of output.
Only the second orange falls within the region between 7 and 11, so we print 1 as our second line of output.

## Apple And Orange Hackerrank Solution in C++

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

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

/*
*Complete the 'countApplesAndOranges' function below.
*
*The function accepts following parameters:
*1. INTEGER s
*2. INTEGER t
*3. INTEGER a
*4. INTEGER b
*5. INTEGER_ARRAY apples
*6. INTEGER_ARRAY oranges
*/

void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges)
{
int count_apples = 0;
int count_oranges = 0;

for (int i = 0; i < apples.size(); i++)
{
apples.at(i) = apples.at(i) + a;
if (apples.at(i) >= s && apples.at(i) <= t)
{
count_apples = count_apples + 1;
}
}

for (int i = 0; i < oranges.size(); i++)
{
oranges.at(i) = oranges.at(i) + b;
if (oranges.at(i) >= s && oranges.at(i) <= t)
{
count_oranges = count_oranges + 1;
}
}

cout << count_apples << endl;
cout << count_oranges << endl;
}

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

vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));

int s = stoi(first_multiple_input[0]);

int t = stoi(first_multiple_input[1]);

string second_multiple_input_temp;
getline(cin, second_multiple_input_temp);

vector<string> second_multiple_input = split(rtrim(second_multiple_input_temp));

int a = stoi(second_multiple_input[0]);

int b = stoi(second_multiple_input[1]);

string third_multiple_input_temp;
getline(cin, third_multiple_input_temp);

vector<string> third_multiple_input = split(rtrim(third_multiple_input_temp));

int m = stoi(third_multiple_input[0]);

int n = stoi(third_multiple_input[1]);

string apples_temp_temp;
getline(cin, apples_temp_temp);

vector<string> apples_temp = split(rtrim(apples_temp_temp));

vector<int> apples(m);

for (int i = 0; i < m; i++)
{
int apples_item = stoi(apples_temp[i]);

apples[i] = apples_item;
}

string oranges_temp_temp;
getline(cin, oranges_temp_temp);

vector<string> oranges_temp = split(rtrim(oranges_temp_temp));

vector<int> oranges(n);

for (int i = 0; i < n; i++)
{
int oranges_item = stoi(oranges_temp[i]);

oranges[i] = oranges_item;
}

countApplesAndOranges(s, t, a, b, apples, oranges);

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 Apple and Orange 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;
}``````

### Similar to Apple And Orange

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.