Codility – FrogRiverOne Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Codility – FrogRiverOne without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’ve googled and asked people about this question, but can’t get the meaning. I know my solution is incorrect, but I hope it can help (it’s on JS):

let C = [1, 3, 1, 4, 2, 3, 5, 4],
  Y = 4;

function solution(X, A) {

  var leaves = [];
  var i = 0;
  var result = -1;

  for (i = 0; i < A.length; i++) {
    if (typeof leaves[A[i]] == 'undefined') {
      leaves[A[i]] = i;
    }
  }

  if (leaves.length <= X) {
    return -1;
  }

  for (i = 1; i <= X; i++) {
    if (typeof leaves[i] == 'undefined') {
      return -1;
    } else {
      result = Math.max(result, leaves[i]);
    }
  }

  return result;
}

console.log(solution(Y, C));

I don’t need a solution to the task, I just need its clearer explanation, i.e what I have to do to solve it.

Answer

Okay, I figured out your problem for you. Your solution is actually almost correct, but you overcomplicated the evaluation. All you have to do is initialize a counter variable to 0, and as you iterate over A in the first loop, whenever leaves[A[i]] is undefined, increment this counter. This indicates that a leaf has fallen into a position where there is no leaf yet. If after incrementing the counter is equal to X, it means all positions now have leaves on them, so just return i. If your loop makes it all the way to the end of A, it means there are uncovered positions, so return -1.

We are here to answer your question about Codility – FrogRiverOne - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji