diff --git a/07- July/README.md b/07- July/README.md index 0742da289..d35f4d77f 100644 --- a/07- July/README.md +++ b/07- July/README.md @@ -24,6 +24,7 @@ 1. **[Fair Distribution of Cookies](#01--fair-distribution-of-cookies)** 1. **[Maximum Number of Achievable Transfer Requests](#02--maximum-number-of-achievable-transfer-requests)** 1. **[Buddy Strings](#03--buddy-strings)** +1. **[Single Number II](#04--single-number-ii)**


@@ -211,4 +212,48 @@ public: } }; ``` + +
+

+ +## 04) [Single Number II](https://leetcode.com/problems/single-number-ii/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Array` `Bit Manipulation` + +### Code + + +```cpp +class Solution { +public: + int singleNumber(vector& nums) { + // Variable to store the single number + int single = 0; + + // Loop through each bit position from 0 to 31 + for(int bits = 0, curr_bit = 0; bits < 32; bits++, curr_bit = 0) { + // Iterate over each element in the vector + for(auto& i : nums) { + // Check if the current bit is set in the element + if(i & (1ll << bits)) + curr_bit++; + } + + // Check if the count of the current bit is not a multiple of 3 + if(curr_bit % 3 == 1) + // Set the corresponding bit in the single variable + single |= (1 << bits); + } + + // Return the single number + return single; + } +}; +``` \ No newline at end of file