Electronics Shop - HackerRank.


Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the  items, given her budget.
Given the price lists for the store's keyboards and USB drives, and Monica's budget, find and print the amount of money Monica will spend. If she doesn't have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items.
For example, suppose she has  to spend. Three types of keyboards cost . Two USB drives cost . She could purchase a , or a . She chooses the latter. She can't buy more than  items so she can't spend exactly .
Function Description
Complete the getMoneySpent function in the editor below. It should return the maximum total price for the two items within Monica's budget, or  if she cannot afford both items.
getMoneySpent has the following parameter(s):
  • keyboards: an array of integers representing keyboard prices
  • drives: an array of integers representing drive prices
  • b: the units of currency in Monica's budget
Input Format
The first line contains three space-separated integers , and , her budget, the number of keyboard models and the number of USB drive models.
The second line contains  space-separated integers , the prices of each keyboard model.
The third line contains  space-separated integers , the prices of the USB drives.
Constraints
  • The price of each item is in the inclusive range .
Output Format
Print a single integer denoting the amount of money Monica will spend. If she doesn't have enough money to buy one keyboard and one USB drive, print -1 instead.
Sample Input 0
10 2 3
3 1
5 2 8
Sample Output 0
9
Explanation 0
She can buy the  keyboard and the  USB drive for a total cost of .
Sample Input 1
5 1 1
4
5
Sample Output 1
-1

Explanation 1
There is no way to buy one keyboard and one USB drive because , so we print .



NOTE: If you are copying my code then its a advise to you to copy it after downloading it to avoid any kind of compilation error its link is available below source code.



SOURCE CODE



import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Solution {

/*
* Complete the getMoneySpent function below.
*/
static int getMoneySpent(int[] keyboards, int[] drives, int b) {
/*
* Write your code here.
*/
int max=0;
for(int i=0;i<keyboards.length;i++)
{
for(int j=0;j<drives.length;j++)
{
if(drives[j]+keyboards[i]>=max && drives[j]+keyboards[i]<=b)
{
max=drives[j]+keyboards[i];
}
}
}
if(max==0)
return(-1);
else
return(max);
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

String[] bnm = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

int b = Integer.parseInt(bnm[0]);

int n = Integer.parseInt(bnm[1]);

int m = Integer.parseInt(bnm[2]);

int[] keyboards = new int[n];

String[] keyboardsItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

for (int keyboardsItr = 0; keyboardsItr < n; keyboardsItr++) {
int keyboardsItem = Integer.parseInt(keyboardsItems[keyboardsItr]);
keyboards[keyboardsItr] = keyboardsItem;
}

int[] drives = new int[m];

String[] drivesItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

for (int drivesItr = 0; drivesItr < m; drivesItr++) {
int drivesItem = Integer.parseInt(drivesItems[drivesItr]);
drives[drivesItr] = drivesItem;
}

/*
* The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
*/

int moneySpent = getMoneySpent(keyboards, drives, b);

bufferedWriter.write(String.valueOf(moneySpent));
bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();
}
}



Click here to Download



OUTPUT








If you have any question then leave a comment below I will do my best to answer that question.

Comments

  1. Good article, but it would be better if in future you can share more about this subject. Keep posting.
    best home appliances shops in dubai

    ReplyDelete
  2. Hi Michael Sheen,

    Thanks for giving yours valuable time to comment on this article. It means alot to us. We will surely share more articles related to it, till then keep visiting us

    Crazy About Coding
    https://crazyaboutcoding.blogspot.com/

    ReplyDelete
  3. Mesmerized article composed of this web journal with other significant data. It is straight to the point that how we will make strides our aptitudes as well as how able to be spoken to a modern stream of polished skill. Cheap lidar scanner Denver, Colorado

    ReplyDelete
  4. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. https://tvbfdeals.com

    ReplyDelete
  5. If you have a washing machine repair anywhere near you in Dubai, our qualified professionals are your only hope, and they possess the expertise with all the right tools and certifications to perform washing machine repair Dubai. But if you're already in need of Washing Machine Repair Dubai, you don't know what to do and where to start from. Well, the solution is simple - let our experienced washing machine repair professional to deal with it for you. We have the requisite expertise and experience to fix your washing machine problem in the quickest possible way.

    ReplyDelete

Post a Comment

Popular posts from this blog

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Pattern of your own name.

Designing Patterns - Print 'Z'.