'Electronics Shop

I have been working on this question, and after submitting my code 7 cases passed, however, 9 cases failed. The question link is here of HackerRank : Electronic Shop

Problem Statement

A girl wants to buy a keyboard, and a USB drive, she want to spend as much as possible but within budget (combining both the items). And if the price of both the items exceeds her budget, return -1 or she can't buy. What is mandatory is that She wants to buy both the items, not a single one.

Example

A range of keyboards is given in the form of an array [40, 50, 60], and USB drives is given as [5,8,12]. Her max budget is 60. Now if we sum up both the things we get two max value of combination :

  • 40 + 12 = 52
  • 50 + 8 = 58

Since 58 one is greater, hence she will get the items worth of 50 and 8.

Input format

  • The first line contains three space-separated integers b, n, and m, 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.

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.

My Algo

1. Take answer variable as 0, and max value initialize it with first element of keyboard + first element of usb drives

2. loop through keyboard, make an inner loop for usb

3. Compare keyboard[i] + drives[j], if greater than b, then return -1.

4. Else find the max value and assign it to answer

5. return answer

My logic is as simple as the requirements, but somehow fails for the cases which has a very large number of elements in the array.

Code

static int getMoneySpent(int[] keyboards, int[] drives, int b) {
    int answer = 0, maxAmount = keyboards[0] + drives[0];

    //This will compare the value of i+j throughout the loop and returns the max one 
    for(int i: keyboards){
      for(int j: drives){
        // Ofcourse if all i+j values will be greater than the max budget then -1
        if((i+j) > b)
          answer = -1;
        else if((i+j) == b){
           answer = i+j;
        }else{
           /*If the value is smaller than the max budget, the finding the max value we get after adding them and comparing with the maxAmount variable */
          if((i+j) > maxAmount){
            maxAmount = i+j;
            answer = maxAmount;
        } 
      }
    }
  }

  return answer;
}

I'm having two cases which failed,, here they are :

Failed Test Case 1

Input =>

539855 818 628
380710 674456 878173 532602 868253 721585 806107 141310 790209 212031 
304748 818920 80938 322601 403071 22899 173564 153826 695108 223665 
346178 957539 975830 573171 641117 932941 822666 575293 132555 479463 
862209 313799 922966 606508 487172 139230 606390 898464 764983 829520 
174879 317603 502680 953013 398753 825387 146407 666457 367618 121790 
68188 478342 25818 506222 135197 232604 963333 79984 549654 776899 
966040 122063 432596 594425 311887 936661 506256 876303 439611 277816 
105689 851641 640971 333 216087 17692 619728 602689 650348 364881 
152060 386548 61364 564569 780938 191826 459905 211804 58177 484711 
995091 754424 57794 619638 695192 297423 983901 430435 239234 170704 
142282 74647 121413 782873 303344 265448 101069 177807 692318 691774 
62306 618191 509537 633333 996922 228947 814154 232698 615359 220853 
306323 173792 624037 655872 527161 848207 426180 724481 130740 792273 
886804 404890 449886 654224 194667 354317 367843 525624 414224 481744 
827725 176927 733780 387166 769479 964040 1{-truncated-}

Expected Output

539854

For full input data here is the link : Input Array Data Full

Failed Test Case 2

Input =>

374625 797 951
183477 732159 779867 598794 596985 156054 445934 156030 99998 58097 
459353 866372 333784 601251 142899 708233 651036 20590 56425 970129 
722162 832631 938765 212387 779 181866 992436 183446 617621 304311 
611791 524875 7068 432043 23068 291295 524893 611991 399952 139526 
46677 292211 973975 366445 232824 456173 90627 785353 618526 199719 
382549 514351 983453 592549 466869 46461 860135 607682 680461 170563 
450601 65067 13268 949100 942415 965850 563416 808580 385504 304683 
15970 97695 230946 684388 241080 440252 683418 122066 610135 495289 
833383 34397 173404 909526 391149 258839 182278 662672 755532 311782 
425252 520186 207989 546834 567829 184897 31321 969804 842475 775308 
449856 939711 395240 895029 926868 598035 727436 922082 326615 88513 
570573 196028 520952 45238 961389 325404 844725 388765 747489 271411 
539814 828925 586884 356834 965473 280998 607171 542819 276062 140956 
296341 802378 165305 74568 15640 987110 423497 772419 394971 198761 
293555 5524 14083 815646 198888 707017 711503 729172{-truncated-}

Expected Output

374625

For full input array data for this one follow this link : Failed Test Case 2 Full Input

I'm there almost but somehow I'm confused why my code is not working for long input array elements. Any help would be appreciated, as it will make me learn new thing in my future endeavor.



Solution 1:[1]

You misunderstood the question. Answer should be -1 if you can't buy any keyborad+usb. Not if there is one set unaffordable, but if they all are. With your current code, what would it return if the very last set is unaffordable?

Here is a code that should work. And comments to explain:

int answer = -1; // that's the default answer.
int maxAmount = 0; // what if first keyboard + first usb are unaffordable? let's put it to 0

//This will compare the value of i+j throughout the loop and returns the max one 
for(int i: keyboards){
  for(int j: drives){
    if((i+j) > b) {
      // do nothing, it's unaffordable (and remove this block)
    }else if((i+j) == b){
       answer = i+j; 
       return answer;// it can't be more, stop the loop and return the solution
    } else if((i+j) > maxAmount){ // no need to put an else, then an if both conditions are related
        maxAmount = i+j;
        answer = maxAmount;
    } 
  }
}

Of course, if you remove the first empty ifblock from the above code, you will have to change the last condition in order to check if it's below the max allowed:

if((i+j)>maxAmount && (i+j)<=b)

Solution 2:[2]

int getMoneySpent(int keyboards_count, int* keyboards, int drives_count, int* drives, int b) {

     int price=-1;

         for(int i=0;i<drives_count;i++)
         {
             for(int j=0;j<keyboards_count;j++)
             {
                 if((drives[i]+keyboards[j]>price) && (drives[i]+keyboards[j]<=b))
                 {
                     price=drives[i]+keyboards[j];
                 }
             }
         }
         return price;
}

Solution 3:[3]

Javascript Solution:

function getMoneySpent(keyboards, drives, b) {
    const combos = [];
    let maxCost = 0
    keyboards.forEach(keyboard => {
        drives.forEach(drive => {
            let currentComboCost = keyboard+drive;
            maxCost = ((currentComboCost <= b) && (currentComboCost > maxCost)) ? currentComboCost : maxCost;
        })
    })
    return maxCost || -1;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 David Buck
Solution 3 Rajan m