-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxNumberOfPairToCreateAccessCode.js
More file actions
42 lines (31 loc) · 1.11 KB
/
Copy pathMaxNumberOfPairToCreateAccessCode.js
File metadata and controls
42 lines (31 loc) · 1.11 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
// ********* EBay OA Question ************
// You have an array of integers (example: [1, 2, 3, 4]).
// You have an access code (example: 1234).
// You want to check how many maximum times you can create that access code by concatenating array elements without reusing the same position twice.
function maxAccessCodePairs(arr, accessCode) {
const target = accessCode.toString();
const freq = {};
// Build frequency map
for (let num of arr) {
const str = num.toString();
freq[str] = (freq[str] || 0) + 1;
}
let total = 0;
// Try every split of target into prefix + suffix
for (let i = 1; i < target.length; i++) {
const prefix = target.slice(0, i);
const suffix = target.slice(i);
if (freq[prefix] && freq[suffix]) {
if (prefix === suffix) {
// choose 2 distinct indices → n*(n-1)
total += freq[prefix] * (freq[prefix] - 1);
} else {
total += freq[prefix] * freq[suffix];
}
}
}
return total;
}
// Example
console.log(maxAccessCodePairs([1, 212, 12], 1212)); // → 3
console.log(maxAccessCodePairs([12, 34, 56, 12, 34], 1234)); // → 4