Skip to content
TheCScience
TheCScience
  • Engineering Subjects
    • Human Values
    • Computer System Architecture
    • Digital Communication
    • Internet of Things
  • NCERT Solutions
    • Class 12
    • Class 11
  • HackerRank solutions
    • HackerRank Algorithms Problems Solutions
    • HackerRank C solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
TheCScience
TheCScience

HackerRank Electronics Shop Problem Solution

Yashwant Parihar, April 13, 2023April 13, 2023

In this post, We are going to solve HackerRank Electronics Shop Problem. A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a given 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]

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 drivers, the prices of the USB drives.

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 Problem Solution
HackerRank Electronics Shop Problem Solution

Electronics Shop C Solution

#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; 
    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]);
    }
    int temp=-1;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(temp<keyboards[i] + pendrives[j] && keyboards[i]+ pendrives[j]<=s)
                temp=keyboards[i] + pendrives[j];
        }
    }
    printf("%d",temp);
    return 0;
}

Electronics Shop C++ Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 11234;
int k[N], u[N];

int main() {
    int s, n, m;
    cin >> s >> n >> m;
    for(int i = 1; i <= n; ++i) cin >> k[i];
    for(int i = 1; i <= m; ++i) cin >> u[i];
    int ans = -1;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= m; ++j){
            int x = k[i] + u[j];
            if(x <= s && ans <= x)
                ans = x;
        }
    }
    cout << ans << '\n';
}

Electronics Shop C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution 
{
    static void Main(String[] args) 
    {        
        string[] tokens_s = Console.ReadLine().Split(' ');
        int s = Convert.ToInt32(tokens_s[0]);
        int n = Convert.ToInt32(tokens_s[1]);
        int m = Convert.ToInt32(tokens_s[2]);
        string[] keyboards_temp = Console.ReadLine().Split(' ');
        int[] keyboards = Array.ConvertAll(keyboards_temp,Int32.Parse);
        string[] pendrives_temp = Console.ReadLine().Split(' ');
        int[] pendrives = Array.ConvertAll(pendrives_temp,Int32.Parse);
        
        int ans = -1;
        
        for(int i = 0; i<keyboards.Length; i++)
            for(int j = 0; j<pendrives.Length; j++)
            {
            int cost = keyboards[i] + pendrives[j];
            if (cost <= s && cost > ans)
            {
                ans = cost;    
            }
        }
        
        Console.WriteLine(ans);
    }
}

Electronics Shop Java Solution

import java.io.*;
import java.util.*;

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    static int getMoneySpent(List<Integer> keyboards, List<Integer> drives, int s){
            int max=-1;
        for(int i=keyboards.size()-1;i>=0;i--)
            {
            for(int j=drives.size()-1;j>=0;j--)
                {
                if(keyboards.get(i)+drives.get(j)<=s && max<keyboards.get(i)+drives.get(j))
                    max=keyboards.get(i)+drives.get(j);
            }
        }
        return max;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int s = in.nextInt();
        int n = in.nextInt();
        int m = in.nextInt();
        ArrayList<Integer> keyboards = new ArrayList<Integer>(n);
        for(int keyboards_i=0; keyboards_i < n; keyboards_i++){
            keyboards.add(in.nextInt());
        }
        ArrayList<Integer> drives = new ArrayList<Integer>(m);
        for(int drives_i=0; drives_i < m; drives_i++){
            drives.add(in.nextInt());
        }
           
        //  The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
        int moneySpent = getMoneySpent(keyboards, drives, s);
        System.out.println(moneySpent);
    }
}

Electronics Shop JavaScript Solution

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, n, m ] = readLine().split(' ').map(Number);
    keyboards = readLine().split(' ').map(Number);
    pendrives = readLine().split(' ').map(Number);

    var maxS = -1;
    for( let i = 0; i < n; i++ ) {
        for( let j = 0; j < m; j++ ) {
            var sum = keyboards[ i ] + pendrives[ j ];
            if ( sum <= s && sum > maxS ) {
                maxS = sum;
            }
            
            if ( sum === s ) break;
        }
        
        if ( sum === s ) break;
    }
    
    console.log( maxS );
}

Electronics Shop Python Solution

#!/bin/python3

import sys


s,n,m = input().strip().split(' ')
s,n,m = [int(s),int(n),int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]

valMax = -1
for key in keyboards:
    for mouse in pendrives:
        total = key+mouse
        if total > valMax and total <= s:
            valMax = total
            
print(valMax)

Other Solution

  • HackerRank Cats and a Mouse Problem Solution
  • HackerRank Forming a Magic Square Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

TheCScience

We at TheCScience.com are working towards the goal to give free education to every person by publishing in dept article about Secondary, Senior-Secondary, and Graduation level subjects.

Pages

About US

Contact US

Privacy Policy

DMCA

Engineering Subjects

Internet of Things

Human Values

Digital Communication

Computer System Architecture

Programming Tutorials

Data Structure and Algorithm

C

Java

NCERT

Class 12th

©2026 TheCScience | WordPress Theme by SuperbThemes