|
| 1 | +# [2682. Find the Losers of the Circular Game](https://leetcode.com/problems/find-the-losers-of-the-circular-game) |
| 2 | + |
| 3 | +[中文文档](/solution/2600-2699/2682.Find%20the%20Losers%20of%20the%20Circular%20Game/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 <= i < n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p> |
| 8 | + |
| 9 | +<p>The rules of the game are as follows:</p> |
| 10 | + |
| 11 | +<p><code>1<sup>st</sup></code> friend receives the ball.</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>After that, <code>1<sup>st</sup></code> friend passes it to the friend who is <code>k</code> steps away from them in the <strong>clockwise</strong> direction.</li> |
| 15 | + <li>After that, the friend who receives the ball should pass it to the friend who is <code>2 * k</code> steps away from them in the <strong>clockwise</strong> direction.</li> |
| 16 | + <li>After that, the friend who receives the ball should pass it to the friend who is <code>3 * k</code> steps away from them in the <strong>clockwise</strong> direction, and so on and so forth.</li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<p>In other words, on the <code>i<sup>th</sup></code> turn, the friend holding the ball should pass it to the friend who is <code>i * k</code> steps away from them in the <strong>clockwise</strong> direction.</p> |
| 20 | + |
| 21 | +<p>The game is finished when some friend receives the ball for the second time.</p> |
| 22 | + |
| 23 | +<p>The <strong>losers</strong> of the game are friends who did not receive the ball in the entire game.</p> |
| 24 | + |
| 25 | +<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the array answer, which contains the losers of the game in the <strong>ascending</strong> order</em>.</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | +<p><strong class="example">Example 1:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> n = 5, k = 2 |
| 32 | +<strong>Output:</strong> [4,5] |
| 33 | +<strong>Explanation:</strong> The game goes as follows: |
| 34 | +1) Start at 1<sup>st</sup> friend and pass the ball to the friend who is 2 steps away from them - 3<sup>rd</sup> friend. |
| 35 | +2) 3<sup>rd</sup> friend passes the ball to the friend who is 4 steps away from them - 2<sup>nd</sup> friend. |
| 36 | +3) 2<sup>nd</sup> friend passes the ball to the friend who is 6 steps away from them - 3<sup>rd</sup> friend. |
| 37 | +4) The game ends as 3<sup>rd</sup> friend receives the ball for the second time. |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p><strong class="example">Example 2:</strong></p> |
| 41 | + |
| 42 | +<pre> |
| 43 | +<strong>Input:</strong> n = 4, k = 4 |
| 44 | +<strong>Output:</strong> [2,3,4] |
| 45 | +<strong>Explanation:</strong> The game goes as follows: |
| 46 | +1) Start at the 1<sup>st</sup> friend and pass the ball to the friend who is 4 steps away from them - 1<sup>st</sup> friend. |
| 47 | +2) The game ends as 1<sup>st</sup> friend receives the ball for the second time. |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | +<p><strong>Constraints:</strong></p> |
| 52 | + |
| 53 | +<ul> |
| 54 | + <li><code>1 <= k <= n <= 50</code></li> |
| 55 | +</ul> |
| 56 | + |
| 57 | +## Solutions |
| 58 | + |
| 59 | +<!-- tabs:start --> |
| 60 | + |
| 61 | +### **Python3** |
| 62 | + |
| 63 | +```python |
| 64 | +class Solution: |
| 65 | + def circularGameLosers(self, n: int, k: int) -> List[int]: |
| 66 | + vis = [False] * n |
| 67 | + i, p = 0, 1 |
| 68 | + while not vis[i]: |
| 69 | + vis[i] = True |
| 70 | + i = (i + p * k) % n |
| 71 | + p += 1 |
| 72 | + return [i + 1 for i in range(n) if not vis[i]] |
| 73 | +``` |
| 74 | + |
| 75 | +### **Java** |
| 76 | + |
| 77 | +```java |
| 78 | +class Solution { |
| 79 | + public int[] circularGameLosers(int n, int k) { |
| 80 | + boolean[] vis = new boolean[n]; |
| 81 | + int cnt = 0; |
| 82 | + for (int i = 0, p = 1; !vis[i]; ++p) { |
| 83 | + vis[i] = true; |
| 84 | + ++cnt; |
| 85 | + i = (i + p * k) % n; |
| 86 | + } |
| 87 | + int[] ans = new int[n - cnt]; |
| 88 | + for (int i = 0, j = 0; i < n; ++i) { |
| 89 | + if (!vis[i]) { |
| 90 | + ans[j++] = i + 1; |
| 91 | + } |
| 92 | + } |
| 93 | + return ans; |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### **C++** |
| 99 | + |
| 100 | +```cpp |
| 101 | +class Solution { |
| 102 | +public: |
| 103 | + vector<int> circularGameLosers(int n, int k) { |
| 104 | + bool vis[n]; |
| 105 | + memset(vis, false, sizeof(vis)); |
| 106 | + for (int i = 0, p = 1; !vis[i]; ++p) { |
| 107 | + vis[i] = true; |
| 108 | + i = (i + p * k) % n; |
| 109 | + } |
| 110 | + vector<int> ans; |
| 111 | + for (int i = 0, j = 0; i < n; ++i) { |
| 112 | + if (!vis[i]) { |
| 113 | + ans.push_back(i + 1); |
| 114 | + } |
| 115 | + } |
| 116 | + return ans; |
| 117 | + } |
| 118 | +}; |
| 119 | +``` |
| 120 | +
|
| 121 | +### **Go** |
| 122 | +
|
| 123 | +```go |
| 124 | +func circularGameLosers(n int, k int) (ans []int) { |
| 125 | + vis := make([]bool, n) |
| 126 | + for i, p := 0, 1; !vis[i]; p++ { |
| 127 | + vis[i] = true |
| 128 | + i = (i + p*k) % n |
| 129 | + } |
| 130 | + for i, x := range vis { |
| 131 | + if !x { |
| 132 | + ans = append(ans, i+1) |
| 133 | + } |
| 134 | + } |
| 135 | + return |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### **...** |
| 140 | + |
| 141 | +``` |
| 142 | +
|
| 143 | +``` |
| 144 | + |
| 145 | +<!-- tabs:end --> |
0 commit comments