We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

# Electronics Shop

# Electronics Shop

+ 86 comments **Java Solution**Here is my solution in Java that passes all cases in O(n log(n)) time with O(1) additional space.**Here is how it works:**We sort keyboards in descending order. We sort usb in ascending order. Then we iterate over them not checking usbs past where 1 usb plus our current keyboard is already greater than s. Read the comments in my solution for a more in depth explanation.Some times HackeRank redirect doesn't work, so here is the direct link you will have to copy and paste: https://github.com/RyanFehr/HackerRank/blob/master/Algorithms/Implementation/Electronics%20Shop/Solution.java

+ 11 comments My python 3 solution:

def getMoneySpent(keyboards, drives, b): l = [] for x in keyboards: for y in drives: if x+y <= b: l.append(x+y) if not l: return -1 else: return max(l)

+ 9 comments Well, did it the pythonic 1 line way

def getMoneySpent(keyboards, drives, s): return max([sum([x,y]) for x in keyboards for y in drives if sum([x,y]) <= s]+[-1])

+ 19 comments //Try This One... //Coded By Himanshu And Jatin.... //Chandigarh University.... #include <bits/stdc++.h> using namespace std; int main() { int s; int n; int m; cin >> s >> n >> m; int k[n]; int z=-1; int u[m]; for(int i=0;i<n;i++) { cin >> k[i]; } for(int i=0;i<m;i++) { cin >> u[i]; } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(k[i]+u[j]<=s && k[i]+u[j]>z) z=k[i]+u[j]; } } cout<<z; return 0; }

+ 3 comments Javascript solution:

function getMoneySpent(keyboards, drives, b) { return keyboards.reduce((acc, curr) => Math.max(acc, ...drives.map(usb => usb + curr).filter(ku => b >= ku)) , -1); }

Load more conversations

Sort 1775 Discussions, By:

Please Login in order to post a comment