Skip to content

Commit

Permalink
cses: 1068. Weird Algorithm + 1083. Missing Number
Browse files Browse the repository at this point in the history
  • Loading branch information
ympons committed Jan 17, 2021
1 parent ed270ed commit 33ce7f7
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cses/01-introductory/1068-weird-algorithm/README.md
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 cses/01-introductory/1068-weird-algorithm/weird_algorithm.cpp
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;
}
23 changes: 23 additions & 0 deletions cses/01-introductory/1083-missing-number/README.md
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 cses/01-introductory/1083-missing-number/missing_number.cpp
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;
}
6 changes: 6 additions & 0 deletions cses/01-introductory/README.md
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++ |
4 changes: 4 additions & 0 deletions cses/02-sorting-and-searching/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sorting and Searching

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/03-dynamic-programming/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Dynamic Programming

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/04-graph-algorithms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Graph Algorithms

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/05-range-queries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Range Queries

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/06-tree-algorithms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Tree Algorithms

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/07-mathematics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Mathematics

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/08-string-algorithms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# String Algorithms

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/09-geometry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Geometry

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/10-advance-techniques/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Advanced Techniques

| # | Title | Language |
|-------:|:------|:------|
4 changes: 4 additions & 0 deletions cses/11-additional-problems/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Additional Problems

| # | Title | Language |
|-------:|:------|:------|

0 comments on commit 33ce7f7

Please sign in to comment.