HackerRank Electronics Shop problem solution

In this 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.

HackerRank Electronics Shop problem solution

Problem solution in Python programming.

#!/bin/python3

import sys


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

total = -1

for x in keyboards:
    for y in pendrives:
        z = x + y
        if z > total and z <= s:
            total = z
         
print(total)
            




Problem solution in Java Programming.

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 sum = keyboards[i] + pendrives[j];
                if (sum > s)  continue;
                ans = Math.max(ans, sum);
            }  
        }
        System.out.println(ans);
    }
}

Problem solution in C++ programming.

#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;
}

Problem solution in C programming.

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

Problem solution in JavaScript programming.

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 max = 0;
    for (var i=0; i<n; i++) {
        for (var j=0; j<m; j++) {
            if (keyboards[i] + pendrives[j] > max && s >= keyboards[i] + pendrives[j]) max = keyboards[i] + pendrives[j];
        }
    }   
    (max === 0) ? console.log('-1') : console.log(max);
}

1 thought on “HackerRank Electronics Shop problem solution”

Comments are closed.