From 9027c94862e2b84bd72ebf9b70ebe7caa79dafdf Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Mon, 30 Jun 2025 18:18:05 +0530 Subject: [PATCH 01/12] Create check_even_odd.cpp Implementation to Check if a number is Even or Odd using Bitwise Operator --- bit_manipulation/check_even_odd.cpp | 88 +++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 bit_manipulation/check_even_odd.cpp diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp new file mode 100644 index 00000000000..78031cf2da9 --- /dev/null +++ b/bit_manipulation/check_even_odd.cpp @@ -0,0 +1,88 @@ +/** + * @file + * @brief Implementation to [Check if a number is Even or Odd using Bitwise Operator] + * (https://www.log2base2.com/c-examples/bitwise/odd-or-even-program-in-c-using-bitwise-operator.html) + * + * @details + * Given an integer N, determine whether it is even or odd using bitwise manipulation. + * The least significant bit (LSB) of a binary number determines its parity: + * - If the LSB is 0, the number is even. + * - If the LSB is 1, the number is odd. + * + * This can be checked efficiently using the bitwise AND operator (&) with 1. + * - If (N & 1) == 0, N is even. + * - If (N & 1) == 1, N is odd. + * + * Worst Case Time Complexity: O(1) + * Space Complexity: O(1) + * + * @author [Vedant Mukhedkar](https://github.com/git5v) + */ + +#include /// for assert +#include +#include /// for IO operations +#include + +/** + * @namespace bit_manipulation + * @brief Bit manipulation algorithms + */ +namespace bit_manipulation { +/** + * @namespace even_odd + * @brief Functions for checking if a number is even or odd using bitwise operations + */ +namespace even_odd { + +/** + * @brief Checks if a number is even or odd using bitwise AND. + * @param N The number to check. + * @returns "Even" if N is even, "Odd" if N is odd. + */ +std::string checkEvenOdd(std::int64_t N) { + return (N & 1) == 0 ? "Even" : "Odd"; +} + +} // namespace even_odd +} // namespace bit_manipulation + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + using bit_manipulation::even_odd::checkEvenOdd; + + // Test Even numbers + assert(checkEvenOdd(0) == "Even"); + assert(checkEvenOdd(2) == "Even"); + assert(checkEvenOdd(100) == "Even"); + assert(checkEvenOdd(-4) == "Even"); + assert(checkEvenOdd(-1000) == "Even"); + + // Test Odd numbers + assert(checkEvenOdd(1) == "Odd"); + assert(checkEvenOdd(3) == "Odd"); + assert(checkEvenOdd(101) == "Odd"); + assert(checkEvenOdd(-5) == "Odd"); + assert(checkEvenOdd(-999) == "Odd"); + + std::cout << "All test cases successfully passed!" << std::endl; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + + // Example usage: + // std::int64_t num; + // std::cout << "Enter a number: "; + // std::cin >> num; + // std::cout << num << " is " << bit_manipulation::even_odd::checkEvenOdd(num) << std::endl; + + return 0; +} From 13d062dedc48e50c995d46de82c72197b91598dd Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Mon, 30 Jun 2025 18:19:07 +0530 Subject: [PATCH 02/12] Update check_even_odd.cpp --- bit_manipulation/check_even_odd.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp index 78031cf2da9..0ff874b90e6 100644 --- a/bit_manipulation/check_even_odd.cpp +++ b/bit_manipulation/check_even_odd.cpp @@ -39,12 +39,12 @@ namespace even_odd { * @brief Checks if a number is even or odd using bitwise AND. * @param N The number to check. * @returns "Even" if N is even, "Odd" if N is odd. - */ -std::string checkEvenOdd(std::int64_t N) { - return (N & 1) == 0 ? "Even" : "Odd"; -} + */ + std::string checkEvenOdd(std::int64_t N) { + return (N & 1) == 0 ? "Even" : "Odd"; + } -} // namespace even_odd + } // namespace even_odd } // namespace bit_manipulation /** From 030c69c7332fcb8a645a27b64215a3d42500c1ab Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Mon, 30 Jun 2025 18:57:27 +0530 Subject: [PATCH 03/12] Create factorial_top_down_dp.cpp --- dynamic_programming/factorial_top_down_dp.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 dynamic_programming/factorial_top_down_dp.cpp diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp new file mode 100644 index 00000000000..a89e7c392fc --- /dev/null +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -0,0 +1,64 @@ +/** + * @file + * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) + * @details + * This program computes the factorial of a non-negative integer using recursion + * with memoization (top-down dynamic programming). It stores intermediate results + * to avoid redundant calculations for improved efficiency. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + @author [Vedant Mukhedkar](https://github.com/git5v) + */ + +#include +#include // For test cases + +/// Array to store computed factorials for memoization +long long memo[1000] = {0}; + +/** + * @brief Computes the factorial of a non-negative integer using recursion and memoization. + * @param n The integer whose factorial is to be computed + * @returns The factorial of n + */ +long long fact_rec(int n) { + if (n == 0) return 1; // Base case: 0! = 1 + if (memo[n] != 0) return memo[n]; // Return already computed value + memo[n] = n * fact_rec(n - 1); // Store and return the computed value + return memo[n]; +} + +/** + * @brief Self-test implementations for the fact_rec function. + * @returns void + */ +void test_fact_rec() { + // Test cases for factorial computation + assert(fact_rec(0) == 1); + assert(fact_rec(1) == 1); + assert(fact_rec(5) == 120); + assert(fact_rec(10) == 3628800); + std::cout << "All test cases passed!\n"; +} + +/** + * @brief Main function to run test cases and interact with the user. + * @returns 0 on program success + */ +int main() { + // Run test cases + test_fact_rec(); + + // User interaction loop + // int n; + // std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; + // std::cin >> n; + // if (n < 0) { + // std::cout << "Please enter a non-negative integer only.\n"; + // return 0; + // } + // std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; + + return 0; +} From 5f213b98e17c96a3295e6576b18779e0818fe6fe Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Fri, 11 Jul 2025 16:23:33 +0530 Subject: [PATCH 04/12] Delete dynamic_programming/factorial_top_down_dp.cpp Deleted the one file as there was 2 files in the commit --- dynamic_programming/factorial_top_down_dp.cpp | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 dynamic_programming/factorial_top_down_dp.cpp diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp deleted file mode 100644 index a89e7c392fc..00000000000 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/** - * @file - * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) - * @details - * This program computes the factorial of a non-negative integer using recursion - * with memoization (top-down dynamic programming). It stores intermediate results - * to avoid redundant calculations for improved efficiency. - * - * Time Complexity: O(n) - * Space Complexity: O(n) - @author [Vedant Mukhedkar](https://github.com/git5v) - */ - -#include -#include // For test cases - -/// Array to store computed factorials for memoization -long long memo[1000] = {0}; - -/** - * @brief Computes the factorial of a non-negative integer using recursion and memoization. - * @param n The integer whose factorial is to be computed - * @returns The factorial of n - */ -long long fact_rec(int n) { - if (n == 0) return 1; // Base case: 0! = 1 - if (memo[n] != 0) return memo[n]; // Return already computed value - memo[n] = n * fact_rec(n - 1); // Store and return the computed value - return memo[n]; -} - -/** - * @brief Self-test implementations for the fact_rec function. - * @returns void - */ -void test_fact_rec() { - // Test cases for factorial computation - assert(fact_rec(0) == 1); - assert(fact_rec(1) == 1); - assert(fact_rec(5) == 120); - assert(fact_rec(10) == 3628800); - std::cout << "All test cases passed!\n"; -} - -/** - * @brief Main function to run test cases and interact with the user. - * @returns 0 on program success - */ -int main() { - // Run test cases - test_fact_rec(); - - // User interaction loop - // int n; - // std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; - // std::cin >> n; - // if (n < 0) { - // std::cout << "Please enter a non-negative integer only.\n"; - // return 0; - // } - // std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; - - return 0; -} From fb5a1d426e910feb6b5b1fa9d6444ed931198341 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Fri, 11 Jul 2025 16:48:57 +0530 Subject: [PATCH 05/12] Create factorial_top_down_dp.cpp --- dynamic_programming/factorial_top_down_dp.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 dynamic_programming/factorial_top_down_dp.cpp diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp new file mode 100644 index 00000000000..79bfbbc669b --- /dev/null +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -0,0 +1,64 @@ +/** + * @file + * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) + * @details + * This program computes the factorial of a non-negative integer using recursion + * with memoization (top-down dynamic programming). It stores intermediate results + * to avoid redundant calculations for improved efficiency. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + @author [Vedant Mukhedkar](https://github.com/git5v) + */ + +#include +#include // For test cases + +/// Array to store computed factorials for memoization +long long memo[1000] = {0}; + +/** + * @brief Computes the factorial of a non-negative integer using recursion and memoization. + * @param n The integer whose factorial is to be computed + * @returns The factorial of n + */ +long long fact_rec(int n) { + if (n == 0) return 1; // Base case: 0! = 1 + if (memo[n] != 0) return memo[n]; // Return already computed value + memo[n] = n * fact_rec(n - 1); // Store and return the computed value + return memo[n]; +} + +/** + * @brief Self-test implementations for the fact_rec function. + * @returns void + */ +void test_fact_rec() { + // Test cases for factorial computation + assert(fact_rec(0) == 1); + assert(fact_rec(1) == 1); + assert(fact_rec(5) == 120); + assert(fact_rec(10) == 3628800); + std::cout << "All test cases passed!\n"; +} + +/** + * @brief Main function to run test cases and interact with the user. + * @returns 0 on program success + */ +int main() { + // Run test cases + test_fact_rec(); + + // User interaction loop + // int n; + // std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; + // std::cin >> n; + // if (n < 0) { + // std::cout << "Please enter a non-negative integer only.\n"; + // return 0; + // } + // std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; + + return 0; +} From f0b4d76f710d04f8527882a60040a91c39e02759 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Fri, 11 Jul 2025 16:50:31 +0530 Subject: [PATCH 06/12] Delete dynamic_programming/factorial_top_down_dp.cpp --- dynamic_programming/factorial_top_down_dp.cpp | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 dynamic_programming/factorial_top_down_dp.cpp diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp deleted file mode 100644 index 79bfbbc669b..00000000000 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/** - * @file - * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) - * @details - * This program computes the factorial of a non-negative integer using recursion - * with memoization (top-down dynamic programming). It stores intermediate results - * to avoid redundant calculations for improved efficiency. - * - * Time Complexity: O(n) - * Space Complexity: O(n) - @author [Vedant Mukhedkar](https://github.com/git5v) - */ - -#include -#include // For test cases - -/// Array to store computed factorials for memoization -long long memo[1000] = {0}; - -/** - * @brief Computes the factorial of a non-negative integer using recursion and memoization. - * @param n The integer whose factorial is to be computed - * @returns The factorial of n - */ -long long fact_rec(int n) { - if (n == 0) return 1; // Base case: 0! = 1 - if (memo[n] != 0) return memo[n]; // Return already computed value - memo[n] = n * fact_rec(n - 1); // Store and return the computed value - return memo[n]; -} - -/** - * @brief Self-test implementations for the fact_rec function. - * @returns void - */ -void test_fact_rec() { - // Test cases for factorial computation - assert(fact_rec(0) == 1); - assert(fact_rec(1) == 1); - assert(fact_rec(5) == 120); - assert(fact_rec(10) == 3628800); - std::cout << "All test cases passed!\n"; -} - -/** - * @brief Main function to run test cases and interact with the user. - * @returns 0 on program success - */ -int main() { - // Run test cases - test_fact_rec(); - - // User interaction loop - // int n; - // std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; - // std::cin >> n; - // if (n < 0) { - // std::cout << "Please enter a non-negative integer only.\n"; - // return 0; - // } - // std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; - - return 0; -} From 14cd706069158116d0a83204e590c3b8ee1c1a86 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:30:47 +0530 Subject: [PATCH 07/12] Update bit_manipulation/check_even_odd.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- bit_manipulation/check_even_odd.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp index 0ff874b90e6..dd9738f9a4d 100644 --- a/bit_manipulation/check_even_odd.cpp +++ b/bit_manipulation/check_even_odd.cpp @@ -20,9 +20,9 @@ */ #include /// for assert -#include +#include /// for uint32_t #include /// for IO operations -#include +#include /// for std::string /** * @namespace bit_manipulation From d1c9bac677f02fe5c14a4a9b0f3503c9c77bdb96 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:30:57 +0530 Subject: [PATCH 08/12] Update bit_manipulation/check_even_odd.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- bit_manipulation/check_even_odd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp index dd9738f9a4d..240c0fd5cfb 100644 --- a/bit_manipulation/check_even_odd.cpp +++ b/bit_manipulation/check_even_odd.cpp @@ -40,7 +40,7 @@ namespace even_odd { * @param N The number to check. * @returns "Even" if N is even, "Odd" if N is odd. */ - std::string checkEvenOdd(std::int64_t N) { + std::string is_even(std::int64_t N) { return (N & 1) == 0 ? "Even" : "Odd"; } From 623867ee67636bf3522f086871e7bc242562b390 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:31:05 +0530 Subject: [PATCH 09/12] Update bit_manipulation/check_even_odd.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- bit_manipulation/check_even_odd.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp index 240c0fd5cfb..0d3ea655fde 100644 --- a/bit_manipulation/check_even_odd.cpp +++ b/bit_manipulation/check_even_odd.cpp @@ -77,12 +77,5 @@ static void test() { */ int main() { test(); // run self-test implementations - - // Example usage: - // std::int64_t num; - // std::cout << "Enter a number: "; - // std::cin >> num; - // std::cout << num << " is " << bit_manipulation::even_odd::checkEvenOdd(num) << std::endl; - return 0; } From 66f3e41d6a43094c16f14eab7479d9c910292c09 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:31:12 +0530 Subject: [PATCH 10/12] Update bit_manipulation/check_even_odd.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- bit_manipulation/check_even_odd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp index 0d3ea655fde..27f89fe5463 100644 --- a/bit_manipulation/check_even_odd.cpp +++ b/bit_manipulation/check_even_odd.cpp @@ -41,7 +41,7 @@ namespace even_odd { * @returns "Even" if N is even, "Odd" if N is odd. */ std::string is_even(std::int64_t N) { - return (N & 1) == 0 ? "Even" : "Odd"; + return (N & 1) == 0; } } // namespace even_odd From a3f753d63386db6fb92bf580fd6a8b15df81ac30 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:40:10 +0530 Subject: [PATCH 11/12] Update check_even_odd.cpp modified --- bit_manipulation/check_even_odd.cpp | 36 +++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp index 27f89fe5463..9450485d9cf 100644 --- a/bit_manipulation/check_even_odd.cpp +++ b/bit_manipulation/check_even_odd.cpp @@ -13,6 +13,18 @@ * - If (N & 1) == 0, N is even. * - If (N & 1) == 1, N is odd. * + * Example: + * Consider 8-bit binary representations of two numbers: + * Number: 10 (decimal) -> 00001010 (binary) + * LSB = 0 -> Even number + * + * Number: 13 (decimal) -> 00001101 (binary) + * LSB = 1 -> Odd number + * + * In both cases, evaluating (N & 1) isolates the LSB: + * - For 10: 00001010 & 00000001 = 0 (Even) + * - For 13: 00001101 & 00000001 = 1 (Odd) + * * Worst Case Time Complexity: O(1) * Space Complexity: O(1) * @@ -41,7 +53,7 @@ namespace even_odd { * @returns "Even" if N is even, "Odd" if N is odd. */ std::string is_even(std::int64_t N) { - return (N & 1) == 0; + return (N & 1) == 0 ? "Even" : "Odd"; } } // namespace even_odd @@ -52,21 +64,21 @@ namespace even_odd { * @returns void */ static void test() { - using bit_manipulation::even_odd::checkEvenOdd; + using bit_manipulation::even_odd::is_even; // Test Even numbers - assert(checkEvenOdd(0) == "Even"); - assert(checkEvenOdd(2) == "Even"); - assert(checkEvenOdd(100) == "Even"); - assert(checkEvenOdd(-4) == "Even"); - assert(checkEvenOdd(-1000) == "Even"); + assert(is_even(0) == "Even"); + assert(is_even(2) == "Even"); + assert(is_even(100) == "Even"); + assert(is_even(-4) == "Even"); + assert(is_even(-1000) == "Even"); // Test Odd numbers - assert(checkEvenOdd(1) == "Odd"); - assert(checkEvenOdd(3) == "Odd"); - assert(checkEvenOdd(101) == "Odd"); - assert(checkEvenOdd(-5) == "Odd"); - assert(checkEvenOdd(-999) == "Odd"); + assert(is_even(1) == "Odd"); + assert(is_even(3) == "Odd"); + assert(is_even(101) == "Odd"); + assert(is_even(-5) == "Odd"); + assert(is_even(-999) == "Odd"); std::cout << "All test cases successfully passed!" << std::endl; } From d0151794b49109ce9fa72c2f174c786adbd7c344 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Mon, 1 Sep 2025 19:15:47 +0530 Subject: [PATCH 12/12] Update check_even_odd.cpp reverted to boolean to check if number is even or not --- bit_manipulation/check_even_odd.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp index 9450485d9cf..17ad0707cac 100644 --- a/bit_manipulation/check_even_odd.cpp +++ b/bit_manipulation/check_even_odd.cpp @@ -52,8 +52,8 @@ namespace even_odd { * @param N The number to check. * @returns "Even" if N is even, "Odd" if N is odd. */ - std::string is_even(std::int64_t N) { - return (N & 1) == 0 ? "Even" : "Odd"; + bool is_even(std::int64_t N) { + return (N & 1) == 0 ? true : false; } } // namespace even_odd @@ -67,18 +67,18 @@ static void test() { using bit_manipulation::even_odd::is_even; // Test Even numbers - assert(is_even(0) == "Even"); - assert(is_even(2) == "Even"); - assert(is_even(100) == "Even"); - assert(is_even(-4) == "Even"); - assert(is_even(-1000) == "Even"); + assert(is_even(0) == true); + assert(is_even(2) == true); + assert(is_even(100) == true); + assert(is_even(-4) == true); + assert(is_even(-1000) == true); // Test Odd numbers - assert(is_even(1) == "Odd"); - assert(is_even(3) == "Odd"); - assert(is_even(101) == "Odd"); - assert(is_even(-5) == "Odd"); - assert(is_even(-999) == "Odd"); + assert(is_even(1) == false); + assert(is_even(3) == false); + assert(is_even(101) == false); + assert(is_even(-5) == false); + assert(is_even(-999) == false); std::cout << "All test cases successfully passed!" << std::endl; }