Skip to content

Commit

Permalink
Added some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Jan 30, 2024
1 parent 2368866 commit b1d101d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions leetcode/problems/cpp/number-of-even-and-odd-bits..cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

#include <vector>
using namespace std;

class Solution {
public:
vector<int> evenOddBit(int n) {
vector<int> ans(2, 0);
int i = 0;
while (n) {
ans[i] += n & 1;
n >>= 1;
i ^= 1;
}
return ans;
}
};
21 changes: 21 additions & 0 deletions leetcode/problems/cpp/sum-of-all-odd-length-subarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

#include "vector"
using namespace std;

class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
int n = arr.size();
int sum = 0;
for (int i = 0; i < n; i++) {
int left = i + 1, right = n - i;
int left_even = (left + 1) / 2, right_even = (right + 1) / 2;
int left_odd = left / 2, right_odd = right / 2;
sum += (left_even * right_even + left_odd * right_odd) * arr[i];
}
return sum;
}
};
17 changes: 17 additions & 0 deletions leetcode/problems/rs/number-of-even-and-odd-bits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

impl Solution {
pub fn even_odd_bit(n: i32) -> Vec<i32> {
let mut ans = vec![0; 2];
let mut n = n;
let mut i = 0;
while n > 0 {
ans[i] += n & 1;
n >>= 1;
i ^= 1;
}
ans
}
}

0 comments on commit b1d101d

Please sign in to comment.