# Apple and Oranges HackerRank Solution in C, C++, Java, Python

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  is the start point, and  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 , and the orange tree is at point .
• When a fruit falls from its tree, it lands  units of distance from its tree of origin along the -axis. *A negative value of  means the fruit fell  units to the tree’s left, and a positive value of  means it falls  units to the tree’s right.

Given the value of  for  apples and  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  and . The apple tree is located at  and the orange at . There are  apples and  oranges. Apples are thrown  units distance from , and  units distance. Adding each apple distance to the position of the tree, they land at . Oranges land at . One apple and two oranges land in the inclusive range  so we print

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

The second line contains two space-separated integers denoting the respective values of  and .

The third line contains two space-separated integers denoting the respective values of  and .

The fourth line contains  space-separated integers denoting the respective distances that each apple falls from point .

The fifth line contains  space-separated integers denoting the respective distances that each orange falls from point .

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.

## Apple and Oranges HackerRank 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;
int ans1=0,ans2=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]);
if((apple[apple_i]+a)<=t && (apple[apple_i]+a)>=s)
ans1++;
}
int *orange = malloc(sizeof(int) * n);
for(int orange_i = 0; orange_i < n; orange_i++){
scanf("%d",&orange[orange_i]);
if((orange[orange_i]+b)<=t && (orange[orange_i]+b)>=s)
ans2++;
}
printf("%d\n%d",ans1,ans2);
return 0;
}```

## Apple and Oranges HackerRank Solution in C++

```#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 Oranges HackerRank 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 Oranges HackerRank Solution in Python

```#!/bin/python

import sys

s,t = raw_input().strip().split(' ')
s,t = [int(s),int(t)]
a,b = raw_input().strip().split(' ')
a,b = [int(a),int(b)]
m,n = raw_input().strip().split(' ')
m,n = [int(m),int(n)]
apple = map(int,raw_input().strip().split(' '))
orange = map(int,raw_input().strip().split(' '))
acount=0
for i in apple:
if s<=a+i<=t:
acount+=1
ocount=0
for i in orange:
if s<=b+i<=t:
ocount+=1
print acount
print ocount```

## Apple and Oranges HackerRank Solution in C#

```using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

static void Main(String[] args) {
int s = Convert.ToInt32(tokens_s);
int t = Convert.ToInt32(tokens_s);
int a = Convert.ToInt32(tokens_a);
int b = Convert.ToInt32(tokens_a);
int m = Convert.ToInt32(tokens_m);
int n = Convert.ToInt32(tokens_m);
int[] apple = Array.ConvertAll(apple_temp,Int32.Parse);
int[] orange = Array.ConvertAll(orange_temp,Int32.Parse);
int apples = 0;
for(int i=0; i<m; i++) {
if(a + apple[i] >= s && a+apple[i] <= t) apples++;
}
int oranges = 0;
for(int i=0; i<n; i++) {
if(b + orange[i] >= s && b+orange[i] <= t) oranges++;
}
Console.WriteLine(apples);
Console.WriteLine(oranges);
}
}```

Attempt – Apple and Oranges HackerRank Challenge

Next HackerRank Challenge Solution