Skip to content

Commit b049dd1

Browse files
random hackerrank question added
1 parent 2b2f758 commit b049dd1

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Sam's house has an apple tree and an orange tree that yield an abundance of fruit.
3+
* Using the information given below, determine the number of apples and oranges that land on Sam's house.
4+
5+
In the diagram below:
6+
7+
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.
8+
Assume the trees are located on a single point, where the apple tree is at point , and the orange tree is at point .
9+
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. *
10+
Apple and orange(2).png
11+
12+
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 )?
13+
14+
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
15+
16+
HackerRank: https://www.hackerrank.com/challenges/apple-and-orange/problem?isFullScreen=true
17+
*/
18+
19+
/*
20+
* Complete the 'countApplesAndOranges' function below.
21+
*
22+
* The function accepts following parameters:
23+
* 1. INTEGER s
24+
* 2. INTEGER t
25+
* 3. INTEGER a
26+
* 4. INTEGER b
27+
* 5. INTEGER_ARRAY apples
28+
* 6. INTEGER_ARRAY oranges
29+
*/
30+
31+
function countApplesAndOranges(s, t, a, b, apples, oranges) {
32+
// console.log(s, t, a, b)
33+
// console.log(apples, oranges)
34+
35+
// a5 - s7 t11 - b15
36+
// [-2, 2, 1]
37+
// [5, -6]
38+
39+
let applesLandedAt = [], orangesLandedAt = [];
40+
41+
for (let i = 0; i < apples.length; i++) {
42+
applesLandedAt.push(a + apples[i])
43+
}
44+
45+
for (let i = 0; i < oranges.length; i++) {
46+
orangesLandedAt.push(b + oranges[i])
47+
}
48+
49+
// console.log(applesLandedAt, orangesLandedAt)
50+
51+
let betweenRangeSumApple = 0, betweenRangeSumOrange = 0;
52+
53+
for (let i = 0; i < applesLandedAt.length; i++) {
54+
if (applesLandedAt[i] >= s && applesLandedAt[i] <= t) {
55+
betweenRangeSumApple++
56+
}
57+
}
58+
59+
for (let i = 0; i < orangesLandedAt.length; i++) {
60+
if (orangesLandedAt[i] >= s && orangesLandedAt[i] <= t) {
61+
betweenRangeSumOrange++
62+
}
63+
}
64+
console.log(betweenRangeSumApple)
65+
console.log(betweenRangeSumOrange)
66+
67+
return [betweenRangeSumApple, betweenRangeSumOrange]
68+
}
69+
70+
console.log(countApplesAndOranges(7, 11, 5, 15, [-2, 2, 1], [5, -6])) // 1, 1
71+
console.log(countApplesAndOranges(7, 10, 4, 12, [2, 3, -4], [3, -2, -4])) // 1, 2

0 commit comments

Comments
 (0)