<script>
function
PriorityQueue () {
this
.collection = [];
this
.printCollection =
function
() {
console.log(
this
.collection);
};
this
.enqueue=
function
(item){
if
(
this
.collection.length===0) {
this
.collection.push(item);
}
else
if
(
this
.collection.length===1){
if
(item[1]>=
this
.collection[0][1]){
this
.collection.push(item);
}
else
if
(item[1]<
this
.collection[0][1]){
this
.collection.unshift(item);
}
}
else
if
(
this
.collection.length>1){
let check=
false
;
for
(let i=0;i<
this
.collection.length;i++){
if
(
this
.collection[i][1]>item[1]){
if
(i===0){
check=
true
;
this
.collection.unshift(item);
break
;
}
else
{
check=
true
;
let sliced=
this
.collection.slice(0,i);
let slicedend=
this
.collection.slice(i,
this
.collection.length);
sliced.push(item);
this
.collection=sliced.concat(slicedend);
break
;
}
}
}
if
(!check){
this
.collection.push(item);
}
};
this
.dequeue =
function
(){
return
this
.collection.shift()[0];
};
this
.size =
function
(){
return
this
.collection.length;
};
this
.isEmpty=
function
(){
return
this
.collection.length===0?
true
:
false
;
};
this
.front=
function
(){
return
this
.collection[0];
};
}
}
function
len(x)
{
let temp = String(x);
return
temp.length;
}
function
canbothEqual(N, K, A, B)
{
let First =
new
PriorityQueue();
for
(let i = 0; i < N; i++) {
First.enqueue(A[i]);
}
let Second =
new
PriorityQueue();
for
(let i = 0; i < N; i++) {
Second.enqueue(B[i]);
}
let counter = 0;
while
(!First.isEmpty() && !Second.isEmpty()) {
let num1 = First.front();
let num2 = Second.front();
if
(num1 > num2) {
First.enqueue(len(num1));
First.dequeue();
counter++;
}
else
if
(num2 > num1) {
Second.enqueue(len(num2));
Second.dequeue();
counter++;
}
else
{
First.dequeue();
Second.dequeue();
}
}
if
(K >= counter)
return
true
;
return
false
;
}
N = 3;
K = 3;
let A = [ 2, 9, 3 ];
let B = [ 1, 100, 9 ];
if
(canbothEqual(N, K, A, B))
console.log(
"Equal"
);
else
console.log(
"Unequal"
);
</script>