Skip to content

Commit 35615d4

Browse files
authored
Merge branch 'smv1999:master' into master
2 parents 4ff7005 + 0a58e7a commit 35615d4

File tree

8 files changed

+544
-110
lines changed

8 files changed

+544
-110
lines changed

2D Arrays/Strassen's_Algo.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Problem Description: Strassens Algorithm in C Language for Matrix Multiplication
2+
#include<stdio.h>
3+
int main(){
4+
int mat1[2][2], mat2[2][2], mat3[2][2], i, j;
5+
int a, b, c, d , e, f, g;
6+
7+
printf("Enter the elements of first matrix:\n"); //Input elements for matrix 1
8+
for(i = 0;i < 2; i++)
9+
{
10+
for(j = 0;j < 2; j++)
11+
{
12+
scanf("%d", &mat1[i][j]);
13+
}
14+
}
15+
16+
printf("Enter the elements of second matrix:\n"); // Input elements for matrix 2
17+
for(i = 0; i < 2; i++)
18+
{
19+
for(j = 0;j < 2; j++)
20+
{
21+
scanf("%d", &mat2[i][j]);
22+
}
23+
}
24+
//Displaying
25+
printf("The Matrix 1:\n"); //Displaying matrix 1 elements
26+
for(i = 0; i < 2; i++)
27+
{
28+
for(j = 0; j < 2; j++)
29+
{
30+
printf("%d\t", mat1[i][j]);
31+
}
32+
printf("\n");
33+
}
34+
35+
printf("The Matrix 2:\n"); //Displayimg matrix 2 elements
36+
for(i = 0;i < 2; i++)
37+
{
38+
for(j = 0;j < 2; j++)
39+
{
40+
printf("%d \t", mat2[i][j]);
41+
}
42+
printf("\n");
43+
}
44+
45+
//reduced eight times Time Complexity to Seven Times i.e T(N) = 7T(N/2) + O(N^2)
46+
47+
// Compexity: before O(n^3) when used Standard Matrix Multiplication , now :O(n^2.808) when used Strassen's Algorithm
48+
49+
//Now we can calculate the product of mat1[i][j] and mat2[i][j] with the following formulas:
50+
a= (mat1[0][0] + mat1[1][1]) * (mat2[0][0] + mat2[1][1]);
51+
b= (mat1[1][0] + mat1[1][1]) * mat2[0][0];
52+
c= mat1[0][0] * (mat2[0][1] - mat2[1][1]);
53+
d= mat1[1][1] * (mat2[1][0] - mat2[0][0]);
54+
e= (mat1[0][0] + mat1[0][1]) * mat2[1][1];
55+
f= (mat1[1][0] - mat1[0][0]) * (mat2[0][0]+mat2[0][1]);
56+
g= (mat1[0][1] - mat1[1][1]) * (mat2[1][0]+mat2[1][1]);
57+
//Now with a,b,c,d,e,f,g which are the submatrices of size N/2*N/2
58+
59+
//Calculate the elements of matrix 3, The resultant matrix mat3[i][j]
60+
mat3[0][0] = a + d- e + g;
61+
mat3[0][1] = c + e;
62+
mat3[1][0] = b + d;
63+
mat3[1][1] = a - b + c + f;
64+
65+
printf("Strassen's algorithm : Matrix Multiplication\n"); //Resultant matrix after applying Strassen's Algo
66+
for(i = 0; i < 2 ; i++)
67+
{
68+
for(j = 0;j < 2; j++)
69+
{
70+
printf("%d\t", mat3[i][j]); //displaying resultant matrix
71+
}
72+
printf("\n");
73+
}
74+
return 0;
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#https://leetcode.com/problems/longest-palindromic-substring/
2+
3+
class Solution(object):
4+
def longestPalindrome(self, s):
5+
"""
6+
:type s: str
7+
:rtype: str
8+
"""
9+
10+
# result to store palindrome
11+
res = ""
12+
13+
for i in range(len(s)):
14+
15+
# for odd len strings
16+
# Eg : "racecar"
17+
tmp = self.helper(i, i, s)
18+
if len(res) < len(tmp):
19+
res = tmp
20+
21+
# for even len strings
22+
# Eg: "aaaabbaa"
23+
tmp = self.helper(i, i + 1, s)
24+
if len(res) < len(tmp):
25+
res = tmp
26+
27+
28+
return res
29+
30+
def helper(self, l, r, s):
31+
# Helper function to check if the left pointer and right pointer are equal
32+
# if inbound and palindrome, move left left and right right
33+
34+
while (l >= 0 and r < len(s) and s[l] == s[r]):
35+
l -= 1
36+
r += 1
37+
38+
39+
return s[l + 1: r]
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//Link to the Problem:
2+
//https://practice.geeksforgeeks.org/problems/merge-two-sorted-arrays-1587115620/1
3+
4+
//Sample Test Case
5+
6+
/*
7+
8+
Input:
9+
n = 4, arr1[] = [1 3 5 7]
10+
m = 5, arr2[] = [0 2 6 8 9]
11+
12+
13+
Output:
14+
arr1[] = [0 1 2 3]
15+
arr2[] = [5 6 7 8 9]
16+
17+
18+
we start from end of first array and start of second array
19+
i.e i points to index 3 of first array and j points to index 0 of second array
20+
21+
i=3
22+
j=0
23+
24+
since 7>0, we swap both of them, now array becomes
25+
26+
arr1[] = [1 3 5 0]
27+
arr2[] = [7 2 6 8 9]
28+
29+
i=2
30+
j=1
31+
32+
since 5>2
33+
34+
arr1[] = [1 3 2 0]
35+
arr2[] = [7 5 6 8 9]
36+
37+
i=1
38+
j=2
39+
40+
3 is not greater than 6, for loop breaks and we come out of the loop
41+
42+
43+
Now we sort the arrays and get the o/p as follows:
44+
45+
arr1[] = [0 1 2 3]
46+
arr2[] = [5 6 7 8 9]
47+
48+
*/
49+
50+
#include <bits/stdc++.h>
51+
using namespace std;
52+
53+
class Solution
54+
{
55+
public:
56+
void merge(int arr1[], int arr2[], int n, int m)
57+
{
58+
59+
// Starting from last index of first array and first index of second arary
60+
int i = n - 1;
61+
int j = 0;
62+
63+
//iterating till the small array is traversed [Note that AND operator is
64+
//used]
65+
while (i >= 0 && j < m)
66+
{
67+
if (arr1[i] > arr2[j])
68+
{
69+
swap(arr1[i], arr2[j]);
70+
i--;
71+
j++;
72+
}
73+
else
74+
{
75+
break;
76+
}
77+
}
78+
79+
//Sorting both the arrays as the order in which element occured would
80+
//have changed
81+
sort(arr1, arr1 + n);
82+
sort(arr2, arr2 + m);
83+
}
84+
};
85+
86+
int main()
87+
{
88+
int t;
89+
cin >> t;
90+
while (t--)
91+
{
92+
93+
//n -> Size of first Array
94+
//m -> Size of Second Array
95+
//i -> Iterator
96+
//arr1 -> First Array
97+
//arr2 ->Second Array
98+
int n, m, i;
99+
cin >> n >> m;
100+
int arr1[n], arr2[m];
101+
for (i = 0; i < n; i++)
102+
{
103+
cin >> arr1[i];
104+
}
105+
for (i = 0; i < m; i++)
106+
{
107+
cin >> arr2[i];
108+
}
109+
110+
//Creating an instance of class
111+
Solution ob;
112+
ob.merge(arr1, arr2, n, m);
113+
for (i = 0; i < n; i++)
114+
{
115+
cout << arr1[i] << " ";
116+
}
117+
for (i = 0; i < m; i++)
118+
{
119+
cout << arr2[i] << " ";
120+
}
121+
cout << "\n";
122+
}
123+
return 0;
124+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Implementation of Next Greater Element problem using stack
3+
4+
Given an array, print the Next Greater Element (NGE) for every element.
5+
The Next greater Element for an element x is the first greater element on
6+
the right side of x in array. Elements for which no greater element exist,
7+
consider next greater element as -1.
8+
9+
For the input array [4, 5, 2, 25}, the next greater elements for each element are as follows.
10+
11+
Element --> NGE
12+
13+
4 --> 5
14+
5 --> 25
15+
2 --> 25
16+
25 --> -1 */
17+
18+
#include <cmath>
19+
#include <cstdio>
20+
#include <vector>
21+
#include <iostream>
22+
#include <algorithm>
23+
#include <stack>
24+
#include <bits/stdc++.h>
25+
using namespace std;
26+
27+
vector<int> NGE(vector<int> v)
28+
{
29+
vector<int> nge(v.size());
30+
stack<int> stk;
31+
for(int i=0;i<v.size(); i++)
32+
{
33+
while(!stk.empty() && v[i] > v[stk.top()])
34+
{
35+
nge[stk.top()]=i;
36+
stk.pop();
37+
}
38+
stk.push(i);
39+
}
40+
while(!stk.empty())
41+
{
42+
nge[stk.top()]=-1;
43+
stk.pop();
44+
}
45+
return nge;
46+
}
47+
48+
int main()
49+
{
50+
int n;
51+
cin >> n;
52+
vector<int> v(n);
53+
54+
for(int i=0;i<n;i++)
55+
cin >> v[i];
56+
57+
vector<int> nge= NGE(v);
58+
59+
for(int i=0; i<n; i++)
60+
cout<< v[i] << " " << (nge[i]== -1 ? -1 : v[nge[i]]) << endl;
61+
62+
return 0;
63+
}
64+
65+
/*
66+
67+
Test case 1:
68+
69+
Sample Input
70+
71+
4
72+
4 5 2 25
73+
74+
Sample Output
75+
76+
4 5
77+
5 25
78+
2 25
79+
25 -1
80+
81+
Test case 2:
82+
83+
Sample Input
84+
85+
4
86+
13 7 6 12
87+
88+
Sample Output
89+
90+
13 -1
91+
7 12
92+
6 12
93+
12 -1
94+
95+
Time Complexity: O(n)
96+
Space Complexity: O(n)
97+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Python code to Check for
2+
# balanced parentheses in an expression
3+
4+
5+
#Function to check parentheses
6+
def validparentheses(s):
7+
open_brace=["{","[","("]
8+
closed_brace=["}","]",")"]
9+
stack=[]
10+
for i in s:
11+
if i in open_brace:
12+
stack.append(i)
13+
elif i in closed_brace:
14+
p=closed_brace.index(i)
15+
if len(stack)>0 and open_brace[p]==stack[len(stack)-1]:
16+
stack.pop()
17+
else:
18+
return False
19+
if(len(stack)==0): #return true if given expression is balanced
20+
return True
21+
else:
22+
return False #return false is not balanced
23+
24+
s=input("Enter the Expression to be evaluated:")
25+
if(validparentheses(s)):
26+
print("Expression is Balanced")
27+
else:
28+
print("Expression is Unbalanced")

0 commit comments

Comments
 (0)