HackerRank Apple and Orange Solution

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

HackerRank Apple and Orange Solution
HackerRank Apple and Orange Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Apple and Orange Solution

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

Apple and orange(2).png

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 fread(ch) freopen(ch,"r",stdin)
#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()
{
	//fread("");
	//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();    
});
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);
    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
Read(s,t,a,b,m,n);
z:=0;
For i:=1 to m do
  begin
    read(d);
    if (a+d>=s) and (a+d<=t) then inc(z);
  end;
 Writeln(z);
z:=0;
 For i:=1 to n do
  begin
    read(d);
    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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Time Conversion Solution

Leave a Reply

Your email address will not be published. Required fields are marked *