Electronics Shop HackerRank Solution in C, C++, Java, Python

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 .


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


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


  • The price of each item is in the inclusive range .

Sample Input 0

10 2 3
3 1
5 2 8

Sample Output 0



Electronics Shop HackerRank 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; 
    scanf("%d %d %d",&s,&n,&m);
    int *keyboards = malloc(sizeof(int) * n);
    for(int keyboards_i = 0; keyboards_i < n; keyboards_i++){
    int *pendrives = malloc(sizeof(int) * m);
    for(int pendrives_i = 0; pendrives_i < m; pendrives_i++){
    int max_spend = -1, i, j, cost;
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            cost = keyboards[i] + pendrives[j];
            if (cost > s) {
            if (cost > max_spend) {
                max_spend = cost;
    printf("%d\n", max_spend);
    return 0;

Electronics Shop HackerRank Solution in C++

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
typedef long long int uli;
using namespace std;

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];
    uli ans=-1;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            uli bet=keyboards[i]+pendrives[j];
                if(ans==-1 || bet>ans)ans=bet;
    return 0;

Electronics Shop HackerRank 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;

Electronics Shop HackerRank Solution in 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(' '))
max = -1
for i in keyboards:
    for j in pendrives:
        sum = i+j
        if sum<=s:
            if sum>max:
                max = sum
print max


Attempt Electronics Shop HackerRank Challenge

Link – https://www.hackerrank.com/challenges/electronics-shop

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/cat-and-a-mouse-hackerrank-solution/

Leave a Comment