Electronics Shop Hackerrank Solution

10 min read
 Electronics Shop Hackerrank Solution


CODE

#include <bits/stdc++.h>
using namespace std;
int main(){
int money;
cin>>money;
int k,m;
cin>>k>>m;
int key[k];
int mouse[m];
int bill;
int maxbill=0;
for(int i=0;i<k;i++){
cin>>key[i];
}
for(int i=0;i<m;i++){
cin>>mouse[i];
}
for(int i=0;i<k;i++){
for(int j=0;j<m;j++){
bill=key[i]+mouse[j];
if(bill>=maxbill && bill<=money){
maxbill=bill;
}
}
}
if(maxbill==0){
cout<<"-1"<<endl;
}else {
cout<<maxbill<<endl;
}
}


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 .

Example


The person can buy a , or a . Choose the latter as the more expensive option and return .

Function Description

Complete the getMoneySpent function in the editor below.

getMoneySpent has the following parameter(s):

  • int keyboards[n]: the keyboard prices
  • int drives[m]: the drive prices
  • int b: the budget

Returns

  • int: the maximum that can be spent, or  if it is not possible to buy both items

Input Format

The first line contains three space-separated integers , and , the 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 .

Sample Input 0

10 2 3
3 1
5 2 8

Sample Output 0

9

Explanation 0

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 return .

Men' fashion , Dating Tips , Relationship Advice

You may like these posts

  •  Electronics Shop Hackerrank SolutionCODE A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget. Given pric…
  •  CODE : Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to .ExampleThere are two subar…
  • Designer PDF Hackerrank SolutionCODE PROBLEMWhen a contiguous block of text is selected in a PDF viewer, the selection is highlighted with a blue rectangle. In this PDF viewer…
  • Equalize the Array Hackerrank SolutionCODE Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.ExampleDe…
  • PROBLEMGiven five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and …

Post a Comment