diff --git a/Week1/Srilekha_Vinjamara/10_cpp.cpp b/Week1/Srilekha_Vinjamara/10_cpp.cpp new file mode 100644 index 0000000..5667b48 --- /dev/null +++ b/Week1/Srilekha_Vinjamara/10_cpp.cpp @@ -0,0 +1,29 @@ +/* +c++ program to find GCD of two numbers +Approach: find maximum of the given 2 numbers and find a number in the range 1 to this maximum which will be the GCD +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // num1 = user input1, num2 = user input2 + int num1, num2; + cin >> num1 >> num2; + int maxi = (num1 > num2) ? num1 : num2; + for(int i = maxi; i >= 1; i--){ + if(num1 % i == 0 && num2 % i == 0){ + cout << "GCD of " << num1 << " & " << num2 << " is: " << i; + break; + } + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/10_cpp_output..png b/Week1/Srilekha_Vinjamara/10_cpp_output..png new file mode 100644 index 0000000..75646c6 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/10_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/11_cpp .cpp b/Week1/Srilekha_Vinjamara/11_cpp .cpp new file mode 100644 index 0000000..3a83c16 --- /dev/null +++ b/Week1/Srilekha_Vinjamara/11_cpp .cpp @@ -0,0 +1,38 @@ +/* +c++ program to find all prime numbers till and including given input number +Approach: sieve of erasthosthenes, all multiples of a number will not be prime and are marked false +*/ + +#include +#include +using namespace std; + +// function isPrime is used to find if all prime numbers from 2 to the given range = num +void isPrime(int num){ + // vec is the variable used to store all prime numbers till a number num + vector vec(num, true); + + for(int i = 2; (i*i) < num; i++){ + if(vec[i] == true){ + for(int j = (i*i); j <= num; j += i) + vec[j] = false; + } + } + for(int i = 2; i <= vec.size(); i++){ + if(vec[i]) + cout << i << " "; + } +} + +int main() { + //code + int t; + cin >> t; + while(t--){ + int num; + cin >> num; + isPrime(num); + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/11_cpp_output..png b/Week1/Srilekha_Vinjamara/11_cpp_output..png new file mode 100644 index 0000000..1f6a1a1 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/11_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/12_cpp.cpp b/Week1/Srilekha_Vinjamara/12_cpp.cpp new file mode 100644 index 0000000..42b2fbd --- /dev/null +++ b/Week1/Srilekha_Vinjamara/12_cpp.cpp @@ -0,0 +1,38 @@ +/* +c++ program to find all prime numbers till and NOT including given input number +Approach: sieve of erasthosthenes, all multiples of a number will not be prime and are marked false +*/ + +#include +#include +using namespace std; + +// function isPrime is used to find if all prime numbers from 2 to the given range = num +void isPrime(int num){ + // vec is the variable used to store all prime numbers till and NOT including number num + vector vec(num, true); + + for(int i = 2; (i*i) < num; i++){ + if(vec[i] == true){ + for(int j = (i*i); j <= num; j += i) + vec[j] = false; + } + } + for(int i = 2; i <= vec.size(); i++){ + if(vec[i]) + cout << i << " "; + } +} + +int main() { + //code + int t; + cin >> t; + while(t--){ + int num; + cin >> num; + isPrime(num); + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/12_cpp_output..png b/Week1/Srilekha_Vinjamara/12_cpp_output..png new file mode 100644 index 0000000..100f2eb Binary files /dev/null and b/Week1/Srilekha_Vinjamara/12_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/13_cpp.cpp b/Week1/Srilekha_Vinjamara/13_cpp.cpp new file mode 100644 index 0000000..970571c --- /dev/null +++ b/Week1/Srilekha_Vinjamara/13_cpp.cpp @@ -0,0 +1,40 @@ +/* +c++ program to find a number raised to it's power +Approach: modular exponentiation/fast multiplication of a number with it's power +*/ + +#include +using namespace std; + +// function modExp is used to find modular exponentiation/fast multiplication of a number x to it's power y +long long modExp(long long x, long long y, long long p){ + // ans = modular exponentiation final value i.e. result + long long ans = 1; + + x = x % p; + if(x == 0) + return 0; + while(y > 0){ + if(y & 1) + ans = (ans*x) % p; + x = (x*x) % p; + y = y >> 1; + } + return ans; +} + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // x = base, y = power, p = modulus value + long long x, y, p; + cin >> x >> y >> p; + cout << modExp(x, y, p); + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/13_cpp_output..png b/Week1/Srilekha_Vinjamara/13_cpp_output..png new file mode 100644 index 0000000..d79ec83 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/13_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/14_cpp.cpp b/Week1/Srilekha_Vinjamara/14_cpp.cpp new file mode 100644 index 0000000..5280f1c --- /dev/null +++ b/Week1/Srilekha_Vinjamara/14_cpp.cpp @@ -0,0 +1,30 @@ +/* +c++ program to find a prime number +Approach: if number is divisible by any number other than 1 and itself, flag and mark as prime (till range as square root of input) +*/ + +#include +using namespace std; + +int main() { + //code + + // t = number of test cases + int t; + cin >> t; + while(t--){ + long int num, flag = 0; + cin >> num; + for(int i = 2; (i*i) <= num; i++){ + if(num % i == 0){ + flag = 1; + break; + } + } + if(flag == 0) + cout << "Yes\n"; + else + cout << "No\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/14_cpp_output..png b/Week1/Srilekha_Vinjamara/14_cpp_output..png new file mode 100644 index 0000000..ca73cc2 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/14_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/15_cpp.cpp b/Week1/Srilekha_Vinjamara/15_cpp.cpp new file mode 100644 index 0000000..d798350 --- /dev/null +++ b/Week1/Srilekha_Vinjamara/15_cpp.cpp @@ -0,0 +1,50 @@ +/* +c++ program to find pairs of primes whose product is less than or equal to the given input value +Approach: find all prime numbers, store them in a vector and then for every possible subset pair, if their product <= given input, print the pair +*/ + +#include +#include +using namespace std; + +// function isPrime is used to find if a number n is prime or not +bool isPrime(long int n) +{ + int flag=0; + for(int i=2;(i*i)<=n;i++){ + if(n%i==0){ + flag=1; + break; + } + } + if(flag==0) + return true; + else + return false; +} + +int main() { + //code + int t; + cin >> t; + while(t--){ + // vec is a vector used to stor all prime numbers till given input range number: num + vector vec; + + long long num; + cin >> num; + for(int i = 2; i <= num; i++){ + if(isPrime(i)) + vec.push_back(i); + } + for(int i = 0; i < vec.size(); i++){ + for(int j = 0; j < vec.size(); j++){ + if(vec[i] * vec[j] <= num) + cout << vec[i] << " " + << vec[j] << " "; + } + } + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/15_cpp_output..png b/Week1/Srilekha_Vinjamara/15_cpp_output..png new file mode 100644 index 0000000..388c757 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/15_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/16_cpp.cpp b/Week1/Srilekha_Vinjamara/16_cpp.cpp new file mode 100644 index 0000000..96b98af --- /dev/null +++ b/Week1/Srilekha_Vinjamara/16_cpp.cpp @@ -0,0 +1,26 @@ +/* +c++ program to find if a number is a power of 2 +Approach: using bitwise operations gives smallest worst case time complexity, power of 2 iff the bitwise and(&) of input number num and (num - 1) equal 0. In all other cases, the input number num isn't a power of 2 +*/ + +#include +using namespace std; + +int main() { + //code + long long t; + cin >> t; + while(t--){ + // num = user input number + long long num; + + cin >> num; + if(num == 0) + cout << "NO\n"; + else if((num & (num - 1)) == 0) + cout << "YES\n"; + else + cout << "NO\n"; + else if((num & (num - 1)) == 0) + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/16_cpp_output..png b/Week1/Srilekha_Vinjamara/16_cpp_output..png new file mode 100644 index 0000000..81e7fd0 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/16_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/1_cpp.cpp b/Week1/Srilekha_Vinjamara/1_cpp.cpp new file mode 100644 index 0000000..078d448 --- /dev/null +++ b/Week1/Srilekha_Vinjamara/1_cpp.cpp @@ -0,0 +1,22 @@ +/* +c++ program to print square of a number +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // num is a variable used to take input + float num; + cin >> num; + cout << num*num; + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/1_cpp_output.png b/Week1/Srilekha_Vinjamara/1_cpp_output.png new file mode 100644 index 0000000..712b047 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/1_cpp_output.png differ diff --git a/Week1/Srilekha_Vinjamara/2_cpp.cpp b/Week1/Srilekha_Vinjamara/2_cpp.cpp new file mode 100644 index 0000000..7506f98 --- /dev/null +++ b/Week1/Srilekha_Vinjamara/2_cpp.cpp @@ -0,0 +1,24 @@ +/* +c++ program to print multiplication table of a number +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // num is the number taken as input + int num; + cin >> num; + for(int i = 1; i <= 10; i++){ + cout << num << " x " << i << " = " << num*i << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/2_cpp_output..png b/Week1/Srilekha_Vinjamara/2_cpp_output..png new file mode 100644 index 0000000..75895c7 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/2_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/3a_cpp.cpp b/Week1/Srilekha_Vinjamara/3a_cpp.cpp new file mode 100644 index 0000000..4bd2e41 --- /dev/null +++ b/Week1/Srilekha_Vinjamara/3a_cpp.cpp @@ -0,0 +1,26 @@ +/* +c++ program to print traingle pattern +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + int num; + + // num is the input taken from user + cin >> num; + for(int i = 1; i <= num; i++){ + for(int j = 1; j <= i; j++) + cout << "* "; + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/3a_cpp_output..png b/Week1/Srilekha_Vinjamara/3a_cpp_output..png new file mode 100644 index 0000000..eee404d Binary files /dev/null and b/Week1/Srilekha_Vinjamara/3a_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/3b_cpp.cpp b/Week1/Srilekha_Vinjamara/3b_cpp.cpp new file mode 100644 index 0000000..e4f0425 --- /dev/null +++ b/Week1/Srilekha_Vinjamara/3b_cpp.cpp @@ -0,0 +1,28 @@ +/* +c++ program to print traingle pattern +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + int num; + + // num is the input taken from user + cin >> num; + for(int i = 1; i <= num; i++){ + for(int k = num-1; k >= i; k--) + cout << " "; + for(int j = 1; j <= i; j++) + cout << "* "; + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/3b_cpp_output..png b/Week1/Srilekha_Vinjamara/3b_cpp_output..png new file mode 100644 index 0000000..d602f5e Binary files /dev/null and b/Week1/Srilekha_Vinjamara/3b_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/4_cpp.cpp b/Week1/Srilekha_Vinjamara/4_cpp.cpp new file mode 100644 index 0000000..8eec2eb --- /dev/null +++ b/Week1/Srilekha_Vinjamara/4_cpp.cpp @@ -0,0 +1,26 @@ +/* +c++ program to print traingle pattern +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + int num; + + // num is the input taken from user + cin >> num; + for(int i = 1; i <= num; i++){ + for(int j = num; j >= i; j--) + cout << "* "; + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/4_cpp_output..png b/Week1/Srilekha_Vinjamara/4_cpp_output..png new file mode 100644 index 0000000..98911b5 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/4_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/5_cpp.cpp b/Week1/Srilekha_Vinjamara/5_cpp.cpp new file mode 100644 index 0000000..b71d43b --- /dev/null +++ b/Week1/Srilekha_Vinjamara/5_cpp.cpp @@ -0,0 +1,35 @@ +/* +c++ program to print traingle pattern +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // num = user input + int num; + cin >> num; + for(int i = 1; i <= num; i++){ + for(int k = num-1; k >= i; k--) + cout << " "; + for(int j = 1; j <= i; j++) + cout << "* "; + cout << "\n"; + } + for(int i = 1; i <= num - 1; i++){ + for(int k = 1; k <= i; k++) + cout << " "; + for(int j = num - 1; j >= i; j--) + cout << "* "; + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/5_cpp_output..png b/Week1/Srilekha_Vinjamara/5_cpp_output..png new file mode 100644 index 0000000..262f9d8 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/5_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/6_cpp.cpp b/Week1/Srilekha_Vinjamara/6_cpp.cpp new file mode 100644 index 0000000..4f3ff4e --- /dev/null +++ b/Week1/Srilekha_Vinjamara/6_cpp.cpp @@ -0,0 +1,37 @@ +/* +c++ program to print traingle pattern +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // num = user input + int num; + cin >> num; + int a = 0, b = 1, c; + for(int i = 1; i <= num; i++){ + for(int j = 1; j <= i; j++){ + if(i == 1 && j == 1) + cout << a << " "; + else if(i == 2 && j == 1) + cout << b << " "; + else{ + c = a + b; + cout << c << " "; + a = b; + b = c; + } + } + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/6_cpp_output..png b/Week1/Srilekha_Vinjamara/6_cpp_output..png new file mode 100644 index 0000000..69571fb Binary files /dev/null and b/Week1/Srilekha_Vinjamara/6_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/7_cpp.cpp b/Week1/Srilekha_Vinjamara/7_cpp.cpp new file mode 100644 index 0000000..e3b4f70 --- /dev/null +++ b/Week1/Srilekha_Vinjamara/7_cpp.cpp @@ -0,0 +1,35 @@ +/* +c++ program to print traingle pattern +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // num is the input taken from user + int num; + cin >> num; + int spaces = 2 * (num - 1) - 1; + for(int i = 1; i <= num; i++){ + for(int j = 1; j <= i; j++) + cout << j << " "; + for(int k = spaces; k >= 1; k--) + cout << " "; + for(int m = i; m >= 1; m--){ + if(m == num) + continue; + cout << m << " "; + } + spaces -= 2; + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/7_cpp_output..png b/Week1/Srilekha_Vinjamara/7_cpp_output..png new file mode 100644 index 0000000..d4a9331 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/7_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/8_cpp.cpp b/Week1/Srilekha_Vinjamara/8_cpp.cpp new file mode 100644 index 0000000..e8bc5ff --- /dev/null +++ b/Week1/Srilekha_Vinjamara/8_cpp.cpp @@ -0,0 +1,30 @@ +/* +c++ program to print traingle pattern +*/ + +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // num is the input taken from user + int num; + cin >> num; + + for(int i = 1; i <= num; i++){ + for(int j = 1; j <= num + 1 - i; j++) + cout << j << " "; + int stars = 2 * (i - 2) + 1; + for(int k = 1; k <= stars; k++) + cout << "* "; + cout << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/8_cpp_output..png b/Week1/Srilekha_Vinjamara/8_cpp_output..png new file mode 100644 index 0000000..c1173e5 Binary files /dev/null and b/Week1/Srilekha_Vinjamara/8_cpp_output..png differ diff --git a/Week1/Srilekha_Vinjamara/9_py.py b/Week1/Srilekha_Vinjamara/9_py.py new file mode 100644 index 0000000..3f4a76d --- /dev/null +++ b/Week1/Srilekha_Vinjamara/9_py.py @@ -0,0 +1,10 @@ +#code to print find factorial of a large number + +t=int(input()) +for t in range(0,t) : + # num = user input + num=int(input()) + fact=1 + for i in range(1,num+1_: + fact=fact*i + print(fact) \ No newline at end of file diff --git a/Week1/Srilekha_Vinjamara/9_py_output..png b/Week1/Srilekha_Vinjamara/9_py_output..png new file mode 100644 index 0000000..e0356ed Binary files /dev/null and b/Week1/Srilekha_Vinjamara/9_py_output..png differ diff --git a/Week2/Srilekha_Vinjamara_Week2/beginner_1_cpp.cpp b/Week2/Srilekha_Vinjamara_Week2/beginner_1_cpp.cpp new file mode 100644 index 0000000..59607ce --- /dev/null +++ b/Week2/Srilekha_Vinjamara_Week2/beginner_1_cpp.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + + // str = user input string + string str; + cin >> str; + str = str + "."; + + // str1 = final output string consisting of word order reversed in the sentence given + string str1 = "", str2 = ""; + + for(int i = 0; i < str.length(); i++){ + if(str[i] == '.'){ + str1 = str2 + '.' + str1; + str2 = ""; + } + else{ + str2 += str[i]; + } + } + str1 = str1.substr(0, str1.size() - 1); + cout << str1; + return 0; +} \ No newline at end of file diff --git a/Week2/Srilekha_Vinjamara_Week2/beginner_1_cpp_output.png b/Week2/Srilekha_Vinjamara_Week2/beginner_1_cpp_output.png new file mode 100644 index 0000000..ce0a194 Binary files /dev/null and b/Week2/Srilekha_Vinjamara_Week2/beginner_1_cpp_output.png differ diff --git a/Week2/Srilekha_Vinjamara_Week2/beginner_2_cpp.cpp b/Week2/Srilekha_Vinjamara_Week2/beginner_2_cpp.cpp new file mode 100644 index 0000000..05ad33f --- /dev/null +++ b/Week2/Srilekha_Vinjamara_Week2/beginner_2_cpp.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +// function to find all possible permutations of an input string, using backtracking +void permute(string a, int l, int r){ + if(l == r) + cout << a << " "; + else{ + for(int i = l; i <= r; i++){ + swap(a[l], a[i]); // combinations + permute(a, l + 1, r); + swap(a[l], a[i]); // backtracking + } + } +} + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + string str; + cin >> str; + permute(str, 0, str.length() - 1); + return 0; +} \ No newline at end of file diff --git a/Week2/Srilekha_Vinjamara_Week2/beginner_2_cpp_output.png b/Week2/Srilekha_Vinjamara_Week2/beginner_2_cpp_output.png new file mode 100644 index 0000000..e02d7ce Binary files /dev/null and b/Week2/Srilekha_Vinjamara_Week2/beginner_2_cpp_output.png differ diff --git a/Week2/Srilekha_Vinjamara_Week2/beginner_3_cpp.cpp b/Week2/Srilekha_Vinjamara_Week2/beginner_3_cpp.cpp new file mode 100644 index 0000000..df0b4fb --- /dev/null +++ b/Week2/Srilekha_Vinjamara_Week2/beginner_3_cpp.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + string str1, str2; + cin >> str1 >> str2; + if(str1.length() != str2.length()) + cout << "No\n"; + else{ + // freq1 = frequencies of all chars in str1 + // freq2 = frequencies of all chars in str2 + // freq1 & freq2 are used to find anagram matches + + int freq1[26] = {0}, freq2[26] = {0}; + int flag = 0; + for(int i = 0; i < str1.length(); i++){ + freq1[str1[i] - 'a']++; + freq2[str2[i] - 'a']++; + } + for(int i = 0; i < 26; i++){ + if(freq1[i] != freq2[i]){ + flag = 1; + break; + } + } + if(flag == 1) + cout << "No\n"; + else + cout << "Yes\n"; + } + return 0; +} \ No newline at end of file diff --git a/Week2/Srilekha_Vinjamara_Week2/beginner_3_cpp_output.png b/Week2/Srilekha_Vinjamara_Week2/beginner_3_cpp_output.png new file mode 100644 index 0000000..af41a91 Binary files /dev/null and b/Week2/Srilekha_Vinjamara_Week2/beginner_3_cpp_output.png differ diff --git a/Week2/Srilekha_Vinjamara_Week2/beginner_4_cpp.cpp b/Week2/Srilekha_Vinjamara_Week2/beginner_4_cpp.cpp new file mode 100644 index 0000000..e076693 --- /dev/null +++ b/Week2/Srilekha_Vinjamara_Week2/beginner_4_cpp.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + string str; + getline(cin, str); + + // helps to get frequencies per character to aid duplicate removal + int freq[256] = {0}; + for(int i = 0; i < str.length(); i++){ + freq[int(str[i])]++; + if(freq[int(str[i])] > 1) + continue; + else + cout << str[i]; + } + return 0; +} \ No newline at end of file diff --git a/Week2/Srilekha_Vinjamara_Week2/beginner_4_cpp_output.png b/Week2/Srilekha_Vinjamara_Week2/beginner_4_cpp_output.png new file mode 100644 index 0000000..2a0a02c Binary files /dev/null and b/Week2/Srilekha_Vinjamara_Week2/beginner_4_cpp_output.png differ diff --git a/Week2/Srilekha_Vinjamara_Week2/intermediate_1_cpp.cpp b/Week2/Srilekha_Vinjamara_Week2/intermediate_1_cpp.cpp new file mode 100644 index 0000000..18c04d6 --- /dev/null +++ b/Week2/Srilekha_Vinjamara_Week2/intermediate_1_cpp.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +// function to find longest even length substring with left sum equal to right sum in the substring +void maxLength(string str){ + int n = str.length(), ans = 0; + for(int i = 0; i < n - 1; i++){ + for(int j = i + 1; j < n; j += 2){ + int len = j - i + 1, leftTotal = 0, rightTotal = 0; + for(int k = 0; k < len/2; k++){ + leftTotal += (str[i + k] - '0'); + rightTotal += (str[i + k + len/2] - '0'); + } + if(leftTotal == rightTotal && len > ans) + ans = len; + } + } + cout << ans; +} + +int main() +{ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + ios::sync_with_stdio(0); + cin.tie(0); + string str; + cin >> str; + maxLength(str); + return 0; +} \ No newline at end of file diff --git a/Week2/Srilekha_Vinjamara_Week2/intermediate_1_cpp_output.png b/Week2/Srilekha_Vinjamara_Week2/intermediate_1_cpp_output.png new file mode 100644 index 0000000..05f2a46 Binary files /dev/null and b/Week2/Srilekha_Vinjamara_Week2/intermediate_1_cpp_output.png differ diff --git a/Week2/Srilekha_Vinjamara_Week2/intermediate_2_cpp.cpp b/Week2/Srilekha_Vinjamara_Week2/intermediate_2_cpp.cpp new file mode 100644 index 0000000..eb3b6d9 --- /dev/null +++ b/Week2/Srilekha_Vinjamara_Week2/intermediate_2_cpp.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +// function to compute the sum left in each round of betting game +void bettingGame(string str){ + int bet = 1, sum = 4; + int flag = 0; + + for(int i = 0; i < str.length(); i++){ + if(sum <= 0 || sum < bet){ + flag = 1; + break; + } + if(str[i] == 'W'){ + sum += bet; + bet = 1; + } + else{ + sum -= bet; + bet = bet*2; + } + } + if(flag != 0) + cout << "-1\n"; + else + cout << sum << "\n"; +} + +int main() { + //code + int t; + cin >> t; + while(t--){ + string str; + cin >> str; + bettingGame(str); + } + return 0; +} \ No newline at end of file diff --git a/Week2/Srilekha_Vinjamara_Week2/intermediate_2_cpp_output.png b/Week2/Srilekha_Vinjamara_Week2/intermediate_2_cpp_output.png new file mode 100644 index 0000000..d8f1f6b Binary files /dev/null and b/Week2/Srilekha_Vinjamara_Week2/intermediate_2_cpp_output.png differ diff --git a/Week2/Srilekha_Vinjamara_Week2/intermediate_3_cpp.cpp b/Week2/Srilekha_Vinjamara_Week2/intermediate_3_cpp.cpp new file mode 100644 index 0000000..f652e05 --- /dev/null +++ b/Week2/Srilekha_Vinjamara_Week2/intermediate_3_cpp.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + +// function to print the longest palindromic substring +void printSubStr(char* str, int low, int high) { + for (int i = low; i <= high; ++i) + cout << str[i]; +} + +// function used to compute the longest palindromic substring +void longestPalSubstr(char* str) { + // The length of Longest palindromic substring + int maxLength = 1, start = 0, len = strlen(str), low, high; + + for (int i = 1; i < len; ++i) { + // find the longest even length palindrome with center points as i-1 and i. + low = i - 1; + high = i; + while (low >= 0 && high < len + && str[low] == str[high]) { + if (high - low + 1 > maxLength) { + start = low; + maxLength = high - low + 1; + } + --low; + ++high; + } + + // find the longest odd length palindrome with center point as i + low = i - 1; + high = i + 1; + while (low >= 0 && high < len + && str[low] == str[high]) { + if (high - low + 1 > maxLength) { + start = low; + maxLength = high - low + 1; + } + --low; + ++high; + } + } + printSubStr(str, start, start + maxLength - 1); +} + +int main() +{ + int t; + cin >> t; + while(t--){ + char* str; + cin >> str; + longestPalSubstr(str); + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/Week2/Srilekha_Vinjamara_Week2/intermediate_3_cpp_output.png b/Week2/Srilekha_Vinjamara_Week2/intermediate_3_cpp_output.png new file mode 100644 index 0000000..1f1cfee Binary files /dev/null and b/Week2/Srilekha_Vinjamara_Week2/intermediate_3_cpp_output.png differ diff --git a/Week2/Srilekha_Vinjamara_Week2/intermediate_4_cpp.cpp b/Week2/Srilekha_Vinjamara_Week2/intermediate_4_cpp.cpp new file mode 100644 index 0000000..86977c7 --- /dev/null +++ b/Week2/Srilekha_Vinjamara_Week2/intermediate_4_cpp.cpp @@ -0,0 +1,54 @@ +#include +#include +using namespace std; + +// function to check if any combination of digits with a number having >= 3 digits is divisible by 8 +bool isDivBy8(int x) { + if (x % 8 == 0) { + return true; + } + if (x <= 10) { + return false; + } + if (x < 100) { + return ((x%10)*10 + x/10) % 8 == 0; + } + + // vector v is used to store all digits in a number with more than 2 digits, to find combinations of 3 digits + vector v; + while (x > 0) { + v.push_back(x % 10); + x /= 10; + } + + const int kN = v.size(); + for (int i = 0; i < kN; i++) { + const int k100i = 100*v[i]; + for (int j = 0; j < kN; j++) { + if (j == i) { + continue; + } + const int k10j = 10*v[j]; + for (int k = 0; k < kN; k++) { + if (i == k || j == k || (v[k]&1) != 0) { + continue; + } + if ((k100i + k10j + v[k]) % 8 == 0) { + return true; + } + } + } + } + return false; +} + +int main() { + int t; + scanf("%d", &t); + while (t--) { + int n; + cin >> n; + cout << (isDivBy8(n) ? "Yes\n" : "No\n"); + } + return 0; +} \ No newline at end of file diff --git a/Week2/Srilekha_Vinjamara_Week2/intermediate_4_cpp_output.png b/Week2/Srilekha_Vinjamara_Week2/intermediate_4_cpp_output.png new file mode 100644 index 0000000..eb5253d Binary files /dev/null and b/Week2/Srilekha_Vinjamara_Week2/intermediate_4_cpp_output.png differ