You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/1800-1899/1823-find-the-winner-of-the-circular-game.md
+13-97Lines changed: 13 additions & 97 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,23 @@
1
1
---
2
2
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
5
5
tags:
6
-
- Array
6
+
- Array
7
+
- Math
8
+
- Recursion
9
+
- Queue
10
+
- Simulation
7
11
8
12
description: "This is a solution to the Find the Winner of the Circular Game problem on LeetCode."
13
+
14
+
sidebar_position: 1823
9
15
---
10
16
11
17
## Problem Description
12
18
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.
13
19
14
-
## Examples
20
+
###Examples
15
21
16
22
**Example 1:**
17
23
@@ -54,102 +60,15 @@ Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is frien
54
60
3.**Return the Result**:
55
61
- After iterating through all elements, return the value of `winner` as the result.
56
62
57
-
<Tabs>
58
-
<TabItemvalue="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
-
<inputtype="number"id="n" />
77
-
<p>Enter the number of steps (k):</p>
78
-
<inputtype="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
-
```
89
63
90
64
#### Complexity Analysis:
91
65
92
66
-**Time Complexity:** $O(n)$ because of iterating through the elements,
0 commit comments