Skip to content

Commit

Permalink
Merge pull request #187 from TesseractCoding/master
Browse files Browse the repository at this point in the history
mpr
  • Loading branch information
JayantGoel001 authored Mar 25, 2021
2 parents 4708806 + ad50076 commit 4bdcb8c
Show file tree
Hide file tree
Showing 14 changed files with 759 additions and 5 deletions.
72 changes: 72 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,78 @@
"contributions": [
"code"
]
},
{
"login": "weirdrag08",
"name": "Aditya Anand",
"avatar_url": "https://avatars.githubusercontent.com/u/60457205?v=4",
"profile": "https://mywebsite0408.000webhostapp.com/",
"contributions": [
"code"
]
},
{
"login": "ankan1811",
"name": "Ankan Pal",
"avatar_url": "https://avatars.githubusercontent.com/u/65054543?v=4",
"profile": "https://www.linkedin.com/in/ankanpal/",
"contributions": [
"code"
]
},
{
"login": "TidbitsJS",
"name": "Sujata Gunale",
"avatar_url": "https://avatars.githubusercontent.com/u/67959015?v=4",
"profile": "https://github.com/TidbitsJS",
"contributions": [
"code"
]
},
{
"login": "tanvi355",
"name": "tanvi355",
"avatar_url": "https://avatars.githubusercontent.com/u/56465105?v=4",
"profile": "https://github.com/tanvi355",
"contributions": [
"code"
]
},
{
"login": "nandinib1999",
"name": "Nandini Bansal",
"avatar_url": "https://avatars.githubusercontent.com/u/41077745?v=4",
"profile": "https://github.com/nandinib1999",
"contributions": [
"code"
]
},
{
"login": "RaiyanMahin",
"name": "Raiyan Bashir Mahin",
"avatar_url": "https://avatars.githubusercontent.com/u/53217558?v=4",
"profile": "https://rmahincuetcse523.wixsite.com/my-site-7",
"contributions": [
"code"
]
},
{
"login": "Harikrishnan6336",
"name": "Hari Krishnan U",
"avatar_url": "https://avatars.githubusercontent.com/u/53964426?v=4",
"profile": "https://github.com/Harikrishnan6336",
"contributions": [
"code"
]
},
{
"login": "gayatri517",
"name": "E S Gayatri",
"avatar_url": "https://avatars.githubusercontent.com/u/70585276?v=4",
"profile": "https://github.com/gayatri517",
"contributions": [
"code"
]
}
],
"repoType": "github",
Expand Down
6 changes: 4 additions & 2 deletions C-Plus-Plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [Check Anagrams](cp/check_anagrams.cpp)
- [Check Pangram](cp/check_pangram.cpp)
- [Check for Subsequence](cp/Check_for_Subsequence.cpp)
- [Length of last word](cp/length_of_last_word.cpp)
- [Container with most water](cp/Container_with_most_water.cpp)
- [Contiguous Sub Array with Given Sum](cp/SubArrayWithGivenSum.cpp)
- [Count of string P in string S](cp/CountofPinS.cpp)
Expand Down Expand Up @@ -218,8 +219,6 @@

## Stack based problems

-[Infix to Postfix Conversiion](../C/stack/Infix_to_Postfix.c)

- [Check for balanced parenthesis](stack/Check_for_balanced_parenthesis.cpp)
- [Evaluation of Postfix Expression](stack/evaluate_postfix.cpp)
- [Largest rectangular area under histogram](stack/Largest_rect_area_under_histogram.cpp)
Expand Down Expand Up @@ -257,6 +256,7 @@ _add list here_
- [Double factorial](math/double_factorial.cpp)
- [Euler's_Totient_Function](math/Euler's_Totient_function.cpp)
- [Extended Euclidean Algorithm](math/Extended_Euclidean_Algorithm.cpp)
- [Find Smallest Prime Factor](math/smallest_prime_factor.cpp)
- [Hamming Distance](math/Hamming_Distance.cpp)
- [Number is Power of 2](math/Check_whether_a_number_is_power_of_2.cpp)
- [Palindrome Number](math/check_palindrome.cpp)
Expand Down Expand Up @@ -309,6 +309,7 @@ _add list here_
- [Minimum number of insertions and deletions](dp/min_ins_del.cpp)
- [Unbounded Knapsack](dp/unbounded_knapsack.cpp)
- [Print Longest Common Subsequence](dp/Print_Longest_Common_Subsequence.cpp)
- [Tiling 2 x N](dp/Tiling_2xN.cpp)
- [Kadane's Algorithm](dp/Kadane_Algorithm.cpp)
- [Shortest_Uncommon_Subseqence](dp/shortest_uncommon_subseq.cpp)

Expand Down Expand Up @@ -354,6 +355,7 @@ _add list here_
- [Count of distinct elements in a window](cp/DistinctElementsinaWindow.cpp)
- [Dynamic Huffman](other/DynamicHuffman.cpp)
- [Divisors of a natural number](other/divisors_of_natural_number.cpp)
- [Distinct Subsets of a Set](other/distinct_subsets_of_set.cpp)
- [Fast Fibonacci Last digit](other/Fast_fibonacci_last_digit.cpp)
- [Find duplicate elements from an array](other/find_duplicate_elements_from_an_array.cpp)
- [Find the Numbers](other/find_the_numbers.cpp)
Expand Down
49 changes: 49 additions & 0 deletions C-Plus-Plus/cp/length_of_last_word.cpp
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)
*/
47 changes: 47 additions & 0 deletions C-Plus-Plus/dp/Tiling_2xN.cpp
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)
*/
99 changes: 99 additions & 0 deletions C-Plus-Plus/math/smallest_prime_factor.cpp
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 )
*/
79 changes: 79 additions & 0 deletions C-Plus-Plus/other/distinct_subsets_of_set.cpp
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). */
Loading

0 comments on commit 4bdcb8c

Please sign in to comment.