Electronics Shop


Problem Statement :


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

Example
b = 60
keyboards = [40, 50, 60]
drives = [5, 8,12]

The person can buy a 40 keyboards + 12 USB drives = 52, or a 50 keyboards + 8 USB drives = 58. Choose the latter as the more expensive option and return 58.


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


Constraints
1 <= n,m <= 1000
1 <= b <= 10^6
The price of each item is in the inclusive range [1, 10^6].



Solution :



title-img


                            Solution in C :

python 3 :

#!/bin/python3

import sys


s,n,m = input().strip().split(' ')
s,n,m = [int(s),int(n),int(m)]
a = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
b = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
ans = -1
for x in a:
    for y in b:
        if x + y <= s:
            ans = max(ans, x + y)
print (ans)








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 max=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(keyboards[i]+pendrives[j]<=s){
                  if(max<keyboards[i]+pendrives[j]){
                      max=keyboards[i]+pendrives[j];
                  }  
                }
            }
        }
        System.out.println(max==0?-1:max);
        
    }
}










C++  :

#include <bits/stdc++.h>

using namespace std;

int ans=-1, a[1005], b[1005], i, n, m, s, j;

int main()
{

    scanf("%d%d%d", &s, &n, &m);
    for(i=1; i<=n; ++i) scanf("%d", &a[i]);
    for(i=1; i<=m; ++i) scanf("%d", &b[i]);

    for(i=1; i<=n; ++i)
    for(j=1; j<=m; ++j)
        if(a[i]+b[j]<=s) ans = max(a[i]+b[j], ans);

    printf("%d\n", ans);

    return 0;
}









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








View More Similar Problems

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →

Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →

Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →

Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →