-
Notifications
You must be signed in to change notification settings - Fork 0
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
9b42595
commit b72c842
Showing
24 changed files
with
1,232 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,35 @@ | ||
#include <iostream> | ||
#include<vector> | ||
#include<string> | ||
|
||
std::vector<std::string> fizzbuzz(int n){ | ||
|
||
std::vector<std::string> result; | ||
|
||
for(int i = 1; i <= n; ++i) { | ||
|
||
if(i%3==0 && i%5!=0) | ||
result.push_back("Fizz"); | ||
else if(i%3!=0 && i%5==0) | ||
result.push_back("Buzz"); | ||
else if(i%3==0 && i%5==0 && i != 0) | ||
result.push_back("FizzBuzz"); | ||
else | ||
result.push_back(std::to_string(i)); | ||
} | ||
|
||
return result; | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
|
||
std::vector<std::string> ans = fizzbuzz(17); | ||
|
||
for(auto element : ans) | ||
std::cout << element << " "; | ||
std::cout << std::endl; | ||
|
||
|
||
} |
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,65 @@ | ||
/* | ||
Write a function that makesinput an array of distinct | ||
integers, and return the length of highest mountain | ||
A mountain is definied as adjacent integers that are strictly | ||
increasing until they reach a peak, at which the become | ||
stricly decreasing | ||
At least 3 number are required to form a mountain | ||
ex: | ||
10 | ||
/ \ -----> This is a mountain | ||
5 3 | ||
*/ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
|
||
// my Implementation | ||
|
||
int solve(std::vector<int> mountain) { | ||
|
||
int N = mountain.size(); | ||
int peak = 0; // found the highest peak at the mountain | ||
for(int i = 1; i < N-2;) { | ||
|
||
// First, we've to find a peak | ||
if(mountain[i] > mountain[i-1] && mountain[i] > mountain[i+1]) { | ||
|
||
int count = 1, j = i; | ||
|
||
// Finding the left size of the mountain | ||
while(j>= 1 && mountain[j]>mountain[j-1]) { | ||
j--; | ||
count++; | ||
} | ||
|
||
// finding the right size of the moutain | ||
while(i<=N-2 && mountain[i]>mountain[i+1]) { | ||
i++; | ||
count++; | ||
} | ||
|
||
peak = std::max(peak,count); | ||
|
||
} else | ||
i++; | ||
|
||
} | ||
|
||
return peak; | ||
|
||
} | ||
|
||
int main(){ | ||
|
||
std::vector<int> nums = {5,6,1,2,3,4,5,4,3,2,0,1,2,3,-2,4}; | ||
std::cout << solve(nums) << std::endl; | ||
|
||
} | ||
|
||
|
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,70 @@ | ||
/* | ||
Given an array containing N integers, and an number S denoting | ||
a target sum. | ||
Find two distinct integers that can pair up to form target sum. | ||
Let us assume there will be only one such pair | ||
input | ||
arra=[10,5,2,3,-6,9,11] | ||
S = 4 | ||
output | ||
[10,-6] | ||
*/ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <unordered_set> | ||
|
||
// solution with approach using sort vector | ||
void solve(std::vector<int> &v, int sum) { | ||
|
||
std::sort(v.begin(),v.end()); | ||
int l = 0, r = v.size()-1; | ||
|
||
while (l <= r) { | ||
if(v[l]+v[r] == sum) { | ||
std::cout << "[" << v[l] << "," << v[r] << "]" << std::endl; | ||
return; | ||
} if (v[l]+v[r] > sum) | ||
r--; | ||
else | ||
l++; | ||
} | ||
|
||
std::cout << "Doesn't exist" << std::endl; | ||
return; | ||
|
||
} | ||
|
||
// solution using unordered_set | ||
void solve2(std::vector<int> &v, int sum) { | ||
|
||
std::unordered_set<int> us; | ||
|
||
for(int i = 0; i < (int)v.size(); ++i) { | ||
if(us.find(sum-v[i])!=us.end()) { | ||
std::cout << "[" << v[i] << "," << sum-v[i] << "]" << std::endl; | ||
return; | ||
} | ||
us.emplace(v[i]); | ||
} | ||
|
||
|
||
std::cout << "Doesn't exist" << std::endl; | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
|
||
std::vector<int> nums = {10,5,2,3,-6,9,11}; | ||
int sum = 4; | ||
|
||
solve(nums,sum); | ||
solve2(nums,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,61 @@ | ||
/* | ||
Given an array containing N integers, and an Number S | ||
denoting a target sum. | ||
Find all distinct integers that can add up to form target | ||
sum. The numbers in each triplet should be ordered in | ||
ascending order, and triplet should be ordered too. | ||
Return empty array if no such triplet exist | ||
*/ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
std::vector<std::vector<int>> solve(std::vector<int> v, int sum){ | ||
|
||
std::vector<std::vector<int>> ans; | ||
// sorting the given vector | ||
std::sort(v.begin(),v.end()); | ||
|
||
// Pick every v[i] number and solve a pair sum for remaining part | ||
for(int i = 0; i < (int)v.size()-2; ++i) { | ||
|
||
int j = i+1; | ||
int k = (int)v.size()-1; | ||
|
||
// Two pointer approach | ||
while(j < k) { | ||
int current_sum = v[i]; | ||
if(current_sum+v[j]+v[k] == sum) { | ||
ans.push_back({current_sum,v[j],v[k]}); | ||
j++; | ||
k--; | ||
} else if(current_sum+v[j]+v[k] > sum) | ||
k--; | ||
else | ||
j++; | ||
} | ||
} | ||
|
||
return ans; | ||
|
||
} | ||
|
||
int main(){ | ||
|
||
std::vector<int> arr = {1,2,3,4,5,6,7,8,9,15}; | ||
int sum = 18; | ||
|
||
auto result = solve(arr,sum); | ||
|
||
for(auto rows : result) { | ||
for(auto elements : rows) | ||
std::cout << elements << " "; | ||
std::cout << std::endl; | ||
} | ||
|
||
|
||
} | ||
|
||
|
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 @@ | ||
#include <iostream> | ||
|
||
int main(void) { | ||
|
||
// to check if a number is even or odd, we just | ||
// have to see if the last bit is equa lto 0 or 1 | ||
int n; | ||
std::cin >> n; | ||
|
||
// if last bit is equal to one, iss a odd number | ||
if(n&1) | ||
std::cout << "Odd" << std::endl; | ||
else | ||
std::cout << "Even" << std::endl; | ||
|
||
|
||
} |
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,22 @@ | ||
#include <iostream> | ||
#include <bitset> | ||
|
||
int clearIthbit(int n, int i) { | ||
|
||
int mask = (1<<i); | ||
return (n&(~mask)); | ||
|
||
} | ||
|
||
int main(void) { | ||
|
||
int n, i; | ||
std::cin >> n >> i; | ||
|
||
std::cout << "Number(decimal) : " << n << std::endl; | ||
std::cout << "Number(binary) : " << std::bitset<4>(n) << std::endl; | ||
std::cout << "pposotion to change : " << i << std::endl; | ||
std::cout << "newNumber(binary) : " << std::bitset<4>(clearIthbit(n,i)) << std::endl; | ||
std::cout << "newNumber(decimal) : " << clearIthbit(n,i) << std::endl; | ||
|
||
} |
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,28 @@ | ||
#include <iostream> | ||
#include <bitset> | ||
|
||
|
||
int clearLastBits(int &n, int i) { | ||
|
||
int mask = (-1<<i); // -1 is equal to all numbers set to 1 | ||
return (n&mask); | ||
|
||
} | ||
|
||
int main(void) { | ||
|
||
int n, i; | ||
std::cin >> n >> i; | ||
|
||
std::cout << "Number(decimal) : " << n << std::endl; | ||
std::cout << "Number(binary) : " << std::bitset<4>(n) << std::endl; | ||
std::cout << "pposotion to change : " << i << std::endl; | ||
std::cout << "newNumber(binary) : " << std::bitset<4>(clearLastBits(n,i)) << std::endl; | ||
std::cout << "newNumber(decimal) : " << clearLastBits(n,i) << std::endl; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
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,22 @@ | ||
#include <iostream> | ||
#include <bitset> | ||
|
||
int getIthBit(int n, int i) { | ||
|
||
int mask = (1<<i); | ||
return (mask&n) > 0 ? 1 : 0; | ||
|
||
} | ||
|
||
|
||
int main(void) { | ||
|
||
int n, i; | ||
std::cin >> n >> i; | ||
|
||
std::cout << "Number(decimal): " << n << std::endl; | ||
std::cout << "Number(binary): " << std::bitset<8>(n) << std::endl; | ||
std::cout << "Bit at " << i << " position: " << getIthBit(n,i) << std::endl; | ||
|
||
|
||
} |
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,51 @@ | ||
/* | ||
You're given two 32-bit numbers, N and M, ando two bit positions | ||
i and j. | ||
Write a method to set all bits between i and j in N equal to M. | ||
M (becomes a substring of N locationed at and starting at j) | ||
Example: | ||
N = 10000000000 | ||
M = 10101 | ||
i = 2, j = 6 | ||
output: 1001010100 | ||
*/ | ||
#include <iostream> | ||
#include <bitset> | ||
|
||
|
||
// First we've to clear a range number of bits | ||
void clearBitsInRange(int &n, int i, int j) { | ||
|
||
int a = (-1)<<(j+1); | ||
int b = (1<<i) - 1; | ||
int mask = a|b; | ||
n = n & mask; | ||
|
||
} | ||
|
||
|
||
// So, we can just make the or operation after left shifting M | ||
void replace(int &n, int &m, int i, int j) { | ||
|
||
clearBitsInRange(n,i,j); | ||
int mask = (m<<i); | ||
n = n | mask; | ||
|
||
} | ||
|
||
|
||
int main(void) { | ||
|
||
int n = 15; | ||
int i = 1; | ||
int j = 3; | ||
int m = 2; | ||
|
||
|
||
replace(n,m,i,j); | ||
std::cout << std::bitset<8>(n) << std::endl; | ||
|
||
} | ||
|
Oops, something went wrong.