Skip to content

Commit 3cfac9e

Browse files
updated
1 parent e38f4f1 commit 3cfac9e

File tree

1 file changed

+13
-97
lines changed

1 file changed

+13
-97
lines changed

dsa-solutions/lc-solutions/1800-1899/1823-find-the-winner-of-the-circular-game.md

Lines changed: 13 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
---
22
id: find-the-winner-of-the-circular-game
3-
title: Find the Winner of the Circular Game
4-
sidebar_label: 1823 - Find the Winner of the Circular Game
3+
title: Find The Winner Of The Circular Game
4+
sidebar_label: 1823 Find The Winner Of The Circular Game
55
tags:
6-
- Array
6+
- Array
7+
- Math
8+
- Recursion
9+
- Queue
10+
- Simulation
711

812
description: "This is a solution to the Find the Winner of the Circular Game problem on LeetCode."
13+
14+
sidebar_position: 1823
915
---
1016

1117
## Problem Description
1218
There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the $(i+1)^{th}$ friend for `1 <= i < n`, and moving clockwise from the nth friend brings you to the 1st friend.
1319

14-
## Examples
20+
### Examples
1521

1622
**Example 1:**
1723

@@ -54,102 +60,15 @@ Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is frien
5460
3. **Return the Result**:
5561
- After iterating through all elements, return the value of `winner` as the result.
5662

57-
<Tabs>
58-
<TabItem value="Solution" label="Solution">
59-
60-
#### Implementation
61-
```jsx live
62-
63-
function Solution() {
64-
const findTheWinner = (n, k) => {
65-
let winner = 1;
66-
for (let i = 1; i < n; i++) {
67-
winner = (winner + k - 1) % (i + 1) + 1;
68-
}
69-
return winner;
70-
};
71-
72-
return (
73-
<div>
74-
<h1>Find the Winner</h1>
75-
<p>Enter the number of friends (n):</p>
76-
<input type="number" id="n" />
77-
<p>Enter the number of steps (k):</p>
78-
<input type="number" id="k" />
79-
<button onClick={() => {
80-
const n = parseInt(document.getElementById("n").value);
81-
const k = parseInt(document.getElementById("k").value);
82-
const winner = findTheWinner(n, k);
83-
alert(`The winner is: ${winner}`);
84-
}}>Find Winner</button>
85-
</div>
86-
);
87-
}
88-
```
8963

9064
#### Complexity Analysis:
9165

9266
- **Time Complexity:** $O(n)$ because of iterating through the elements,
9367
- **Space Complexity:** $O(1)$
9468

95-
## Code in Different Languages
96-
<Tabs>
97-
<TabItem value="JavaScript" label="JavaScript">
98-
<SolutionAuthor name="@Ishitamukherjee2004"/>
99-
```javascript
100-
class Solution {
101-
findTheWinner(n, k) {
102-
let winner = 1;
103-
for (let i = 1; i < n; i++) {
104-
winner = (winner + k - 1) % (i + 1) + 1;
105-
}
106-
return winner;
107-
}
108-
}
109-
```
110-
</TabItem>
11169

112-
<TabItem value="TypeScript" label="TypeScript">
113-
<SolutionAuthor name="@Ishitamukherjee2004"/>
114-
```typescript
115-
class Solution {
116-
findTheWinner(n: number, k: number): number {
117-
let winner = 1;
118-
for (let i = 1; i < n; i++) {
119-
winner = (winner + k - 1) % (i + 1) + 1;
120-
}
121-
return winner;
122-
}
123-
}
124-
```
125-
</TabItem>
126-
<TabItem value="Python" label="Python">
127-
<SolutionAuthor name="@Ishitamukherjee2004"/>
128-
```python
129-
class Solution:
130-
def findTheWinner(self, n: int, k: int) -> int:
131-
winner = 1
132-
for i in range(1, n):
133-
winner = (winner + k - 1) % (i + 1) + 1
134-
return winner
135-
```
136-
</TabItem>
137-
<TabItem value="Java" label="Java">
138-
<SolutionAuthor name="@Ishitamukherjee2004"/>
139-
```java
140-
public class Solution {
141-
public int findTheWinner(int n, int k) {
142-
int winner = 1;
143-
for (int i = 1; i < n; i++) {
144-
winner = (winner + k - 1) % (i + 1) + 1;
145-
}
146-
return winner;
147-
}
148-
}
149-
```
150-
</TabItem>
151-
<TabItem value="C++" label="C++">
152-
<SolutionAuthor name="@Ishitamukherjee2004"/>
70+
#### C++ Solution
71+
15372
```cpp
15473
class Solution {
15574
public:
@@ -161,12 +80,9 @@ public:
16180
return winner;
16281
}
16382
};
83+
16484
```
165-
</TabItem>
166-
</Tabs>
16785
168-
</TabItem>
169-
</Tabs>
17086
17187
17288

0 commit comments

Comments
 (0)