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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Electronics Shop
  5. Discussions

Electronics Shop

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1775 Discussions, By:

votes

Please Login in order to post a comment

  • ryanfehr18
    5 years ago+ 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

    213|
    Permalink
    View more Comments..
  • SergZerg
    3 years ago+ 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)
    
    38|
    Permalink
    View more Comments..
  • LeHarkunwar
    5 years ago+ 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])
    
    23|
    Permalink
    View more Comments..
  • Himanshu_1061
    5 years ago+ 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;
    }
    
    18|
    Permalink
    View more Comments..
  • mbournehalley
    4 years ago+ 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);
    }
    
    17|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature