From 8541bb367440dea46d58953f6561d9dd480a5249 Mon Sep 17 00:00:00 2001 From: Rudra Pratap Singh <145354880+Rudra2637@users.noreply.github.com> Date: Sat, 27 Sep 2025 10:15:09 +0530 Subject: [PATCH 1/2] Fix: use uint64_t for input and counter in countSetBits (#3003) Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- bit_manipulation/count_of_set_bits.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/count_of_set_bits.cpp b/bit_manipulation/count_of_set_bits.cpp index dd29fd8bf19..dfc4d7fec7e 100644 --- a/bit_manipulation/count_of_set_bits.cpp +++ b/bit_manipulation/count_of_set_bits.cpp @@ -36,11 +36,19 @@ namespace count_of_set_bits { * @returns total number of set-bits in the binary representation of number `n` */ std::uint64_t countSetBits( - std ::int64_t n) { // int64_t is preferred over int so that + std ::uint64_t n) { // uint64_t is preferred over int so that // no Overflow can be there. + //It's preferred over int64_t because it Guarantees that inputs are always non-negative, + //which matches the algorithmic problem statement. + //set bit counting is conceptually defined only for non-negative numbers. + //Provides a type Safety: Using an unsigned type helps prevent accidental negative values, + + std::uint64_t count = 0; // "count" variable is used to count number of set-bits('1') + // in binary representation of number 'n' + //Count is uint64_t because it Prevents theoretical overflow if someone passes very large integers. + // Behavior stays the same for all normal inputs. + // Safer for edge cases. - int count = 0; // "count" variable is used to count number of set-bits('1') - // in binary representation of number 'n' while (n != 0) { ++count; n = (n & (n - 1)); From 643e0a96e05295e2798d47dcbcf9b43b81e23de1 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sat, 27 Sep 2025 06:56:46 +0200 Subject: [PATCH 2/2] style: remove interaction with the user (#3006) Helps with #2753 Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- math/number_of_positive_divisors.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/math/number_of_positive_divisors.cpp b/math/number_of_positive_divisors.cpp index 9407c4dc8d0..6d8f8589678 100644 --- a/math/number_of_positive_divisors.cpp +++ b/math/number_of_positive_divisors.cpp @@ -23,7 +23,6 @@ **/ #include -#include /** * Function to compute the number of positive divisors. @@ -80,13 +79,5 @@ void tests() { */ int main() { tests(); - int n; - std::cin >> n; - if (n == 0) { - std::cout << "All non-zero numbers are divisors of 0 !" << std::endl; - } else { - std::cout << "Number of positive divisors is : "; - std::cout << number_of_positive_divisors(n) << std::endl; - } return 0; }