-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathElectronicsShop.java
More file actions
53 lines (48 loc) · 1.34 KB
/
Copy pathElectronicsShop.java
File metadata and controls
53 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
*
* Problem Statement-
* [Electronics Shop](https://www.hackerrank.com/challenges/electronics-shop/problem)
*
*/
package com.javaaid.hackerrank.solutions.generalprogramming.basicprogramming;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class ElectronicsShop {
static int getMoneySpent(int[] keyboards, int[] drives, int s) {
int max = 0;
int kb = 0, mo = 0;
for (int i = 0; i < keyboards.length; i++) {
for (int j = 0; j < drives.length; j++) {
int tmp = keyboards[i] + drives[j];
if (tmp >= max && tmp <= s) {
kb = i;
mo = j;
max = tmp;
}
}
}
return ((kb == 0 && mo == 0) ? -1 : 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();
int[] keyboards = new int[n];
for (int keyboards_i = 0; keyboards_i < n; keyboards_i++) {
keyboards[keyboards_i] = in.nextInt();
}
int[] drives = new int[m];
for (int drives_i = 0; drives_i < m; drives_i++) {
drives[drives_i] = 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);
in.close();
}
}