Skip to content

3178. Find the Child Who Has the Ball After K Seconds 👍

  • Time: $O(1)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
9
class Solution {
 public:
  int numberOfChild(int n, int k) {
    // the time for the ball to return to 0
    const int roundTime = 2 * (n - 1);
    const int pos = k % roundTime;
    return pos < n ? pos : roundTime - pos;
  }
};
1
2
3
4
5
6
7
8
class Solution {
  public int numberOfChild(int n, int k) {
    // the time for the ball to return to 0
    final int roundTime = 2 * (n - 1);
    final int pos = k % roundTime;
    return pos < n ? pos : roundTime - pos;
  }
}
1
2
3
4
5
6
class Solution:
  def numberOfChild(self, n: int, k: int) -> int:
    # the time for the ball to return to 0
    roundTime = 2 * (n - 1)
    pos = k % roundTime
    return pos if pos < n else roundTime - pos