Files

47 lines
1.4 KiB
Java
Raw Permalink Normal View History

2017-03-24 00:02:30 -05:00
// Author: Rodney Shaghoulian
2018-07-27 16:45:56 -07:00
// Github: github.com/RodneyShag
// HackerRank: hackerrank.com/RodneyShag
2017-03-24 00:02:30 -05:00
import java.util.Scanner;
2017-04-05 18:42:35 -05:00
// For an element in A, if there's a matching element in B, this creates a "beautiful pair".
// Each element can only be used once to create a beautiful pair.
2017-03-24 00:02:30 -05:00
2017-04-05 18:42:35 -05:00
// Additionaly, We MUST change exactly 1 element in B. We attempt to change it to create
// 1 more beautiful pair. In the special case where we already have the max number of
// beautiful pairs, being forced to change it gives us 1 less beautiful pair.
2017-03-24 00:02:30 -05:00
// Time Complexity: O(n)
// Space Complexity: O(1)
public class Solution {
private static final int MAX_NUM = 1000; // max value of any number in array
2017-09-11 09:32:17 -07:00
public static void main(String[] args) {
2017-03-24 00:02:30 -05:00
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int [] bucketA = new int[MAX_NUM + 1];
for (int i = 0; i < N; i++) {
bucketA[scan.nextInt()]++;
}
int beautifulPairs = 0;
for (int i = 0; i < N; i++) {
int num = scan.nextInt();
if (bucketA[num] > 0) {
bucketA[num]--;
beautifulPairs++;
}
}
scan.close();
/* Accounts for changing 1 element in B */
if (beautifulPairs == N) {
beautifulPairs--;
} else {
beautifulPairs++;
}
System.out.println(beautifulPairs);
}
}