Skip to content

Added code snippets of C++ on bit manipulation, math & number, searching, sorting #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions snippets/cpp/bit-manipulation/check-power-of-two.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Check Power of Two
description: Checks if a given number is a power of two using bitwise operations.
tags: bit-manipulation, power-of-two
author: ashukr07
---

```cpp
bool is_power_of_two(int n) {
return n > 0 && (n & (n - 1)) == 0;
}

// Usage:
is_power_of_two(16); // Returns: true
is_power_of_two(18); // Returns: false
```
20 changes: 20 additions & 0 deletions snippets/cpp/bit-manipulation/count-set-bits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Count Set Bits
description: Counts the number of 1 bits in the binary representation of a number.
tags: bit-manipulation, set-bits
author: ashukr07
---

```cpp
int count_set_bits(int n) {
int count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}

// Usage:
count_set_bits(13); // Returns: 3 (Binary: 1101)
```
20 changes: 20 additions & 0 deletions snippets/cpp/bit-manipulation/find-non-repeating-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Find Non-Repeating Number
description: Finds the number that appears only once in an array where every other number appears twice.
tags: bit-manipulation, xor
author: ashukr07
---

```cpp
int find_non_repeating(const std::vector<int>& nums) {
int result = 0;
for (int num : nums) {
result ^= num;
}
return result;
}

// Usage:
std::vector<int> nums = {4, 1, 2, 1, 2};
find_non_repeating(nums); // Returns: 4
```
21 changes: 21 additions & 0 deletions snippets/cpp/bit-manipulation/reverse-bits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Reverse Bits
description: Reverses the bits of a given integer.
tags: bit-manipulation, reverse-bits
author: ashukr07
---

```cpp
unsigned int reverse_bits(unsigned int n) {
unsigned int result = 0;
for (int i = 0; i < 32; ++i) {
result <<= 1;
result |= n & 1;
n >>= 1;
}
return result;
}

// Usage:
reverse_bits(43261596); // Returns: 964176192 (Binary: 00000010100101000001111010011100 -> 00111001011110000010100101000000)
```
18 changes: 18 additions & 0 deletions snippets/cpp/bit-manipulation/xor-of-range.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: XOR of Range
description: Finds XOR of all numbers from 1 to n using properties of XOR.
tags: bit-manipulation, xor
author: ashukr07
---

```cpp
int xor_upto_n(int n) {
if (n % 4 == 0) return n;
if (n % 4 == 1) return 1;
if (n % 4 == 2) return n + 1;
return 0;
}

// Usage:
xor_upto_n(5); // Returns: 1 (1 ^ 2 ^ 3 ^ 4 ^ 5 = 1)
```
29 changes: 0 additions & 29 deletions snippets/cpp/debuging/vector-print.md

This file was deleted.

25 changes: 25 additions & 0 deletions snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Binary to Decimal Conversion
description: Converts a binary number represented as a string to its decimal equivalent.
tags: binary, conversion
author: ashukr07
---

```cpp
int binary_to_decimal(const std::string& binary) {
int decimal = 0;
int base = 1; // Base value for the least significant bit

for (int i = binary.length() - 1; i >= 0; --i) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2; // Move to the next power of 2
}
return decimal;
}

// Usage:
std::string binary = "1011"; // Binary representation of 11
binary_to_decimal(binary); // Returns: 11
```
22 changes: 22 additions & 0 deletions snippets/cpp/math-and-numbers/check-perfect-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: Check Perfect Number
description: Checks if a number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).
tags: math, perfect-number
author: ashukr07
---

```cpp
bool is_perfect(int n) {
int sum = 1; // 1 is a divisor for all n > 1
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
sum += i;
if (i != n / i) sum += n / i;
}
}
return sum == n && n != 1;
}

// Usage:
is_perfect(28); // Returns: true
```
21 changes: 21 additions & 0 deletions snippets/cpp/math-and-numbers/compound-interest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Compound Interest
description: Calculates the compound interest for a given principal, rate, time, and number of times interest applied per time period.
tags: math, finance
author: ashukr07
---

```cpp
#include <cmath>

double compound_interest(double principal, double rate, double time, int n) {
return principal * std::pow(1 + rate / n, n * time);
}

// Usage:
double principal = 1000.0; // Initial amount
double rate = 0.05; // Annual interest rate (5%)
double time = 2; // Time in years
int n = 4; // Compounded quarterly
compound_interest(principal, rate, time, n); // Returns: 1104.081632653061
```
16 changes: 16 additions & 0 deletions snippets/cpp/math-and-numbers/factorial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Factorial
description: Calculates the factorial of a given non-negative integer.
tags: math, factorial
author: ashukr07
---

```cpp
int factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive step
}

// Usage:
factorial(5); // Returns: 120
```
16 changes: 16 additions & 0 deletions snippets/cpp/math-and-numbers/fibonacci-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Fibonacci Number
description: Calculates the nth Fibonacci number using recursion.
tags: math, fibonacci, recursion
author: ashukr07
---

```cpp
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

// Usage:
fibonacci(6); // Returns: 8
```
15 changes: 15 additions & 0 deletions snippets/cpp/math-and-numbers/gcd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: GCD
description: Computes the greatest common divisor (GCD) of two integers.
tags: math, gcd
author: ashukr07
---

```cpp
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}

// Usage:
gcd(48, 18); // Returns: 6
```
15 changes: 15 additions & 0 deletions snippets/cpp/math-and-numbers/lcm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: LCM
description: Computes the least common multiple (LCM) of two integers.
tags: math, lcm
author: ashukr07
---

```cpp
int lcm(int a, int b) {
return (a / gcd(a, b)) * b; // Using GCD to calculate LCM
}

// Usage:
lcm(12, 18); // Returns: 36
```
20 changes: 20 additions & 0 deletions snippets/cpp/math-and-numbers/sum-of-digits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Sum of Digits
description: Calculates the sum of the digits of an integer.
tags: math, digits
author: ashukr07
---

```cpp
int sum_of_digits(int n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}

// Usage:
sum_of_digits(123); // Returns: 6
```
26 changes: 26 additions & 0 deletions snippets/cpp/searching/binary-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Binary Search
description: Searches for a target value in a sorted array using binary search.
tags: searching, binary-search
author: ashukr07
---

```cpp
int binary_search(const std::vector<int>& arr, int target) {
int left = 0, right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2; // Avoids overflow
if (arr[mid] == target) return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1; // Element not found
}

// Usage:
std::vector<int> nums = {1, 3, 5, 7, 9};
binary_search(nums, 7); // Returns: 3 (index of element)
binary_search(nums, 2); // Returns: -1 (not found)
```
20 changes: 20 additions & 0 deletions snippets/cpp/searching/linear-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Linear Search
description: Searches for a target value in an array using linear search.
tags: searching, linear-search
author: ashukr07
---

```cpp
int linear_search(const std::vector<int>& arr, int target) {
for (int i = 0; i < arr.size(); ++i) {
if (arr[i] == target) return i; // Return index if found
}
return -1; // Element not found
}

// Usage:
std::vector<int> nums = {4, 2, 8, 5, 1};
linear_search(nums, 5); // Returns: 3 (index of element)
linear_search(nums, 10); // Returns: -1 (not found)
```
22 changes: 22 additions & 0 deletions snippets/cpp/sorting/bubble-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: Bubble Sort
description: Sorts an array using the bubble sort algorithm.
tags: sorting, bubble-sort
author: ashukr07
---

```cpp
void bubble_sort(std::vector<int>& arr) {
for (int i = 0; i < arr.size() - 1; ++i) {
for (int j = 0; j < arr.size() - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
}
}
}
}

// Usage:
std::vector<int> nums = {5, 3, 8, 6, 2};
bubble_sort(nums); // nums becomes: {2, 3, 5, 6, 8}
```
24 changes: 24 additions & 0 deletions snippets/cpp/sorting/insertion-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Insertion Sort
description: Sorts an array using the insertion sort algorithm.
tags: sorting, insertion-sort
author: ashukr07
---

```cpp
void insertion_sort(std::vector<int>& arr) {
for (int i = 1; i < arr.size(); ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}

// Usage:
std::vector<int> nums = {12, 11, 13, 5, 6};
insertion_sort(nums); // nums becomes: {5, 6, 11, 12, 13}
```
Loading