-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cses: 1068. Weird Algorithm + 1083. Missing Number
- Loading branch information
Showing
15 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Weird Algorithm | ||
|
||
Consider an algorithm that takes as input a positive integer `n`. If `n` is `even`, the algorithm **divides** it by `two`, and if `n` is `odd`, the algorithm **multiplies** it by `three` and **adds** `one`. The algorithm repeats this, until `n` is `one`. For example, the sequence for `n=3` is as follows: | ||
|
||
- `3 → 10 → 5 → 16 → 8 → 4 → 2 → 1` | ||
|
||
Your task is to simulate the execution of the algorithm for a given value of `n`. | ||
|
||
**Input** | ||
- The only input line contains an integer n. | ||
|
||
**Output** | ||
- Print a line that contains all values of n during the algorithm. | ||
|
||
**Constraints** | ||
- `1 ≤ n ≤ 106` | ||
|
||
**Example** | ||
``` | ||
Input: | ||
3 | ||
Output: | ||
3 10 5 16 8 4 2 1 | ||
``` |
17 changes: 17 additions & 0 deletions
17
cses/01-introductory/1068-weird-algorithm/weird_algorithm.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <stdio.h> | ||
#define ll long long | ||
|
||
void printNumbers(ll n) { | ||
printf("%lld", n); | ||
while (n > 1) { | ||
n = (n & 1) ? n*3+1 : n/2; | ||
printf(" %lld", n); | ||
} | ||
} | ||
|
||
int main() { | ||
ll n; | ||
scanf("%lld", &n); | ||
printNumbers(n); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Missing Number | ||
|
||
You are given all numbers between `1,2,…,n` except one. Your task is to find the missing number. | ||
|
||
**Input** | ||
- The first input line contains an integer `n`. | ||
- The second line contains `n−1` numbers. Each number is distinct and between `1` and `n` (inclusive). | ||
|
||
**Output** | ||
- Print the missing number. | ||
|
||
**Constraints** | ||
- `2 ≤ n ≤ 2*10^5` | ||
|
||
**Example** | ||
``` | ||
Input: | ||
5 | ||
2 3 1 5 | ||
Output: | ||
4 | ||
``` |
17 changes: 17 additions & 0 deletions
17
cses/01-introductory/1083-missing-number/missing_number.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <stdio.h> | ||
#define ll long long | ||
|
||
int main() { | ||
ll n; | ||
scanf("%lld", &n); | ||
|
||
ll curr = 0, sum = n*(n+1)/2; | ||
int a; n--; | ||
while (n--) { | ||
scanf("%d", &a); | ||
curr += a; | ||
} | ||
|
||
printf("%lld", sum - curr); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Introductory Problems | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| | ||
| 1068 | [Weird Algorithm](https://github.com/ympons/katas/tree/master/cses/01-introductory/1068-weird-algorithm) | C++ | | ||
| 1083 | [Missing Number](https://github.com/ympons/katas/tree/master/cses/01-introductory/1083-missing-number) | C++ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Sorting and Searching | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Dynamic Programming | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Graph Algorithms | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Range Queries | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Tree Algorithms | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Mathematics | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# String Algorithms | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Geometry | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Advanced Techniques | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Additional Problems | ||
|
||
| # | Title | Language | | ||
|-------:|:------|:------| |