Skip to content

Commit bbd3d0a

Browse files
authored
Merge pull request #3712 from AmruthaPariprolu/feature/2275
solution added to 2275
2 parents 4b67742 + 89da0a5 commit bbd3d0a

File tree

1 file changed

+312
-0
lines changed

1 file changed

+312
-0
lines changed
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
---
2+
id: Largest-Combination-With-Bitwise-AND-Greater-Than-Zero
3+
title: Largest Combination With Bitwise AND Greater Than Zero
4+
sidebar_label: 2275-Largest Combination With Bitwise AND Greater Than Zero
5+
- Arrays
6+
- Brute Force
7+
- Optimized approach
8+
- LeetCode
9+
- Python
10+
- Java
11+
- C++
12+
13+
description: "This is a solution to Largest Combination With Bitwise AND Greater Than Zero problem on LeetCode."
14+
sidebar_position: 76
15+
---
16+
17+
## Problem Statement
18+
In this tutorial, we will solve the Largest Combination With Bitwise AND Greater Than Zero problem . We will provide the implementation of the solution in Python, Java, and C++.
19+
20+
### Problem Description
21+
22+
The bitwise AND of an array nums is the bitwise AND of all integers in nums.
23+
24+
For example, for nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.
25+
Also, for nums = [7], the bitwise AND is 7.
26+
You are given an array of positive integers candidates. Evaluate the bitwise AND of every combination of numbers of candidates. Each number in candidates may only be used once in each combination.
27+
28+
Return the size of the largest combination of candidates with a bitwise AND greater than 0.
29+
30+
31+
### Examples
32+
33+
**Example 1:**
34+
Input: candidates = [16,17,71,62,12,24,14]
35+
Output: 4
36+
Explanation: The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
37+
The size of the combination is 4.
38+
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
39+
Note that more than one combination may have the largest size.
40+
For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
41+
**Example 2:**
42+
Input: candidates = [8,8]
43+
Output: 2
44+
Explanation: The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
45+
The size of the combination is 2, so we return 2.
46+
47+
### Constraints
48+
- `1 <= candidates.length <= 105`
49+
- `1 <= candidates[i] <= 107`
50+
## Solution of Given Problem
51+
52+
### Intuition and Approach
53+
54+
The problem can be solved using a brute force approach or an optimized Technique.
55+
56+
<Tabs>
57+
<tabItem value="Brute Force" label="Brute Force">
58+
59+
### Approach 1:Brute Force (Naive)
60+
61+
62+
Brute Force Approach: The brute force approach would involve evaluating the bitwise AND for every possible combination of the given candidates. However, given the constraints, this approach is impractical due to its high computational complexity.
63+
#### Codes in Different Languages
64+
65+
<Tabs>
66+
<TabItem value="C++" label="C++" default>
67+
<SolutionAuthor name="@AmruthaPariprolu"/>
68+
69+
```cpp
70+
#include <vector>
71+
#include <algorithm>
72+
73+
int largestCombinationBruteForce(std::vector<int>& candidates) {
74+
int n = candidates.size();
75+
int max_size = 0;
76+
77+
// Iterate over all possible combinations of candidates
78+
for (int mask = 1; mask < (1 << n); ++mask) {
79+
int bitwise_and = -1; // Initialize with all bits set
80+
int count = 0;
81+
82+
for (int i = 0; i < n; ++i) {
83+
if (mask & (1 << i)) {
84+
if (bitwise_and == -1) {
85+
bitwise_and = candidates[i];
86+
} else {
87+
bitwise_and &= candidates[i];
88+
}
89+
++count;
90+
}
91+
}
92+
93+
if (bitwise_and > 0) {
94+
max_size = std::max(max_size, count);
95+
}
96+
}
97+
98+
return max_size;
99+
}
100+
101+
102+
103+
```
104+
</TabItem>
105+
<TabItem value="Java" label="Java">
106+
<SolutionAuthor name="@AmruthaPariprolu"/>
107+
108+
```java
109+
import java.util.*;
110+
111+
public class BruteForceSolution {
112+
public int largestCombinationBruteForce(int[] candidates) {
113+
int n = candidates.length;
114+
int max_size = 0;
115+
116+
// Iterate over all possible combinations of candidates
117+
for (int mask = 1; mask < (1 << n); ++mask) {
118+
int bitwiseAnd = -1; // Initialize with all bits set
119+
int count = 0;
120+
121+
for (int i = 0; i < n; ++i) {
122+
if ((mask & (1 << i)) != 0) {
123+
if (bitwiseAnd == -1) {
124+
bitwiseAnd = candidates[i];
125+
} else {
126+
bitwiseAnd &= candidates[i];
127+
}
128+
++count;
129+
}
130+
}
131+
132+
if (bitwiseAnd > 0) {
133+
max_size = Math.max(max_size, count);
134+
}
135+
}
136+
137+
return max_size;
138+
}
139+
}
140+
141+
142+
```
143+
144+
145+
</TabItem>
146+
<TabItem value="Python" label="Python">
147+
<SolutionAuthor name="@AmruthaPariprolu"/>
148+
149+
```python
150+
import java.util.*;
151+
152+
public class BruteForceSolution {
153+
public int largestCombinationBruteForce(int[] candidates) {
154+
int n = candidates.length;
155+
int max_size = 0;
156+
157+
// Iterate over all possible combinations of candidates
158+
for (int mask = 1; mask < (1 << n); ++mask) {
159+
int bitwiseAnd = -1; // Initialize with all bits set
160+
int count = 0;
161+
162+
for (int i = 0; i < n; ++i) {
163+
if ((mask & (1 << i)) != 0) {
164+
if (bitwiseAnd == -1) {
165+
bitwiseAnd = candidates[i];
166+
} else {
167+
bitwiseAnd &= candidates[i];
168+
}
169+
++count;
170+
}
171+
}
172+
173+
if (bitwiseAnd > 0) {
174+
max_size = Math.max(max_size, count);
175+
}
176+
}
177+
178+
return max_size;
179+
}
180+
}
181+
182+
183+
```
184+
185+
</TabItem>
186+
</Tabs>
187+
188+
189+
### Complexity Analysis
190+
191+
- Time Complexity: $O(2^n*n)$
192+
- Evaluates all possible subsets (2^n) and performs bitwise AND operations on each subset.
193+
- Space Complexity: $O(1)$
194+
- Uses a constant amount of extra space.
195+
196+
197+
</tabItem>
198+
<tabItem value="Optimized approach" label="Optimized approach">
199+
200+
### Approach 2: Optimized approach
201+
202+
Optimized Approach: We can use bitwise manipulation to efficiently determine the largest combination of candidates with a bitwise AND greater than 0. The key idea is to examine each bit position independently and count how many numbers have that bit set. The maximum number of such numbers for any bit position gives the size of the largest combination with a bitwise AND greater than 0.
203+
204+
#### Code in Different Languages
205+
206+
<Tabs>
207+
<TabItem value="C++" label="C++" default>
208+
<SolutionAuthor name="@AmruthaPariprolu"/>
209+
210+
```cpp
211+
#include <vector>
212+
#include <algorithm>
213+
214+
int largestCombinationOptimized(const std::vector<int>& candidates) {
215+
int max_count = 0;
216+
217+
// Iterate over each bit position
218+
for (int bit = 0; bit < 31; ++bit) {
219+
int count = 0;
220+
int bit_mask = 1 << bit;
221+
222+
for (int num : candidates) {
223+
if (num & bit_mask) {
224+
++count;
225+
}
226+
}
227+
228+
max_count = std::max(max_count, count);
229+
}
230+
231+
return max_count;
232+
}
233+
234+
235+
```
236+
</TabItem>
237+
<TabItem value="Java" label="Java">
238+
<SolutionAuthor name="@AmruthaPariprolu"/>
239+
240+
```java
241+
import java.util.*;
242+
243+
public class OptimizedSolution {
244+
public int largestCombinationOptimized(int[] candidates) {
245+
int maxCount = 0;
246+
247+
// Iterate over each bit position
248+
for (int bit = 0; bit < 31; ++bit) {
249+
int count = 0;
250+
int bitMask = 1 << bit;
251+
252+
for (int num : candidates) {
253+
if ((num & bitMask) != 0) {
254+
++count;
255+
}
256+
}
257+
258+
maxCount = Math.max(maxCount, count);
259+
}
260+
261+
return maxCount;
262+
}
263+
}
264+
265+
266+
```
267+
268+
269+
</TabItem>
270+
<TabItem value="Python" label="Python">
271+
<SolutionAuthor name="@AmruthaPariprolu"/>
272+
273+
```python
274+
def largest_combination_optimized(candidates):
275+
max_count = 0
276+
277+
# Iterate over each bit position
278+
for bit in range(31):
279+
bit_mask = 1 << bit
280+
count = sum(1 for num in candidates if num & bit_mask)
281+
282+
max_count = max(max_count, count)
283+
284+
return max_count
285+
286+
287+
288+
```
289+
290+
</TabItem>
291+
</Tabs>
292+
293+
#### Complexity Analysis
294+
295+
- Time Complexity: $O(n)$
296+
- Iterates over each of the 31 possible bit positions and counts how many candidates have that bit set.
297+
- Space Complexity: $O(1)$
298+
- Uses only a few extra variables for counting and tracking the maximum size.
299+
- This approach is efficient and straightforward.
300+
301+
</tabItem>
302+
</Tabs>
303+
304+
---
305+
306+
<h2>Authors:</h2>
307+
308+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
309+
{['AmruthaPariprolu'].map(username => (
310+
<Author key={username} username={username} />
311+
))}
312+
</div>

0 commit comments

Comments
 (0)