![HackerRank Apple and Orange Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Apple-and-Orange-Problem-Solution.jpg)
HackerRank Apple and Orange Problem Solution
In this post, We are going to solve 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 units of distance from its tree of origin along the -Xaxis. *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 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].
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 the point 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 into position 3.
The second apple falls at position 7.
The third apple falls at position 6.
The first orange falls at position 20.
The second orange falls at position 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 Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Apple-and-Orange-Problem-Solution-1024x576.jpg)
Apple and Orange C Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int rmin, rmax;
scanf("%d%d", &rmin, &rmax);
int iapple, iorange;
int napple, norange;
scanf("%d%d%d%d", &iapple,&iorange,&napple,&norange);
int c1 = 0,c2 = 0,x;
for( int i = 0; i < napple; i++) {
scanf("%d", &x);
if( x + iapple >= rmin && x+ iapple <= rmax) c1++;}
for( int i = 0; i < norange; i++) {scanf("%d", &x);
if( x +iorange >= rmin && x + iorange <= rmax) c2++;
}
printf("%d\n%d", c1,c2);
return 0;
}
Apple and Orange C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int l, r, n, m, a, b;
int ans[2];
int main() {
cin >> l >> r >> a >> b >> n >> m;
while (n--) {
int x;
cin >> x;
ans[0] += (l <= a + x && a + x <= r);
}
while (m--) {
int x;
cin >> x;
ans[1] += (l <= b + x && b + x <= r);
}
cout << ans[0] << '\n' << ans[1];
return 0;
}
Apple and Orange C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
string[] tokens_s = Console.ReadLine().Split(' ');
int s = Convert.ToInt32(tokens_s[0]);
int t = Convert.ToInt32(tokens_s[1]);
string[] tokens_a = Console.ReadLine().Split(' ');
int a = Convert.ToInt32(tokens_a[0]);
int b = Convert.ToInt32(tokens_a[1]);
string[] tokens_m = Console.ReadLine().Split(' ');
int m = Convert.ToInt32(tokens_m[0]);
int n = Convert.ToInt32(tokens_m[1]);
string[] apple_temp = Console.ReadLine().Split(' ');
int[] apple = Array.ConvertAll(apple_temp,Int32.Parse);
string[] orange_temp = Console.ReadLine().Split(' ');
int[] orange = Array.ConvertAll(orange_temp,Int32.Parse);
var totalApples = 0;
var totalOranges = 0;
// read all apples
for (var i = 0; i < m; i++)
{
var absoluteDistance = apple[i] + a;
if (absoluteDistance >= s && absoluteDistance <= t) totalApples++;
}
Console.WriteLine(totalApples);
// read all oranges
for (var i = 0; i < n; i++)
{
var absoluteDistance = orange[i] + b;
if (absoluteDistance >= s && absoluteDistance <= t) totalOranges++;
}
Console.WriteLine(totalOranges);
}
}
Apple and Orange Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* 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
*/
public static void countApplesAndOranges(int s, int t, int a, int b, List<Integer> apples, List<Integer> oranges) {
var appleCount = apples
.stream()
.map(x -> a + x)
.filter(x -> x >= s && x <= t)
.count();
var orangeCount = oranges
.stream()
.map(x -> b + x)
.filter(x -> x >= s && x <= t)
.count();
System.out.println(appleCount);
System.out.println(orangeCount);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int s = Integer.parseInt(firstMultipleInput[0]);
int t = Integer.parseInt(firstMultipleInput[1]);
String[] secondMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int a = Integer.parseInt(secondMultipleInput[0]);
int b = Integer.parseInt(secondMultipleInput[1]);
String[] thirdMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int m = Integer.parseInt(thirdMultipleInput[0]);
int n = Integer.parseInt(thirdMultipleInput[1]);
List<Integer> apples = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> oranges = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
Result.countApplesAndOranges(s, t, a, b, apples, oranges);
bufferedReader.close();
}
}
Apple and Orange JavaScript Solution
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();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var s_temp = readLine().split(' ');
var s = parseInt(s_temp[0]);
var t = parseInt(s_temp[1]);
var a_temp = readLine().split(' ');
var a = parseInt(a_temp[0]);
var b = parseInt(a_temp[1]);
var m_temp = readLine().split(' ');
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
apple = readLine().split(' ');
apple = apple.map(Number);
orange = readLine().split(' ');
orange = orange.map(Number);
write(getAppleNumbers(s,t,a,apple));
write(getOrangesNumbers(s,t,b,orange));
}
function getAppleNumbers(s,t,a,fruit){
var distmin;
var distmax;
var count=0;
for(i in fruit){
if(fruit[i]>0){
distmin = (s-a);
distmax = (t-a)
if(fruit[i]>=distmin && fruit[i]<=distmax){
count++;
}
}
}
return count;
}
function getOrangesNumbers(s,t,b,fruit){
var distmin;
var distmax;
var count=0;
for(i in fruit){
if(fruit[i]<0){
distmin = (b-t);
distmax = (b-s);
if((fruit[i]*(-1))>=distmin && (fruit[i]*(-1))<=distmax){
count++;
}
}
}
return count;
}
function write(text){
process.stdout.write(text+"\n");
}
Apple and Orange Python Solution
#!/bin/python3
import sys
s,t = input().strip().split(' ')
s,t = [int(s),int(t)]
a,b = input().strip().split(' ')
a,b = [int(a),int(b)]
b -= a
s -= a
t -= a
m,n = input().strip().split(' ')
m,n = [int(m),int(n)]
apple = list(filter(lambda x: int(x) >= s and int(x) <= t, input().strip().split(' ')))
orange = list(filter(lambda x: int(x)+b <= t and int(x)+b >=s, input().strip().split(' ')))
print(len(apple))
print(len(orange))
Other Solutions
1xBet registration in India
https://rajabets-in-india.com/