-1

I'm aware of the modular arithmetic approach to solve this question, I just took it upon myself to solve it in the more naive way, but I just created an infinite loop somewhere in the inner for loop and there is no way out (Please do not tell me to do it in the standard way, This is just a challenge that I made upon myself)

The function return the size of a maximal subset of S where the sum of any 2 numbers in s’ is not evenly divisible by k.

here is my code that causes an infinite loop, so be careful

function nonDivisibleSubset(arr, k) {
  let maxElements = 0;
  for (let i = 0; i < arr.length; i++) {
    console.log(i);
    // arr 1 to (arr.length - 1)
    let result = [];
    let noOfElements = 0;
    for (let j = 0 + 1; j < arr.length; j++) {
      console.log(`hi${j}`);
      //arr 2 to arr.length
      console.log(result.length);
      if (
        result.length === 0 &&
        (arr[i] + arr[j]) % k !== 0
      ) {
        console.log("hi2");
        result.push(arr[i], arr[j]);
        noOfElements += 2;
      } else {
        let isDivisible = false;
        console.log(`result length: ${result.length}`);
        for (let l = 0; l < result.length; l++) {
          console.log(l);
          if (i !== l && (arr[l] + arr[j]) % 4 === 0) {
            isDivisible = true;
            break;
          }
          if (!isDivisible) {
            result.push(arr[l]);
            console.log(result);
            noOfElements += 1;
            break; //removing this break will initialize the infinite loop
          }
        }
      }
    }
    if (noOfElements > maxElements) {
      maxElements = noOfElements;
    }
  }
  return maxElements;
}

//initialise
nonDivisibleSubset([19, 10, 12, 10, 24, 25, 22], 4)
  • 1
    The problem is that you push onto the `result` array inside the array that loops over it. The array keeps getting longer and you can't catch up. If you want to loop over only the elements that were in the array before the loop started, assign a variable to `result.length` and use that as the loop limit. – Barmar Jan 26 '23 at 04:31
  • Can someone please code a complete function that works and returns the desired output following the naive approach? – supremeincubator Jan 26 '23 at 05:01
  • 1
    Even if you fix the infinite loop problem, this algorithm idea cannot always solve the problem. It is a greedy approach that does not consider that **not** adding a particular viable value might open the door to a better solution in the end. So even if we would answer how to avoid the infinite loop, you'll still not have a working algorithm. As you know about a good algorithm, I'm not sure what you expect now... – trincot Jan 28 '23 at 11:49

1 Answers1

0

The resul array continues to increase in size as the repeat statement spins.

Here is an example of how you can do it:


for (let l = 0, size = result.length; l < size; l++) {
    if (i !== l && (arr[l] + arr[j]) % 4 === 0) {
        isDivisible = true;
        break;
    }
    if (!isDivisible) {
        result.push(arr[l]);
        console.log(result);
        console.log(result.length);
        noOfElements += 1;
        break; //removing this break will initialize the infinite loop
    }
}

And,


if (i !== l && (arr[l] + arr[j]) % 4 === 0)

Isn't it k, not 4?


if (i !== l && (arr[l] + arr[j]) % k === 0)

I hope it helps.

paransaik
  • 96
  • 1
  • 6