package algorithm;
import java.util.*;
public class Q43 {
public static void main(String[] args) {
System.out.println(solution(8,new int[]{5,6,7},new int[]{1,4,5}));
}
public static int solution(int n, int[] lost, int[] reserve) {
List<Integer> losts = new ArrayList<>();
List<Integer> reserves = new ArrayList<>();
for(int x: lost){
losts.add(x);
}
for (int y: reserve){
reserves.add(y);
}
for(int i=0; i<reserves.size(); i++){
if (losts.contains(reserves.get(i))){
int duplicate = reserves.get(i);
losts.remove(Integer.valueOf(duplicate));
reserves.remove(Integer.valueOf(duplicate));
}
}
Collections.sort(losts);
Collections.sort(reserves);
for (int x : reserves) {
for (int j = 0; j < losts.size(); j++) {
int y = losts.get(j);
int isPresent = x - y;
if (isPresent == 0 || isPresent == -1 || isPresent == 1) {
losts.remove(Integer.valueOf(y));
break;
}
}
}
return n - losts.size();
}
}