Circle of Numbers

Description


Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighbouring numbers is equal (note that 0 and n - 1 are neighbouring, too).

Given n and firstNumber, find the number which is written in the radially opposite position to firstNumber.

Example

For n = 10 and firstNumber = 2, the output should be circleOfNumbers(n, firstNumber) = 7.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] integer n

A positive even integer.

Guaranteed constraints: 4 ≤ n ≤ 20.

  • [input] integer firstNumber

Guaranteed constraints: 0 ≤ firstNumber ≤ n - 1.

  • [output] integer

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
    console.log("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solution


1
2
3
function circleOfNumbers(n, firstNumber) {
    return (n/2 + firstNumber)%n
}