From e698c356540514d7a8b48bb6a8c83b1ba766fdee Mon Sep 17 00:00:00 2001 From: Wander_Lust Date: Thu, 5 Mar 2026 19:55:52 +0530 Subject: [PATCH] add codeforces solutions folder --- codeforces/231A.cpp | 22 ++ leetcode/lc1545/IterativeDivideConquer.cpp | 52 ++++ leetcode/lc1545/brute.cpp | 17 ++ leetcode/lc1545/recursion.cpp | 48 ++++ leetcode/lc1582/brute.cpp | 32 +++ leetcode/lc1582/precompute1s.cpp | 31 +++ leetcode/lc1582/spaceOptimized.cpp | 48 ++++ leetcode/lc1689.cpp | 17 ++ leetcode/lc1758.cpp | 25 ++ leetcode/lc191.cpp | 18 ++ leetcode/lc237.cpp | 11 + leetcode/lc33.cpp | 45 ++++ leetcode/lc35.cpp | 20 ++ leetcode/lc401/BinaryEnumeration.cpp | 14 + leetcode/lc401/enumerateHrsMins.cpp | 14 + leetcode/lc401/notes.md | 291 +++++++++++++++++++++ leetcode/lc693/AlternatingCompare.cpp | 15 ++ leetcode/lc693/AlternatingXOR.cpp | 10 + leetcode/lc762.cpp | 30 +++ leetcode/lc868.cpp | 18 ++ 20 files changed, 778 insertions(+) create mode 100644 codeforces/231A.cpp create mode 100644 leetcode/lc1545/IterativeDivideConquer.cpp create mode 100644 leetcode/lc1545/brute.cpp create mode 100644 leetcode/lc1545/recursion.cpp create mode 100644 leetcode/lc1582/brute.cpp create mode 100644 leetcode/lc1582/precompute1s.cpp create mode 100644 leetcode/lc1582/spaceOptimized.cpp create mode 100644 leetcode/lc1689.cpp create mode 100644 leetcode/lc1758.cpp create mode 100644 leetcode/lc191.cpp create mode 100644 leetcode/lc237.cpp create mode 100644 leetcode/lc33.cpp create mode 100644 leetcode/lc35.cpp create mode 100644 leetcode/lc401/BinaryEnumeration.cpp create mode 100644 leetcode/lc401/enumerateHrsMins.cpp create mode 100644 leetcode/lc401/notes.md create mode 100644 leetcode/lc693/AlternatingCompare.cpp create mode 100644 leetcode/lc693/AlternatingXOR.cpp create mode 100644 leetcode/lc762.cpp create mode 100644 leetcode/lc868.cpp diff --git a/codeforces/231A.cpp b/codeforces/231A.cpp new file mode 100644 index 0000000..9f8fcbb --- /dev/null +++ b/codeforces/231A.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +int main() { + int n; + cin >> n; + + int count = 0; + + for(int i = 0; i < n; i++) { + int a, b, c; + cin >> a >> b >> c; + + if(a + b + c >= 2) { + count++; + } + } + + cout << count << endl; + + return 0; +} diff --git a/leetcode/lc1545/IterativeDivideConquer.cpp b/leetcode/lc1545/IterativeDivideConquer.cpp new file mode 100644 index 0000000..5f63868 --- /dev/null +++ b/leetcode/lc1545/IterativeDivideConquer.cpp @@ -0,0 +1,52 @@ +class Solution{ + public: + char findKthBit(int n, int k){ + int invertCount = 0; + int len = (1<1){ + if(k==len/2+1){ + return invertCount % 2==0? '1' : '0'; + + } + if(k>len/2){ + k = len-k+1; + invertCount++; + } + len/=2; + } + return invertCount%2==0?'0':'1'; + + } +}; + +//TC: o(n) , SC : o(1) + +/* +Approach: + +We do NOT build the full string Sn because its length grows exponentially (2^n - 1). +Instead, we use the recursive structure: + +Sn = S(n-1) + "1" + reverse(invert(S(n-1))) + +For any position k: +1) If k lies in the left half: + → It directly corresponds to the same position in S(n-1). + +2) If k is the middle element: + → The value is always '1'. + +3) If k lies in the right half: + → The right half is reverse(invert(S(n-1))). + → Mirror the index into the left half. + → Remember that the value must be inverted. + +We keep reducing the problem from Sn → S(n-1) until we reach +the base case S1 = "0". + +The final answer depends on how many times we entered +the right half (i.e., how many inversions occurred). + +Time Complexity: O(n) +Space Complexity: O(1) (iterative version) +*/ diff --git a/leetcode/lc1545/brute.cpp b/leetcode/lc1545/brute.cpp new file mode 100644 index 0000000..936b820 --- /dev/null +++ b/leetcode/lc1545/brute.cpp @@ -0,0 +1,17 @@ +class Solution{ + public: + char findKthBit(int n, int k){ + string seq = "0"; + for(int i = 1;i=0;--j){ + char invertedBit = (temp[j]=='1')? '0': '1'; + seq+=invertedBit; + } + } + return seq[k-1]; + + + } +} diff --git a/leetcode/lc1545/recursion.cpp b/leetcode/lc1545/recursion.cpp new file mode 100644 index 0000000..282fc2c --- /dev/null +++ b/leetcode/lc1545/recursion.cpp @@ -0,0 +1,48 @@ +class Solution{ + public: + char findKthBit(int n, int k){ + if(n==1) return '0'; + int len = 1<> &mat){ + int ans = 0; + int r = mat.size(); + int c = mat[0].size(); + for(int row=0;row>& mat) { + int m = mat.size(); + int n = mat[0].size(); + vector rowCount(m, 0); + vector colCount(n, 0); + + for (int row = 0; row < m; row++) { + for (int col = 0; col < n; col++) { + if (mat[row][col] == 1) { + rowCount[row]++; + colCount[col]++; + } + } + } + + int ans = 0; + for (int row = 0; row < m; row++) { + for (int col = 0; col < n; col++) { + if (mat[row][col] == 1) { + if (rowCount[row] == 1 && colCount[col] == 1) { + ans++; + } + } + } + } + + return ans; + } +}; diff --git a/leetcode/lc1582/spaceOptimized.cpp b/leetcode/lc1582/spaceOptimized.cpp new file mode 100644 index 0000000..dd249cb --- /dev/null +++ b/leetcode/lc1582/spaceOptimized.cpp @@ -0,0 +1,48 @@ +class Solution{ + public: + int numSpecial(vector> &mat){ + int ans = 0; + int m = mat.size(); + int n = mat[0].size(); + for(int row = 0;row>i && 1) res ++; + }*/ + + //brian-kernighan's algo + while(n){ + n = n&(n-1); + res++ + + } + return res; + } +}; +//n&n-1 resets the last set bit to 0, brian's algo takes o(number of set bits to run) unlike brute force which takes o(total no. of bits). both are o(1). diff --git a/leetcode/lc237.cpp b/leetcode/lc237.cpp new file mode 100644 index 0000000..44a40ef --- /dev/null +++ b/leetcode/lc237.cpp @@ -0,0 +1,11 @@ +class Solution{ + public : + void deleteNode(ListNode* node){ + ListNode* nextNode = node->next; + node->val = nextNode->val; + node->next = nextNode->next; + delete nextNode; + } +} +//The idea behind this solution is that since we are not given the head pointer and therefore cannot access the previous node, we cannot delete the current node in the normal way (which would require updating prev->next). So instead of deleting the given node directly, we copy the value of its next node into it. This makes the current node effectively take on the identity of the next node. Then we adjust the current node’s next pointer to skip over the next node, thereby removing it from the list. Finally, we delete that next node to free memory. This works because the problem guarantees that the given node is not the tail, so a next node always exists. + diff --git a/leetcode/lc33.cpp b/leetcode/lc33.cpp new file mode 100644 index 0000000..72246f8 --- /dev/null +++ b/leetcode/lc33.cpp @@ -0,0 +1,45 @@ +class Solution{ + public: + int search(vector nums, int target){ + int start = 0, end = nums.size() - 1; + while(left<=right){ + int mid = start + (end - start)/2; + if(nums[mid]==target) return mid; + + if(nums[start]<=nums[mid]){ + if(target>=nums[start] && target< nums[mid]) + end = mid-1 + else + start = mid+1; + } + else{ + if(target>nums[mid] && target<= nums[end]) + start = mid+1; + else + end = mid-1; + + } + return -1; + } +}; + +//TC : o(logn), SC: o(1) + +/* +Approach: + +This is a modified binary search for a rotated sorted array. +The array was originally sorted in ascending order and then rotated once, +which means there is exactly one pivot point. Because of this, at any +given mid index, at least one half (left or right) must still be sorted. + +At each step: +1) Find the middle index. +2) Determine which half is sorted. +3) Check if the target lies within the sorted half. + - If yes, move the search to that half. + - If no, move to the other half. + +By always eliminating half of the array, we maintain +O(log n) time complexity without explicitly finding the pivot. +*/ diff --git a/leetcode/lc35.cpp b/leetcode/lc35.cpp new file mode 100644 index 0000000..597d789 --- /dev/null +++ b/leetcode/lc35.cpp @@ -0,0 +1,20 @@ +class Solution{ + public: + int searchInsert(vector&nums, int target){ + int start = 0, end = nums.size() -1; + while(start<=end){ + int mid = left+(right-left)/2; + if(nums[mid]==target) return mid; + + if(target readBinaryWatch(int turned On){ + vector ans; + for(int i =0;i<1024;i++){ + int h = i>>6, m = i & 63; + if(h<12 && m<60 && __builtin_popcount(i) == turnedOn) + ans.push_back(to_string(h)+":"+(m<10?"0":"")+to_string(m)); + } + return ans; + } +} +//TC : o(1) as total number of enumerations will be independent of input size +//SC : o(1) diff --git a/leetcode/lc401/enumerateHrsMins.cpp b/leetcode/lc401/enumerateHrsMins.cpp new file mode 100644 index 0000000..5ed2228 --- /dev/null +++ b/leetcode/lc401/enumerateHrsMins.cpp @@ -0,0 +1,14 @@ +class Solution{ + public : + vector readBinaryWatch(int turnedOn){ + vector ans; + for(int h=0;h<12;++h){ + for(int m = 0;m<60;++m){ + if(__builtin_popcount(h)+ __builtin_popcount(m) == turnedOn){ + ans.push_back(to_string(h)+":"+(m<10?"0":"")+to_string(m)); + } + } + } + return ans; + } +} diff --git a/leetcode/lc401/notes.md b/leetcode/lc401/notes.md new file mode 100644 index 0000000..fee7807 --- /dev/null +++ b/leetcode/lc401/notes.md @@ -0,0 +1,291 @@ +# C++ Bit Manipulation Reference + +This document contains: +- GCC / Clang built-in bit functions +- C++20 `` header utilities +- C++23 additions + +--- + +# Part 1 — GCC / Clang Built-in Bit Functions + +These are compiler-specific (GCC / Clang). +They are fast (usually O(1)) and often map to CPU instructions. + +--- + +## 1. `__builtin_popcount` + +Counts number of set bits (1s). + +### Variants + +```cpp +__builtin_popcount(x) // → int +__builtin_popcountl(x) // → long +__builtin_popcountll(x) // → long long +``` + +### Example + +```cpp +int x = 13; // 1101 +int cnt = __builtin_popcount(x); // 3 +``` + +--- + +## 2. `__builtin_clz` + +Count Leading Zeros (from MSB). + +### Variants + +```cpp +__builtin_clz(x) +__builtin_clzl(x) +__builtin_clzll(x) +``` + +### Example + +```cpp +int x = 8; // 000...1000 +int zeros = __builtin_clz(x); // 28 (32-bit int) +``` + +### Notes + +> ⚠️ Undefined if `x == 0` + +```cpp +// Highest set bit index: +int highest = 31 - __builtin_clz(x); +``` + +--- + +## 3. `__builtin_ctz` + +Count Trailing Zeros (from LSB). + +### Variants + +```cpp +__builtin_ctz(x) +__builtin_ctzl(x) +__builtin_ctzll(x) +``` + +### Example + +```cpp +int x = 8; // 1000 +int zeros = __builtin_ctz(x); // 3 +``` + +> ⚠️ Undefined if `x == 0` + +--- + +## 4. `__builtin_parity` + +Returns `1` if odd number of set bits, `0` if even. + +```cpp +int x = 7; // 111 +int p = __builtin_parity(x); // 1 +``` + +--- + +## 5. `__builtin_ffs` + +Find First Set Bit (1-indexed from right). Returns `0` if input is `0`. + +```cpp +int x = 10; // 1010 +int pos = __builtin_ffs(x); // 2 +``` + +--- + +# Part 2 — C++20 `` Header (Portable Standard) + +```cpp +#include +``` + +> Works on **unsigned integer types** only. + +--- + +## 1. `std::popcount` + +Counts number of set bits. + +```cpp +std::popcount(x); +``` + +--- + +## 2. `std::countl_zero` + +Count leading zeros. Safe for `x == 0` (returns bit width). + +```cpp +std::countl_zero(x); +``` + +--- + +## 3. `std::countl_one` + +Count consecutive leading ones. + +```cpp +std::countl_one(x); +``` + +--- + +## 4. `std::countr_zero` + +Count trailing zeros. Safe for `x == 0` (returns bit width). + +```cpp +std::countr_zero(x); +``` + +--- + +## 5. `std::countr_one` + +Count consecutive trailing ones. + +```cpp +std::countr_one(x); +``` + +--- + +## 6. `std::has_single_bit` + +Returns `true` if exactly one bit is set (i.e., power of 2). + +```cpp +std::has_single_bit(x); + +// Equivalent to: +x > 0 && (x & (x - 1)) == 0; +``` + +--- + +## 7. `std::bit_width` + +Number of bits required to represent `x`. + +```cpp +std::bit_width(x); + +// Equivalent to (for x > 0): +floor(log2(x)) + 1; +``` + +--- + +## 8. `std::bit_floor` + +Largest power of 2 ≤ `x`. + +```cpp +std::bit_floor(x); + +// Example: +std::bit_floor(10); // → 8 +``` + +--- + +## 9. `std::bit_ceil` + +Smallest power of 2 ≥ `x`. + +```cpp +std::bit_ceil(x); + +// Example: +std::bit_ceil(10); // → 16 +``` + +--- + +## 10. `std::rotl` — Rotate Left + +Circular left rotation. + +```cpp +std::rotl(x, s); +``` + +--- + +## 11. `std::rotr` — Rotate Right + +Circular right rotation. + +```cpp +std::rotr(x, s); +``` + +--- + +# Part 3 — C++23 Additions + +--- + +## 1. `std::byteswap` + +Swap byte order (endianness conversion). + +```cpp +std::byteswap(x); + +// Example: +// 0x12345678 → 0x78563412 +``` + +--- + +## 2. `std::endian` + +Detect system byte order. + +```cpp +#include + +if (std::endian::native == std::endian::little) { + // little-endian system +} +``` +# Applications +A number of the form 1 << k has a one bit in position k and all other bits are zero, +so we can use such numbers to access single bits of numbers. In particular, the +kth bit of a number is one exactly when x & (1 << k) is not zero. The following +code prints the bit representation of an int number x: +``` +for (int i = 31; i >= 0; i--) { +if (x&(1<>1); + return (m&(m+1)==0); + } +} + +// TC & SC : o(1) +// n>>1 inverts all bits and XOR makes m have all set bits if n had alternating bits. (m+1) makes all bits of m as zero and m&(m+1) will be zero diff --git a/leetcode/lc762.cpp b/leetcode/lc762.cpp new file mode 100644 index 0000000..f7a985a --- /dev/null +++ b/leetcode/lc762.cpp @@ -0,0 +1,30 @@ +class Solution{ + public: + int countPrimeSetBits(int left, int right){ + int ans = 0; + for(int i = left;i<=right;i++){ + if(isPrime(__builtin_popcount(i))) + ans++; + } + return ans; + } + bool isPrime(int n){ + if(n<2) return false; + for(int i =2;i*i<=n;i++) + if(n%i == 0) return false; + return true; + } +}; +//TC : o(right-left+1) +//SC : o(1) + +/* +int countPrimeSetBits(int L, int R) { + int cnt = 0, hash[20] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1}; + for (int i = L; i <= R; i++) { + bitset<20> b(i); + if(hash[b.count()]) cnt++; + } + return cnt; + } + */ diff --git a/leetcode/lc868.cpp b/leetcode/lc868.cpp new file mode 100644 index 0000000..7be372a --- /dev/null +++ b/leetcode/lc868.cpp @@ -0,0 +1,18 @@ +class Solution{ + public : + int binaryGap(int n){ + int last = -1; + int ans =0; + for(int i =0;i<32;i++){ + if(n>>i & 1){ + if(last!=-1) + ans = max(ans, i-last); + last = i; + } + } + return ans; + } +}; +//We'll store last, the index of the last set bit encountered. If n has the ith bit set, answer is i - last, and then the new last value would be last = i. +//TC : o(logn) , logn is the number of digits in the binary representation of n +//SC : o(1)