Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
7oSkaaa committed Jul 4, 2023
2 parents a83a9c9 + 29ad8b6 commit 49f43fe
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions 07- July/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**

<hr>
<br><br>
Expand Down Expand Up @@ -211,4 +212,48 @@ public:
}
};
```

<hr>
<br><br>

## 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<int>& 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;
}
};
```

0 comments on commit 49f43fe

Please sign in to comment.