diff --git a/Pull Here/LeetCode/CountingBits/Iamalok007.cpp b/Pull Here/LeetCode/CountingBits/Iamalok007.cpp new file mode 100644 index 0000000..e5fb341 --- /dev/null +++ b/Pull Here/LeetCode/CountingBits/Iamalok007.cpp @@ -0,0 +1,44 @@ +#include +#include + +class Solution { +public: + std::vector countBits(int n) { + std::vector ans; + for (int i = 0; i <= n; i++) { + int temp = i; + int count = 0; + while (temp > 0) { + if (temp % 2 != 0) { + count++; + } + temp = temp / 2; + } + ans.push_back(count); + } + return ans; + } +}; + +int main() { + int n; + std::cout << "Enter a number (n): "; + std::cin >> n; + + Solution solution; + std::vector result = solution.countBits(n); + + std::cout << "Count of 1s in binary representation from 0 to " << n << ":\n"; + for (int i = 0; i <= n; i++) { + std::cout << i << ": " << result[i] << " "; + } + + return 0; +} + + +/*output: +Enter a number (n): 5 +Count of 1s in binary representation from 0 to 5: +0: 0 1: 1 2: 1 3: 2 4: 1 5: 2 +*/ \ No newline at end of file