Bob is about to go on a trip. But first he needs to take care of his supply of socks. Each sock has its own color. Bob wants to take as many pairs of clean socks as possible (both socks in the pair should be of the same color).
Socks are divided into two drawers: clean and dirty socks. Bob has time for only one laundry and his washing machine can clean at most K socks. He wants to pick socks for laundering in such a way that after washing he will have a maximal number of clean, same-colored pairs of socks. It is possible that some socks cannot be paired with any other sock, because Bob may have lost some socks over the years.
Bob has exactly N clean and M dirty socks, which are described in arrays C and D, respectively. The colors of the socks are represented as integers (equal numbers representing identical colors).
For example, given four clean socks and five dirty socks:
If Bob's washing machine can clean at most K = 2 socks, then he can take a maximum of three pairs of clean socks. He can wash one red sock and one green sock, numbered 1 and 2 respectively. Then he will have two pairs of red socks and one pair of green socks.
Write a function:
function solution(K, C, D);
that, given an integer K (the number of socks that the washing machine can clean), two arrays C and D (containing the color representations of N clean and M dirty socks respectively), returns the maximum number of pairs of socks that Bob can take on the trip.
For example, given K = 2, C = [1, 2, 1, 1] and D = [1, 4, 3, 2, 4], the function should return 3, as explained above.
Assume that:
- K is an integer within the range [0..50];
- each element of arrays C and D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let cleanCount = [];
let dirtyCount = [];
let pairs = 0;
for (let i = 0; i < A.length; i++) {
}
for (let i = 0; i < D.le)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let cleanCount = [];
let dirtyCount = [];
let pairs = 0;
for (let i = 0; i < C.length; i++) {
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let cleanCount = Array.from({})
let dirtyCount = [];
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let cleanCount = Array.from({ length: C.length + 1 }, () => 0)
let dirtyCount = [];
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let cleanCount = Array.from({ length: C.length + 1 }, () => 0);
let dirtyCount = [];
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let cleanCount = Array.from({ length: C.length + 1 }, () => 0);
let dirtyCount = Array.from({ length: D.length + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let
let cleanCount = Array.from({ length: C.length + 1 }, () => 0);
let dirtyCount = Array.from({ length: D.length + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: C.length + 1 }, () => 0);
let dirtyCount = Array.from({ length: D.length + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < max; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < max; i++) {
if (C[i]) {
}
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]] = 0;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
}
for (let i = 0; i < D.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0)
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
for (let i = 1)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
for (let i = 1; )
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
for (let i = 1; i < )
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
for (let i = 1; i < max + 1; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 0; i < max + 1) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 0; i < max + 1; i++) {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 0; i < max + 1; i++) {
if
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 0; i < max + 1; i++) {
if (cleanCount % 2)
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] % 2 === 0) {
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 0; i < max + 1; i++) {
if (cleanCount % 2 === 1) {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 0) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 0; i < max + 1; i++) {
if (cleanCount % 2 === 1) {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 0; i < max + 1; i++) {
if (cleanCount % 2 === 1) {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 0; i < max + 1; i++) {
if (cleanCount[i] === 1) {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 1; i < max + 1; i++) {
if (cleanCount[i] === 1) {
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 1; i < max + 1; i++) {
if (cleanCount[i] === 1 && dirtyCount) {
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
while (K > 0) {
for (let i = 1; i < max + 1; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
while (toWas > 0) {
for (let i = 1; i < max + 1; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
K--
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
while (toWash < K) {
for (let i = 1; i < max + 1; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K; i++) {
while (dirtyCount[i] >= 2) {
dirtyCount[i]
pairs++;
toWash++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K; i++) {
while (dirtyCount[i] >= 2) {
dirtyCount[i] -= 2;
pairs++;
toWash++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K; i++) {
while (dirtyCount[i] >= 2 && toWash) {
dirtyCount[i] -= 2;
pairs++;
toWash++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K; i++) {
while (dirtyCount[i] >= 2 && toWash < K) {
dirtyCount[i] -= 2;
pairs++;
toWash += 2;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K; i++) {
while (dirtyCount[i] >= 2 && toWash < K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K-1; i++) {
while (dirtyCount[i] >= 2 && toWash < K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K - 1; i++) {
while (dirtyCount[i] >= 2 && toWash < K - 1) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash < K; i++) {
while (dirtyCount[i] >= 2 && toWash < K - 1) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
[0, [1, 1, 1, 1, 2], [2]]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
[0, [1, 1, 1, 1, 2], [2]]
[1, [1, 1, 1, 1, 2], [2]]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
[0, [1, 1, 1, 1, 2], [2]]
[1, [1, 1, 1, 1, 2], [2]]
[10, [1, 1, 1, 1, 2], [2]]
function result: 2
function result: 3
function result: 3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
[0, [1, 1, 1, 1, 2], [2]]
[1, [1, 1, 1, 1, 2], [2]]
[10, [1, 1, 1, 1, 2], [2]]
[10, [1], [2, 2, 2, 2, 2, 2, 50, 50]]
function result: 2
function result: 3
function result: 3
function result: 4
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
[0, [1, 1, 1, 1, 2], [2]]
[1, [1, 1, 1, 1, 2], [2]]
[10, [1, 1, 1, 1, 2], [2]]
[10, [1], [2, 2, 2, 2, 2, 2, 50, 50]]
function result: 2
function result: 3
function result: 3
function result: 4
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(K, C, D) {
// write your code in JavaScript (Node.js 8.9.4)
let max = Math.max(...C, ...D)
let cleanCount = Array.from({ length: max + 1 }, () => 0);
let dirtyCount = Array.from({ length: max + 1 }, () => 0);
let pairs = 0;
for (let i = 0; i < C.length; i++) {
cleanCount[C[i]]++;
if (cleanCount[C[i]] === 2) {
cleanCount[C[i]] = 0;
pairs++;
}
}
for (let i = 0; i < D.length; i++) {
dirtyCount[D[i]]++;
}
let toWash = 0
for (let i = 1; i < max + 1 && toWash < K; i++) {
if (cleanCount[i] === 1 && dirtyCount[i]) {
dirtyCount[i]--;
pairs++;
toWash++;
}
}
for (let i = 1; i < max + 1 && toWash + 2 <= K; i++) {
while (dirtyCount[i] >= 2 && toWash + 2 <= K) {
dirtyCount[i] -= 2;
toWash += 2;
pairs++;
}
}
return pairs;
}
The solution obtained perfect score.
tests causing solution that's only pairing clean socks of odd count with dirty socks of odd count to fail
tests causing solution that's not saving if dirty sock was used to pair with clean sock to fail