diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 5e14827..d843b16 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -5,7 +5,7 @@ "id": "name", "type": "promptString", "description": "New problem name", - "default": "NEW_PROBLEM_X" + "default": "New-Prbolem-Name" } ], "tasks": [ diff --git a/Problems/One-Two-Skip-A-Few/One-Two-Skip-A-Few.test.ts b/Problems/One-Two-Skip-A-Few/One-Two-Skip-A-Few.test.ts new file mode 100644 index 0000000..917c5a1 --- /dev/null +++ b/Problems/One-Two-Skip-A-Few/One-Two-Skip-A-Few.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; +import howManyMissing from "./solver"; + +describe("Returns the correct number of missing numbers", () => { + it("should return 4 for [1, 2, 3, 8, 9]", () => { + expect(howManyMissing([1, 2, 3, 8, 9])).toEqual(4); + }); + + it("should return 1 for [1, 3]", () => { + expect(howManyMissing([1, 3])).toEqual(1); + }); + + it("should return 2 for [7, 10, 11, 12]", () => { + expect(howManyMissing([7, 10, 11, 12])).toEqual(2); + }); + + it("should return 5 for [1, 3, 5, 7, 9, 11]", () => { + expect(howManyMissing([1, 3, 5, 7, 9, 11])).toEqual(5); + }); + + it("should return 0 for [5, 6, 7, 8]", () => { + expect(howManyMissing([5, 6, 7, 8])).toEqual(0); + }); + + it("should return 0 for []", () => { + expect(howManyMissing([])).toEqual(0); + }); + + it("should return 100 for [5, 6, 86, 87, 90, 97, 111]", () => { + expect(howManyMissing([5, 6, 86, 87, 90, 97, 111])).toEqual(100); + }); +}); diff --git a/Problems/One-Two-Skip-A-Few/README.md b/Problems/One-Two-Skip-A-Few/README.md new file mode 100644 index 0000000..df409d3 --- /dev/null +++ b/Problems/One-Two-Skip-A-Few/README.md @@ -0,0 +1,43 @@ +# One-Two-Skip-A-Few + +You are given a sorted list `l` of length `n`, for example `[1, 3, 4, 5, 7]`. + +Your task is to determine how many numbers are missing between `l[0]` and `l[n - 1]`. +In other words, count how many numbers would appear if you counted from `l[0]` up to `l[n - 1]`, but are not present in the list. + +Example: + +Input: +l = [1, 3, 4, 5, 7] + +Numbers between 1 and 7 would be: +1, 2, 3, 4, 5, 6, 7 + +Missing numbers: +2, 6 + +Output: +2 + +## Documentation + +### Solution Idea + +This problem can be solved with a simple mathematical observation. + +If `x` is the starting number (`l[0]`) and `y` is the final number (`l[n - 1]`), then there are always `y - x + 1` total numbers when counting from `x` to `y`. + +Since the list already contains `n` of those numbers, the number of missing numbers is: + +`(y - x + 1) - n` + +### [Implementation](./solver.ts) + +(I don't need to explain that 🙃) + +--- + +### Information's + +This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com). +[Post](https://slothbytes.beehiiv.com/p/ai-agents) from November 18, 2025. diff --git a/Problems/One-Two-Skip-A-Few/solver.ts b/Problems/One-Two-Skip-A-Few/solver.ts new file mode 100644 index 0000000..fff0e45 --- /dev/null +++ b/Problems/One-Two-Skip-A-Few/solver.ts @@ -0,0 +1,4 @@ +export default function howManyMissing(list: number[]): number { + if (list.length <= 0) return 0; + return list.at(-1)! - list.at(0)! - list.length + 1; +} diff --git a/README.md b/README.md index b7bdc02..ab41507 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ This repository is about solving coding-problems. It contains famous (or not tha ## Problems -Solved: **20 problems** +Solved: **21 problems** -🟢 Easy: 14 +🟢 Easy: 15 🟠 Medium: 5 🔴 Hard: 1 @@ -36,6 +36,7 @@ Solved: **20 problems** | [Shortest Path Dijkstra](./Problems/Shortes-Path-Dijkstra/README.md) | 🟠 Medium | | [Sleep Time](./Problems/Sleep-Time/README.md) | 🟢 Easy | | [Reach Exit](./Problems/Reach-Exit/README.md) | 🟢 Easy | +| [One Two Skip a Few](./Problems/One-Two-Skip-A-Few/README.md) | 🟢 Easy | diff --git a/package.json b/package.json index 12c799d..b09141f 100644 --- a/package.json +++ b/package.json @@ -1,35 +1,35 @@ { - "name": "problem-solving", - "version": "CP-20", - "description": "This Package is all about solving coding problems", - "homepage": "https://github.com/Dennis-Bauer/Problem-Solving#readme", - "bugs": { - "url": "https://github.com/Dennis-Bauer/Problem-Solving/issues" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/Dennis-Bauer/Problem-Solving.git" - }, - "license": "ISC", - "author": "Dennis Bauer", - "type": "commonjs", - "scripts": { - "test": "vitest run ./Problems", - "test:dev": "vitest ./Problems --reporter=verbose", - "test:file": "vitest", - "test:ci": "vitest --run --passWithNoTests --reporter=json > vitest-report.json", - "build": "tsc", - "normalize": "biome check --write --unsafe", - "prepare": "husky install" - }, - "dependencies": { - "decimal.js": "^10.6.0", - "husky": "^9.1.7", - "typescript": "^5.9.2", - "vitest": "^3.2.4" - }, - "devDependencies": { - "@biomejs/biome": "2.3.11", - "@types/node": "^24.3.0" - } + "name": "problem-solving", + "version": "CP-20", + "description": "This Package is all about solving coding problems", + "homepage": "https://github.com/Dennis-Bauer/Problem-Solving#readme", + "bugs": { + "url": "https://github.com/Dennis-Bauer/Problem-Solving/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Dennis-Bauer/Problem-Solving.git" + }, + "license": "ISC", + "author": "Dennis Bauer", + "type": "commonjs", + "scripts": { + "test": "vitest run ./Problems", + "test:dev": "vitest ./Problems --reporter=verbose", + "test:file": "vitest", + "test:ci": "vitest --run --passWithNoTests --reporter=json > vitest-report.json", + "build": "tsc", + "normalize": "biome check --write --unsafe", + "prepare": "husky install" + }, + "dependencies": { + "decimal.js": "^10.6.0", + "husky": "^9.1.7", + "typescript": "^5.9.2", + "vitest": "^3.2.4" + }, + "devDependencies": { + "@biomejs/biome": "2.3.11", + "@types/node": "^24.3.0" + } }