Repeating sequence can be solved with mathematical formulae for O(1) complexity [Kotlin]
IntuitionThe problem describes a simple sequence of numbers that start from zero, increase to a maximum value and then decline again to zero.
The maximum value
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem describes a simple sequence of numbers that start from zero, increase to a maximum value and then decline again to zero.
The maximum value is the number of transitions in any direction before you reach the last child in the chain and are forced to reverse directions.

# Approach
<!-- Describe your approach to solving the problem. -->
The number of transtions before you reach the end of the chain is give by n - 1

max_transitions = n-1

Starting from zero, you return again to zero after doing two sets of transtitions, one going from left to right, and then going back from right to left. Let's call this the full loop length.

full_loop_length = 2* max_transitions

Take the modulus of k against the full loop length. 

mod_val = k % full_loop_length

If the value returned is less than or equal to the max-transitions, the ball is in the forward leg of the loop, and hence the modulus itself gives the current location of the ball. 
If the modules is greater than max-tranistion, the ball is on the reverse leg of the loop. Here you subtract the modulus value from the loop length to get the current location.

if mod_val <= max_transitions
    return mod_val
else 
    return full_loop_length - mod_val


# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(1) as it is a couple of simple mathematical calculations.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
Three new variables are created to keep the code readable. So space complexity is also O(3) which simplifies to O(1)

# Code
```kotlin []
class Solution {
    fun numberOfChild(n: Int, k: Int): Int {
        val transitions = n - 1
        val loopLength = 2*transitions
        val modulus = k%loopLength

        if(modulus < transitions)
            return modulus
        else
            return loopLength - modulus     
    }
}
```
['Kotlin']