JSFiddle - React, Tailwind, and code Playground

by Santosh Thakur

JavaScript

function fishAlive(A, B) {

  let len = A.length;
  let C = [];
  let D = [];
  let killed = 0;
  for (let i = 0; i < len; i++) {
    let j = len - 1 - i;
    if (i <= j || 1) {
      if (B[i] === 1) {
        C.push({
          strength: A[i],
          index: i
        });
      } else if (C.length) {
        killed++;
        if (C[C.length - 1].strength < A[i]) {

          A.splice(C[C.length - 1].index, 1);
          B.splice(C[C.length - 1].index, 1);
          len--;
          i--;
          C.pop();
        } else {
          A.splice(i, 1);
          B.splice(i, 1);
          len--;
          i--;
        }
      }