--> "Apple And Orange" - A Java HackerRank Solution

The Apple and Orange – Java HackerRank Solution

Apple and Orange pic

Summary

The “Apple and Orange” problem requires you to calculate the landing positions of apples and oranges and count how many fall on Sam’s house based on given distances. Following these steps, you can determine and print the number of fruits landing on the house.

Problem Statement

Sam’s house is located on a one-dimensional line. You are given:

  • The starting and ending points of Sam’s house.
  • The location of an apple tree and an orange tree.
  • Distances at which apples and oranges fall from their respective trees.

You need to determine how many apples and oranges land on Sam’s house.

Inputs

  1. s and t are integers that denote the starting and ending points of Sam’s house, respectively.
  2. a and b: Integers representing the positions of the apple and orange trees.
  3. An array of integers representing the distances at which each apple falls from the apple tree.
  4. An array of integers representing the distances at which each orange falls from the orange tree.

Steps to Solve the Apple and Orange

  1. Input Reading
    • Read integers s and t (house start and end points).
    • Read integers a and b (apple tree and orange tree positions).
    • Read the arrays of distances for apples and oranges.
  2. Calculate Landing Positions
    • For each apple, determine its landing position by adding its distance to the position of the apple tree (a).
    • For each orange, calculate its landing position by adding its distance to the position of the orange tree (b).
  3. Count Fruits on the House
    • Check each calculated apple position to see if it falls within the range [s, t].
    • Check each calculated orange position to see if it falls within the range [s, t].
  4. Output the Results
    • Print the number of apples that fall on Sam’s house.
    • Print the number of oranges that have fallen on Sam’s house.

Example

Let’s walk through an example step by step:

  • Inputs:
    • s = 7, t = 10 (Sam’s house is between 7 and 10).
    • a = 4 (apple tree position).
    • b = 12 (orange tree position).
    • apples = [2, 3, -4] represents a list of distances where apples fall relative to the apple tree.
    • oranges = [3, -2, -4] represents a list of distances where oranges fall relative to the apple tree.
  • Calculate Apple Landing Positions:
    • Apple 1: 4 + 2 = 6
    • Apple 2: 4 + 3 = 7
    • Apple 3: 4 – 4 = 0
  • Calculate Orange Landing Positions:
    • Orange 1: 12 + 3 = 15
    • Orange 2: 12 – 2 = 10
    • Orange 3: 12 – 4 = 8
  • Count Apples on the House:
    • Apples at positions: 6, 7, 0
    • Only the Apple 7 falls on the house.
  • Count Oranges on the House:
    • Oranges at positions: 15, 10, 8
    • Oranges 10 and 8 fall on the house.
  • Output:
    • Number of apples in the house: 1
    • Number of oranges in the house: 2
Apple and Orange page 1
Apple and Orange page 2
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 nearer to apple house
     *  2. INTEGER t nearer to orange houuse 
     *  3. INTEGER a apple tree
     *  4. INTEGER b orange tree
     *  5. INTEGER_ARRAY apples
     *  6. INTEGER_ARRAY oranges
     */

    public static void countOrangesAndApples(int s, int t, int a, int b, List<Integer> apples, List<Integer> oranges) {
    // Write your code here
        int appleCount = 0;
        int orangeCount = 0;
        for(int i : apples)
            if((i+a) >= s && (i+a) <= t) appleCount++;
        for(int j : oranges)
            if((j+b) <= t && (j+b) >= s) orangeCount++;
            
        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();
    }
}
The Problem Solution

Leave a Reply

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