Skip to content

Commit

Permalink
practice leetcode - largest combination with bitwise and greater than…
Browse files Browse the repository at this point in the history
… zero
  • Loading branch information
vuquang23 committed Jan 6, 2025
1 parent a608f2f commit ec8facb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "largest_combination_with_bitwise_and_greater_than_zero"
version = "0.1.0"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/

struct Solution;

impl Solution {
pub fn largest_combination(candidates: Vec<i32>) -> i32 {
use std::cmp::max;
let mut ans = 0;
for i in 0..25 {
let mut cnt = 0;
for &num in candidates.iter() {
if (num & (1 << i)) != 0 {
cnt += 1;
}
}
ans = max(ans, cnt);
}
ans
}
}

fn main() {
let result = Solution::largest_combination(vec![16, 17, 71, 62, 12, 24, 14]);
println!("{result}");
}

0 comments on commit ec8facb

Please sign in to comment.