Electronic Shop (Hacker Rank)

Overview

In this article we will try to solve one of the problem from HackerRank called Electronic Shop. A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget. Given price lists for keyboards and USB drives and a budget, find the cost to buy them. If it is not possible to buy both items, return -1.

Example

b = 60
keyboards = [40, 50, 60]
drives = [5, 8, 12]

A person can buy a 40 keyboard + 12 USB drive = 52 or a 50 Keyboard + 8 USB drive = 58. We will use the later option as it’s more expensive.

Code

In this section we will try to solve the above problem using Java. Our function will take three parameters. First one is the array of prices of keyboards, second is the array of prices of drives and the third one is the max price.

First we will sort both the arrays in descending order:

int[] keyboardDesc = Arrays.stream(keyboards).boxed()
        .sorted(Collections.reverseOrder())
        .mapToInt(Integer::intValue)
        .toArray();
int[] drivesDesc = Arrays.stream(drives).boxed()
        .sorted(Collections.reverseOrder())
        .mapToInt(Integer::intValue)
        .toArray();

Now we will iterate both the array – add the prices and check of the sum is less than the maximum price allowed. We also need to check if this new cost is more than the previous max, if it is we override the max:

int max = -1;
for (int i = 0; i < keyboardDesc.length; i++) {
    for (int j = 0; j < drivesDesc.length; j++) {
        final int cost = keyboards[i] + drives[j];
        if (cost <= b && cost > max) {
            max = cost;
        }
    }
}

ElectronicShop.java

import java.util.Arrays;
import java.util.Collections;

public class ElectronicShop {

    public static int getMoneySpent(int[] keyboards, int[] drives, int b) {
        int[] keyboardDesc = Arrays.stream(keyboards).boxed()
                .sorted(Collections.reverseOrder())
                .mapToInt(Integer::intValue)
                .toArray();
        int[] drivesDesc = Arrays.stream(drives).boxed()
                .sorted(Collections.reverseOrder())
                .mapToInt(Integer::intValue)
                .toArray();
        int max = -1;
        for (int i = 0; i < keyboardDesc.length; i++) {
            for (int j = 0; j < drivesDesc.length; j++) {
                final int cost = keyboards[i] + drives[j];
                if (cost <= b && cost > max) {
                    max = cost;
                }
            }
        }
        return max;
    }
}

Test

Let’s write a simple test for the above code:

import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class ElectronicShopTest {

    @Test
    public void getMoneySpent() {
        Assert.assertEquals(58, ElectronicShop.getMoneySpent(new int[]{40, 50, 60}, new int[]{5, 8, 12}, 60));
        Assert.assertEquals(9, ElectronicShop.getMoneySpent(new int[]{3, 1}, new int[]{5, 2, 8}, 10));
    }
}

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: