-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from TesseractCoding/master
mpr
- Loading branch information
Showing
14 changed files
with
759 additions
and
5 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
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
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,49 @@ | ||
/* | ||
Given a string s consists of some words separated by spaces, | ||
return the length of the last word in the string. | ||
If the last word does not exist, return 0. | ||
A word is a maximal substring consisting of non-space characters only. | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/* | ||
Well, the basic idea is very simple. | ||
Start from the tail of s and move backwards to find the first non-space character. | ||
Then from this character, move backwards and count the number of non-space characters | ||
until we pass over the head of s or meet a space character. | ||
The count will then be the length of the last word. | ||
*/ | ||
|
||
int lengthOfLastWord(string s) | ||
{ | ||
int lengthofword = 0, tail = s.length() - 1; | ||
while (tail >= 0 && s[tail] == ' ') | ||
tail--; | ||
while (tail >= 0 && s[tail] != ' ') | ||
{ | ||
lengthofword++; | ||
tail--; | ||
} | ||
return lengthofword; | ||
} | ||
|
||
int main() | ||
{ | ||
cout << "Enter the string:"; | ||
string str; | ||
getline(cin, str); | ||
|
||
int ans = lengthOfLastWord(str); | ||
cout << "The length of the last word in the string is: " << ans; | ||
} | ||
|
||
/* | ||
Input:Hello World | ||
Output:5 | ||
*/ | ||
/* | ||
Time Complexity:O(n),where n is the length of the string | ||
Space Complexity:O(1) | ||
*/ |
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,47 @@ | ||
/* Problem Statement => | ||
Given a “2 x n” board and tiles of size “2 x 1”, count the number of ways to | ||
tile the given board using the 2 x 1 tiles. | ||
A tile can either be placed horizontally i.e., as a 1 x 2 tile or vertically | ||
i.e., as 2 x 1 tile. */ | ||
|
||
#include <iostream> | ||
#include <climits> | ||
#include <vector> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
long long numberOfWays(long long length){ | ||
int mod = 1000000007; | ||
vector<long long> dp(length + 1, 0); | ||
|
||
// Case 1: if n == 1 , only 1 configuration of tile is possible | ||
dp[1] = 1; | ||
|
||
// Case 2: if n == 2 , only 2 configurations are possible | ||
dp[2] = 2; | ||
|
||
/* Case 3: if n > 2, then we got 2 possible subcases: | ||
Subcase 1: if we place length wise, call dp[i-2]; | ||
Subcase 2: if we place width wise, call dp[i-1]; */ | ||
for (int i = 3; i <= length; i++){ | ||
dp[i] = ((dp[i - 2] % mod) + (dp[i - 1] % mod)) % mod; | ||
} | ||
return dp[length]; | ||
} | ||
|
||
int main(){ | ||
long long length; | ||
cout << "Enter the length of the floor: " << endl; | ||
cin >> length; | ||
cout << "Number of ways to tile floor of length 2 x " << length << " are: " << numberOfWays(length) << endl; | ||
} | ||
|
||
/* Sample Input: | ||
n=3 | ||
Sample Output: | ||
3 | ||
Expected Time Complexity: | ||
O(n) | ||
Expected Space Complexity: | ||
O(n) | ||
*/ |
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,99 @@ | ||
/* | ||
Given a number. find it's smallest prime factor. | ||
we have to get the smallest factor of that number and | ||
the smallest factor has to be a prime number. | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool check_prime(int number) | ||
{ | ||
/* check whether number is prime or not */ | ||
for(int i = 3; i * i <= number; i += 2) | ||
{ | ||
if(number % i == 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int get_smallest_prime_factor(int number) | ||
{ | ||
int get_factor = 0; | ||
/* | ||
loop runs till sqrt( number ) | ||
for not getting precision errors | ||
use i * i <= number | ||
*/ | ||
for(int i = 3 ; i * i <= number; i += 2) | ||
{ | ||
if(number % i == 0) | ||
{ | ||
get_factor = 1; | ||
int prime = check_prime(i); | ||
if(prime) | ||
{ | ||
/* that means the number is smallest prime factor */ | ||
return i; | ||
} | ||
} | ||
} | ||
if(!get_factor) | ||
{ | ||
/* | ||
that means the number itself it's smallest prime factor. | ||
Ex : 17 | ||
so we can return the number. | ||
*/ | ||
return number; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
cout << "Enter the number : \n"; | ||
int number; | ||
cin >> number; | ||
/* | ||
if the number is even, we can say that the smallest prime factor | ||
is 2 for any even number. | ||
because 2 is prime and it's a factor of | ||
every even number. | ||
*/ | ||
if(! (number & 1) ) | ||
{ | ||
cout << "Smallest prime factor for this number is : " << "2" << endl; | ||
} | ||
else if(number & 1) | ||
{ | ||
int smallest_prime_factor = get_smallest_prime_factor(number); | ||
cout << "Smallest prime factor for this number is : " << endl; | ||
cout << smallest_prime_factor << endl; | ||
} | ||
} | ||
|
||
/* | ||
Standard Input and Output | ||
1. Even number | ||
Enter the number : | ||
435346 | ||
Smallest prime factor for this number is : 2 | ||
2. Odd Number | ||
Enter the number : | ||
35345 | ||
Smallest prime factor for this number is : | ||
5 | ||
3. Odd Prime Number : | ||
Enter the number : | ||
23 | ||
Smallest prime factor for this number is : | ||
23 | ||
Time Complexity : O(sqrt (N) ) | ||
Space Complexity : O( 1 ) | ||
*/ |
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,79 @@ | ||
/*Finding all distinct subsets of a given set using BitMasking Approach | ||
Examples: | ||
Input: S = {1, 2, 2} | ||
Output: {}, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2} | ||
Explanation: | ||
The total subsets of given set are - | ||
{}, {1}, {2}, {2}, {1, 2}, {1, 2}, {2, 2}, {1, 2, 2} | ||
Here {2} and {1, 2} are repeated twice so they are considered only once in the output*/ | ||
|
||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<string> split(const string &s, char ch) | ||
{ | ||
vector<string> elems; | ||
stringstream ss(s); | ||
string item; | ||
while (getline(ss, item, ch)) | ||
elems.push_back(item); | ||
|
||
return elems; | ||
} | ||
|
||
// Function to find all subsets of given set(repeated subset is considered only once) | ||
int printPowerSet(vector<int> arr, int n) | ||
{ | ||
vector<string> list; | ||
|
||
for (int i = 0; i < (int) pow(2, n); i++) | ||
{ | ||
string subset = ""; | ||
for (int j = 0; j < n; j++) | ||
{ | ||
// Check if jth bit in the i is set. If the bit is set, we consider jth element from set | ||
if ((i & (1 << j)) != 0) | ||
subset += to_string(arr[j]) + "|"; | ||
} | ||
|
||
// if subset is encountered for the first time, If we use set<string>, we can directly insert | ||
if (find(list.begin(), list.end(), subset) == list.end()) | ||
list.push_back(subset); | ||
} | ||
|
||
for (string subset : list) | ||
{ | ||
// split the subset and print its elements | ||
vector<string> a = split(subset, '|'); | ||
for (string str: a) | ||
cout << str << " "; | ||
cout << endl; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> arr; | ||
int n = arr.size(); | ||
|
||
printPowerSet(arr, n); | ||
|
||
return 0; | ||
} | ||
|
||
/*Input: 10 12 12 | ||
Output: | ||
10 | ||
12 | ||
10 12 | ||
12 12 | ||
10 12 12 */ | ||
|
||
/*Time complexity | ||
For every index, we make 2 recursion calls and there are n elements so total time complexity is O(2^n). | ||
Space complexity | ||
There are 2^n-1 subsets and for every subset, we need O(n) space on average so total space complexity is O(2^n * n). */ |
Oops, something went wrong.