-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2368866
commit b1d101d
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |