forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
40 lines (35 loc) · 1.23 KB
/
solution.java
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
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 b = in.nextInt();
int n = in.nextInt();
int m = in.nextInt();
Integer[] keyboards = new Integer[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();
}
// If we sort one in descending and the other in ascending
Arrays.sort(keyboards, Collections.reverseOrder());// Descending order
Arrays.sort(drives);// Ascending order
int max = -1;
for(int i = 0, j = 0; i < n; i++){
for(; j < m; j++){
if(keyboards[i]+drives[j] > b) break; // This prevents j from incrementing
if(keyboards[i]+drives[j] > max)
max = keyboards[i]+drives[j];
}
}
System.out.println(max);
}
}
// Time Complexity: O(n+m(log(n+m)))
// Space Complexity: O(1)