Skip to content

Circle of Numbers

Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighboring numbers is equal (note that 0 and n - 1 are neighboring, 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

circle_of_numbers(n, firstNumber) = 7
Image Credit: CodeSignal

Solution

py
def circle_of_numbers(n, first_number):
    return (first_number + n // 2) % n


print(circle_of_numbers(10, 2))
js
function circleOfNumbers(n, first_number) {
  return (first_number + n / 2) % n;
}

console.log(circleOfNumbers(10, 2));

my thoughts are neither my employer's nor my wife's