Hacker Rank – Apple and Orange

Overview

In this article we will try to solve one of the problem from Hacker Rank. It’s called Apple and Orange.

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.

s is the start point of the house, 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.

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])?

Code

We will iterate through all the elements in the apples and orange array – we will add it to the values of the tree position and will check if the added values lies between the values of the house.

for (int i = 0; i < apples.length; i++) {
    if (a + apples[i] >= s && a + apples[i] <= t) {
        appleCount++;
    }
}

Full code is below:

AppleOrange.java

public class AppleOrange {

    public static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges) {
        int appleCount = 0;
        for (int i = 0; i < apples.length; i++) {
            if (a + apples[i] >= s && a + apples[i] <= t) {
                appleCount++;
            }
        }
        System.out.println(appleCount);

        int orangeCount = 0;
        for (int i = 0; i < oranges.length; i++) {
            if (b + oranges[i] >= s && b + oranges[i] <= t) {
                orangeCount++;
            }
        }
        System.out.println(orangeCount);
    }
}

Test

Below is a simple JUnit test for the above code:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;

public class AppleOrangeTest {

    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    private final PrintStream originalOut = System.out;

    @Before
    public void setUpStreams() {
        System.setOut(new PrintStream(outContent));
    }

    @After
    public void restoreStreams() {
        System.setOut(originalOut);
    }

    @Test
    public void test1() {
        AppleOrange.countApplesAndOranges(7, 10, 4, 12, new int[]{2, 3, -4}, new int[]{3, -2, -4});
        assertEquals("1\n2\n", outContent.toString());
    }

    @Test
    public void test2() {
        AppleOrange.countApplesAndOranges(7, 11, 5, 15, new int[]{-2, 2, 1}, new int[]{5, -6});
        assertEquals("1\n1\n", outContent.toString());
    }
}

Conclusion

In this article we saw how to solve the Apple and Orange problem from HackerRank in Java.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: