Hello Programmers, In this post, you will know how to solve the HackerRank Electronics Shop Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Electronics Shop Solution
Task
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]
drivers = [5, 8, 12]
The person can buy a 40 keyboard + 12 USB drive = 52, or a 50 keyboard + 8 USB drive = 58. Choose the latter as the more expensive option and return 58.
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 –1 if it is not possible to buy both items
Input Format
The first line contains three space-separated integers b, n, and m, the budget, the number of keyboard models and the number of USB drive models.
The second line contains n space–separated integers keyboard[i], the prices of each keyboard model.
The third line contains m space-separated integers drives, the prices of the USB drives.
Constraints
- 1 <= n, m < 1000
- 1 <= b <= 106
- The price of each item is in the inclusive range [1, 106].
Sample Input 0
10 2 3 3 1 5 2 8
Sample Output 0
9
Explanation 0
Buy the 2nd keyboard and the 3rd USB drive for a total cost of 8 + 1 = 9.
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 4 + 5 > 5, so return -1.
HackerRank Electronics Shop Solution
Electronics Shop Solution in C
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int s; int n; int m,i,j,k,l; scanf("%d %d %d",&s,&n,&m); int *keyboards = malloc(sizeof(int) * n); for(int keyboards_i = 0; keyboards_i < n; keyboards_i++){ scanf("%d",&keyboards[keyboards_i]); } int *pendrives = malloc(sizeof(int) * m); for(int pendrives_i = 0; pendrives_i < m; pendrives_i++){ scanf("%d",&pendrives[pendrives_i]); } k=-1; for(i=0;i<n;i++) { if(keyboards[i]>=s) { continue; } for(j=0;j<m;j++) { if(keyboards[i]+pendrives[j]<=s&&keyboards[i]+pendrives[j]>k) { k=keyboards[i]+pendrives[j]; } } } printf("%d",k); return 0; }
Electronics Shop Solution in Cpp
#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second int main(){ int s; int n; int m; cin >> s >> n >> m; vector<int> keyboards(n); for(int keyboards_i = 0;keyboards_i < n;keyboards_i++){ cin >> keyboards[keyboards_i]; } vector<int> pendrives(m); for(int pendrives_i = 0;pendrives_i < m;pendrives_i++){ cin >> pendrives[pendrives_i]; } int res=-1; FOR(i,n) FOR(j,m) if (keyboards[i]+pendrives[j] <= s) res = max(res, keyboards[i]+pendrives[j]); cout << res << "\n"; return 0; }
Electronics Shop Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int s = in.nextInt(); int n = in.nextInt(); int m = in.nextInt(); int[] keyboards = new int[n]; for(int keyboards_i=0; keyboards_i < n; keyboards_i++){ keyboards[keyboards_i] = in.nextInt(); } int[] pendrives = new int[m]; for(int pendrives_i=0; pendrives_i < m; pendrives_i++){ pendrives[pendrives_i] = in.nextInt(); } int ans = -1; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { int v = keyboards[i]+pendrives[j]; if(v>ans && v<= s) { ans = v; } } } System.out.println(ans); } }
Electronics Shop Solution in Python
#!/bin/python import sys s,n,m = raw_input().strip().split(' ') s,n,m = [int(s),int(n),int(m)] keyboards = map(int,raw_input().strip().split(' ')) pendrives = map(int,raw_input().strip().split(' ')) sorted(keyboards) sorted(pendrives) max = -1 for i in keyboards: for j in pendrives: sum = i+j if sum<=s: if sum>max: max = sum print max
Electronics Shop Solution using JavaScript
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var s_temp = readLine().split(' '); var s = parseInt(s_temp[0]); var n = parseInt(s_temp[1]); var m = parseInt(s_temp[2]); keyboards = readLine().split(' '); keyboards = keyboards.map(Number); pendrives = readLine().split(' '); pendrives = pendrives.map(Number); var amountSpent = 0; var temp = 0; keyboards.forEach(function(keyCost, index) { pendrives.forEach(function(driveCost, dex) { if(keyCost+driveCost <= s && keyCost+driveCost > amountSpent) { amountSpent = keyCost+driveCost; } }); }); if(amountSpent === 0) { console.log(-1); } else { console.log(amountSpent); } }
Electronics Shop Solution in Scala
object Solution { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in); var s = sc.nextInt(); // money to spend var n = sc.nextInt(); // keyboards var m = sc.nextInt(); // usb drives var keyboards = Array.fill(n)(sc.nextInt); var pendrives = Array.fill(m)(sc.nextInt); val prices = for(k <- keyboards; p <- pendrives) yield k+p val filtered = prices.filter{_ <= s} val res = if(filtered.isEmpty) -1 else filtered.max println(res) } }
Electronics Shop Solution in Pascal
{$MODE OBJFPC} var s : integer; n,m,i,j : integer; a,b : array[1..1001] of integer; ans : integer; BEGIN readln(s,n,m); for i:=1 to n do read(a[i]); for i:=1 to m do read(b[i]); ans:= -1; for i:=1 to n do for j:=1 to m do if (a[i] + b[j] <= s) and (a[i] + b[j] > ans) then ans:= a[i] + b[j]; write(ans); END.
Disclaimer: This problem (Counting Valleys) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.