JSFiddle - React, Tailwind, and code Playground

by nomadev

JavaScript

// Complete the permutationEquation function below.
function permutationEquation(p) {
    return p.map(v => {
        var v1 = p[v - 1];
        return p[v1 - 1];
    });

}

// console.log(permutationEquation([4, 3, 5, 1, 2]));
console.log(permutationEquation([5,2,1,3,4]));

// n = 5
// 1 3 5 4 2 -- good
// 4 2 3 1 5 -- bad
//  where .	p[i] = 1 <= i <= n

// prisoners, candies, fromSeat
function saveThePrisoner(n, m, s) {
	// init seat to offset
    let seat = s;
    // if the number of candies + the offset - 1 < number of chairs
    // return the currentOffset + 1
    if (m + s - 1 < n)
    	return seat + 1;
      // otherwise sum candies + currentSeat - 1 module of n
    seat = (m + s - 1) % n;
    // if the result is 0, return the last chair
    return seat > 0 ? seat : n;
}


//console.log(saveThePrisoner(10, 54680780 , 10));