# HackerRank Apple and Orange Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Apple and Orange Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

## HackerRank Apple and Orange

### Problem

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  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 [s, t])?

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

Output Format

Print two integers on two different lines:

1. The first integer: the number of apples that fall on Sam’s house.
2. 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 2 = 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.

## HackerRank Apple and Orange Solution

### Apple and Orange Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int s;
int t;
scanf("%d %d",&s,&t);
int a;
int b;
scanf("%d %d",&a,&b);
int m;
int n, res1 = 0, res2 = 0;
scanf("%d %d",&m,&n);
int *apple = malloc(sizeof(int) * m);
for(int apple_i = 0; apple_i < m; apple_i++){
scanf("%d",&apple[apple_i]);
apple[ apple_i ] += a;
res1 += ( apple[ apple_i ] >= s && apple[ apple_i ] <= t )?1:0;
}
int *orange = malloc(sizeof(int) * n);
for(int orange_i = 0; orange_i < n; orange_i++){
scanf("%d",&orange[orange_i]);
orange[ orange_i ] += b;
res2 += ( orange[ orange_i ] >= s && orange[ orange_i ] <= t)?1:0;
}

printf("%d\n%d", res1, res2);

return 0;
}

### Apple and Orange Solution in Cpp

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <ctime>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
const double eps = 1e-8;
const int maxn = 112345;

int main()
{
//fwrite("");

int s,t,a,b,m,n,x;
int aa,bb;
aa = bb = 0;

scanf("%d%d%d%d%d%d",&s,&t,&a,&b,&m,&n);
while(m--)
{
scanf("%d",&x);
if(s <= a+x && a+x <= t) aa++;
}

while(n--)
{
scanf("%d",&x);
if(s <= b+x && b+x <= t) bb++;
}

printf("%d\n%d\n",aa,bb);

return 0;
}

### Apple and Orange Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int t = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
int m = in.nextInt();
int n = in.nextInt();
int[] apple = new int[m];
int app = 0;
for(int apple_i=0; apple_i < m; apple_i++){
apple[apple_i] = in.nextInt();
if (a + apple[apple_i] >= s && a + apple[apple_i] <= t) {
app++;
}
}
int[] orange = new int[n];
int or = 0;
for(int orange_i=0; orange_i < n; orange_i++){
orange[orange_i] = in.nextInt();
if (b + orange[orange_i] >= s && b + orange[orange_i] <= t) {
or++;
}
}

System.out.println(app);
System.out.println(or);
}
}

### Apple and Orange Solution in Python

s,t = map(int, input().strip().split(' '))
a,b = map(int, input().strip().split(' '))
m,n = map(int, input().strip().split(' '))
apple = list(map(int, input().strip().split(' ')))
orange = list(map(int, input().strip().split(' ')))

sapple = sum([1 for f in apple if (f+a) >= s and (f+a) <= t])
sorange = sum([1 for f in orange if (f+b) >= s and (f+b) <= t])

print (sapple)
print (sorange)

### Apple and Orange Solution using JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
input_stdin += data;
});

process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});

return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
var s = parseInt(s_temp[0]);
var t = parseInt(s_temp[1]);
var a = parseInt(a_temp[0]);
var b = parseInt(a_temp[1]);
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
apple = apple.map(Number);
orange = orange.map(Number);
var x = 0, y = 0;

for (var i=0;i<apple.length;i++) {
if (apple[i]+a >= s && apple[i]+a <= t) {
x++;
}
}
for (var i=0;i<orange.length;i++) {
if (orange[i]+b >= s && orange[i]+b <= t) {
y++;
}
}
console.log(x);
console.log(y);
}

### Apple and Orange Solution in Scala

object Solution {

def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
var s = sc.nextInt();
var t = sc.nextInt();
var a = sc.nextInt();
var b = sc.nextInt();
var m = sc.nextInt();
var n = sc.nextInt();
var apple = new Array[Int](m);
for(apple_i <- 0 to m-1) {
apple(apple_i) = sc.nextInt();
}
var orange = new Array[Int](n);
for(orange_i <- 0 to n-1) {
orange(orange_i) = sc.nextInt();
}

val ax = apple.map(a + _).filter(x => x >= s && x <= t).size;
val ox = orange.map(b + _).filter(x => x >= s && x <= t).size;

println(ax)
println(ox)

}
}

### Apple and Orange Solution in Pascal

Var
s,t,a,b,m,n,d,i,z:longint;
Begin
z:=0;
For i:=1 to m do
begin
if (a+d>=s) and (a+d<=t) then inc(z);
end;
Writeln(z);
z:=0;
For i:=1 to n do
begin
if (b+d>=s) and (b+d<=t) then inc(z);
end;
Writeln(z);
End.

Disclaimer: This problem (Apple and Orange) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

### FAQ:

#### 1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

#### 2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

#### 3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

#### 4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

#### 5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.

#### 6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.

#### 7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.