aboutsummaryrefslogtreecommitdiff
path: root/week05/ex4.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'week05/ex4.cpp')
-rw-r--r--week05/ex4.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/week05/ex4.cpp b/week05/ex4.cpp
new file mode 100644
index 0000000..2e77c9d
--- /dev/null
+++ b/week05/ex4.cpp
@@ -0,0 +1,33 @@
+#include <iostream>
+
+int main() {
+ size_t N, nums[30];
+ std::cin >> N;
+ for (size_t i = 0; i < N; i++) {
+ std::cin >> nums[i];
+ }
+
+ size_t maxJumpsIndex = 0, maxJumpsCount = 0;
+ bool visited[30] = { false };
+
+ for (size_t start = 0; start < N; start++) {
+ size_t current = start, jumpsCount = 0;
+
+ while (!visited[current]) {
+ visited[current] = true;
+ current = nums[current];
+ jumpsCount++;
+ }
+
+ if (jumpsCount > maxJumpsCount) {
+ maxJumpsCount = jumpsCount;
+ maxJumpsIndex = start;
+ }
+
+ for (size_t j = 0; j < N; j++) {
+ visited[j] = false;
+ }
+ }
+
+ std::cout << maxJumpsIndex << " " << maxJumpsCount << std::endl;
+}