Skip to content

Commit

Permalink
Find the Losers of the Circular Game
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 18, 2023
1 parent cd090e9 commit 17e9598
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ add_task(find-the-index-of-the-first-occurrence-in-a-string)
add_task(find-the-k-beauty-of-a-number)
add_task(find-the-longest-balanced-substring-of-a-binary-string)
add_task(find-the-longest-valid-obstacle-course-at-each-position)
add_task(find-the-losers-of-the-circular-game)
add_task(find-the-maximum-achievable-number)
add_task(find-the-maximum-divisibility-score)
add_task(find-the-maximum-number-of-marked-indices)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_find_the_losers_of_the_circular_game test.cpp)
21 changes: 21 additions & 0 deletions solutions/find-the-losers-of-the-circular-game/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <vector>

class Solution {
public:
static std::vector<int> circularGameLosers(int n, int k) {
std::vector<bool> seen(n);
for (int i = 0, steps = k; !seen[i]; i = (i + steps) % n, steps += k) {
seen[i] = true;
}

std::vector<int> ans;
for (int i = 0; i < n; ++i) {
if (!seen[i]) {
ans.push_back(i + 1);
}
}
return ans;
}
};
29 changes: 29 additions & 0 deletions solutions/find-the-losers-of-the-circular-game/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
int n;
int k;
std::vector<int> expected;
};

std::vector<TestCase> test_cases{
{
.n = 5,
.k = 2,
.expected{4, 5},
},
{
.n = 4,
.k = 4,
.expected{2, 3, 4},
},
};

for (const auto &[n, k, expected] : test_cases) {
const auto actual = Solution::circularGameLosers(n, k);
REQUIRE(expected == actual);
}
}

0 comments on commit 17e9598

Please sign in to comment.