## Algorithm

Problem Name: Algorithms - Apple and Orange

In this HackerRank Functions in Algorithms - Java programming proble

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

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

For example, Sam's house is between x = 7 and t = 0. 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 apple = [2,3 -4] units distance from a , and orange = [3,-2,-4] units distance. Adding each apple distance to the position of the tree, they land at [4 + 2, 4+3, 4 + -4] = [15,10,8]. One apple and two oranges land in the inclusive range 7- 10so 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 <= 10**5
• -10**5 <= d <= 10**5
• a < s < t < 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


## Code Examples

### #1 Code Example with C Programming

Code - C Programming


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

int
isInHouse(int, int, int);

int
isInHouse(int s, int t, int d){
return ((d >= s) && (d <= t));
}

int main(){
int aIn = 0, oIn = 0;
int s;
int t;
scanf("%d %d",&s,&t);
int a;
int b;
scanf("%d %d",&a,&b);
int m;
int n;
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]);
}
int *orange = malloc(sizeof(int) * n);
for(int orange_i = 0; orange_i < n; orange_i++){
scanf("%d",&orange[orange_i]);
}

for(int m_i = 0; m_i < m; m_i++){
if(!(apple[m_i] < 0))
aIn += isInHouse(s, t, apple[m_i] + a);
}

for(int n_i = 0; n_i < n; n_i++){
if(!(orange[n_i] > 0))
oIn += isInHouse(s, t, orange[n_i] + b);
}

printf("%d\n", aIn);
printf("%d", oIn);

return 0;
}

Copy The Code &

### #2 Code Example with C++ Programming

Code - C++ Programming


#include<bits/stdc++.h>
using namespace std;
int main(){
int s,t,a,b,m,n,apple = 0,orange = 0;
cin >> s >> t;
cin >> a >> b;
cin >> m >> n;
int aq[m],bq[n];
for(int i = 0; i< m; i++)
{
cin >> aq[i];
}
for(int i = 0; i < n; i++)
{
cin>>bq[i];
}
for(int i = 0;i < m; i++)
{
if(((a + aq[i]) >= s) && ((a+aq[i]) <= t)) {apple++;}
}
for(int i = 0; i < n; i++)
{
if(((b + bq[i]) >= s) && ((b + bq[i]) <= t)) {orange++;}
}
cout << apple << endl << orange;
return 0;
}


Copy The Code &

### #3 Code Example with Java Programming

Code - Java Programming


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

public class Solution {

// Complete the countApplesAndOranges function below.
static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges) {
long np = 0;
long no =  0;
for(int i = 0; i < apples.length; i++){
if(a + apples[i] >=  && a + apples[i] <= t){
np++;
}
}
for(int j = 0; j < oranges.length; j++){
if(b + oranges[j] >= s && b + oranges[j] <=t){
no++;
}
}
System.out.println(np);
System.out.println(no);
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
String[] st = scanner.nextLine().split(" ");

int s = Integer.parseInt(st);

int t = Integer.parseInt(st);

String[] ab = scanner.nextLine().split(" ");

int a = Integer.parseInt(ab);

int b = Integer.parseInt(ab);

String[] mn = scanner.nextLine().split(" ");

int m = Integer.parseInt(mn);

int n = Integer.parseInt(mn);

int[] apples = new int[m];

String[] applesItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < m; i++) {
int applesItem = Integer.parseInt(applesItems[i]);
apples[i] = applesItem;
}

int[] oranges = new int[n];

String[] orangesItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < n; i++) {
int orangesItem = Integer.parseInt(orangesItems[i]);
oranges[i] = orangesItem;
}

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

scanner.close();
}
}

Copy The Code &

### #4 Code Example with C# Programming

Code - C# Programming


using System;

class Solution
{
static void Main(String[] args)
{
var applesFallingOnHouse = 0;
var orangesFallingOnHouse = 0;

var houseStart = int.Parse(tokens_s);
var houseEnd = int.Parse(tokens_s);

var appleTreePosition = int.Parse(tokens_a);
var OrangeTreePosition = int.Parse(tokens_a);

//No need to capture number of apples and oranges as I use foreach loop to iterate them.

var fallingApples = Array.ConvertAll(apple_temp, Int32.Parse);
var fallingOranges = Array.ConvertAll(orange_temp, Int32.Parse);

foreach (var fallingApple in fallingApples)
{
var fallingApplePosition = appleTreePosition + fallingApple;
if (fallingApplePosition >= houseStart && fallingApplePosition <= houseEnd)
++applesFallingOnHouse;
}

foreach (var fallingOrange in fallingOranges)
{
var fallingOrangePosition = OrangeTreePosition + fallingOrange;
if (fallingOrangePosition >= houseStart && fallingOrangePosition <= houseEnd)
++orangesFallingOnHouse;
}

Console.WriteLine(applesFallingOnHouse);
Console.WriteLine(orangesFallingOnHouse);
}
}

Copy The Code &

### #5 Code Example with Python Programming

Code - Python Programming


import math
import os
import random
import re
import sys

# Complete the countApplesAndOranges function below.
def countApplesAndOranges(s, t, a, b, apples, oranges):
acount = 0
bcount = 0
for i in range(len(apples)):
temp = a + apples[i]
if(temp in range(s,t + 1)):
acount += 1
for i in range(len(oranges)):
temp = b + oranges[i]
if(temp in range(s, t + 1)):
bcount +=
print (acount)
print (bcount)

if __name__ == '__main__':
st = input().split()

s = int(st)

t = int(st)

ab = input().split()

a = int(ab)

b = int(ab)

mn = input().split()

m = int(mn)

n = int(mn)

apples = list(map(int, input().rstrip().split()))

oranges = list(map(int, input().rstrip().split()))

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

Copy The Code &

### #6 Code Example with PHP Programming

Code - PHP Programming


= $s and$a + $f <=$t;
}));
}

$stdin = fopen("php://stdin", "r"); fscanf($stdin, "%[^\n]", $st_temp);$st = explode(' ', $st_temp);$s = intval($st);$t = intval($st); fscanf($stdin, "%[^\n]", $ab_temp);$ab = explode(' ', $ab_temp);$a = intval($ab);$b = intval($ab); fscanf($stdin, "%[^\n]", $mn_temp);$mn = explode(' ', $mn_temp);$m = intval($mn);$n = intval($mn); fscanf($stdin, "%[^\n]", $apples_temp);$apples = array_map('intval', preg_split('/ /', $apples_temp, -1, PREG_SPLIT_NO_EMPTY)); fscanf($stdin, "%[^\n]", $oranges_temp);$oranges = array_map('intval', preg_split('/ /', $oranges_temp, -1, PREG_SPLIT_NO_EMPTY)); countApplesAndOranges($s, $t,$a, $b,$apples, $oranges); fclose($stdin);

Copy The Code &