Skip to content

Commit

Permalink
updated structure
Browse files Browse the repository at this point in the history
Signed-off-by: akshatnema <[email protected]>
  • Loading branch information
akshatnema committed May 29, 2022
1 parent f82fcdf commit b461bb6
Show file tree
Hide file tree
Showing 83 changed files with 313 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions GFG/Product array puzzle - GFG/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Product array puzzle
## Easy
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given an array <strong>nums[]</strong> of size <strong>n</strong>, construct a Product Array <strong>P</strong> (of same size n) such that<strong> P[i]</strong> is equal to the product of all the elements of <strong>nums</strong> except nums[i].</span></p>

<p>&nbsp;</p>

<p><span style="font-size:18px"><strong>Example 1:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:
</strong>n = 5
nums[] = {10, 3, 5, 6, 2}
<strong>Output:
</strong>180 600 360 300 900<strong>
Explanation: </strong>
For i=0, P[i] = 3*5*6*2 = 180.
For i=1, P[i] = 10*5*6*2 = 600.
For i=2, P[i] = 10*3*6*2 = 360.
For i=3, P[i] = 10*3*5*2 = 300.
For i=4, P[i] = 10*3*5*6 = 900.</span>
</pre>

<p><span style="font-size:18px"><strong>Example 2:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:
</strong>n = 2
nums[] = {12,0}
<strong>Output:
</strong>0 12</span>

</pre>

<p><span style="font-size:18px"><strong>Your Task:</strong><br>
You do not have to read input. Your task is to complete the function&nbsp;<strong>productExceptSelf() </strong>that takes array nums[] and n&nbsp;as input parameters and returns a list of n&nbsp;integers denoting the product array P.&nbsp;If the array has only one element the returned list should&nbsp;should contains one value i.e {1}</span><br>
<span style="font-size:18px"><strong>Note:&nbsp;</strong>Try to solve this problem without using the division operation.</span><br>
&nbsp;</p>

<p><span style="font-size:18px"><strong>Expected Time Complexity:&nbsp;</strong>O(n)<br>
<strong>Expected Auxiliary Space:&nbsp;</strong>O(n)</span><br>
&nbsp;</p>

<p><span style="font-size:18px"><strong>Constraints:</strong><br>
1 &lt;= n &lt;= 1000<br>
0 &lt;= nums<sub>i</sub> &lt;= 200</span><br>
<span style="font-size:18px">Array&nbsp;may contain duplicates.</span></p>
<p></p>
</div>
63 changes: 63 additions & 0 deletions GFG/Product array puzzle - GFG/product-array-puzzle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// { Driver Code Starts
//Initial template for C++

#include<bits/stdc++.h>
using namespace std;



// } Driver Code Ends
//User function template for C++

class Solution{
public:
// nums: given vector
// return the Product vector P that hold product except self at each index
vector<long long int> productExceptSelf(vector<long long int>& nums, int n) {
vector<long long int> pre1(n);
vector<long long int> pre2(n);
pre1[0]=nums[0];
for(int i=1;i<n;i++)
pre1[i]=pre1[i-1]*nums[i];
pre2[n-1]=nums[n-1];
for(int i=n-2;i>=0;i--){
pre2[i]=pre2[i+1]*nums[i];
}
vector<long long int> ans;
for(int i=0;i<n;i++){
long long int x=1,y=1;
if(i-1>=0) x=x*pre1[i-1];
if(i+1<n) y=y*pre2[i+1];
ans.push_back(x*y);
}
return ans;
}
};


// { Driver Code Starts.
int main()
{
int t; // number of test cases
cin>>t;
while(t--)
{
int n; // size of the array
cin>>n;
vector<long long int> arr(n),vec(n);

for(int i=0;i<n;i++) // input the array
{
cin>>arr[i];
}
Solution obj;
vec = obj.productExceptSelf(arr,n); // function call

for(int i=0;i<n;i++) // print the output
{
cout << vec[i] << " ";
}
cout<<endl;
}
return 0;
} // } Driver Code Ends
34 changes: 34 additions & 0 deletions GFG/Replace O's with X's - GFG/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Replace O's with X's
## Medium
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given a matrix <strong>mat</strong> of size <strong>N x M</strong> where every element is either ‘O’ or ‘X’.<br>
Replace all ‘O’ with ‘X’ that are surrounded by ‘X’.<br>
A ‘O’ (or a set of ‘O’) is considered to be by surrounded by ‘X’ if there are ‘X’ at locations just below, just above, just left and just right of it.</span></p>

<p><strong><span style="font-size:18px">Example 1:</span></strong></p>

<pre><span style="font-size:18px"><strong>Input:</strong> n = 5, m = 4
mat = {{'X', 'X', 'X', 'X'},
&nbsp; {'X', 'O', 'X', 'X'},
&nbsp; {'X', 'O', 'O', 'X'},
&nbsp; {'X', 'O', 'X', 'X'},
&nbsp; {'X', 'X', 'O', 'O'}}
<strong>Output:</strong> ans = {{'X', 'X', 'X', 'X'},
&nbsp; {'X', 'X', 'X', 'X'},
&nbsp; {'X', 'X', 'X', 'X'},
&nbsp; {'X', 'X', 'X', 'X'},
&nbsp; {'X', 'X', 'O', 'O'}}
<strong>Explanation:</strong> Following the rule the above
matrix is the resultant matrix. </span>
</pre>

<p><span style="font-size:18px"><strong>Your Task:</strong><br>
You do not need to read input or print anything. Your task is to complete the function <strong>fill()</strong> which takes n, m and mat as input parameters ad returns a 2D array representing the resultant matrix.</span></p>

<p><span style="font-size:18px"><strong>Expected Time Complexity:</strong> O(n*m)<br>
<strong>Expected Auxiliary Space:</strong> O(n*m)</span></p>

<p><span style="font-size:18px"><strong>Constraints:</strong><br>
1 ≤ n, m ≤ 500</span></p>
<p></p>
</div>
74 changes: 74 additions & 0 deletions GFG/Replace O's with X's - GFG/replace-os-with-xs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// { Driver Code Starts
// Initial Template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
// User function Template for C++

class Solution{
public:
void change(vector<vector<char>> &mat, int i, int j){
mat[i][j]='*';
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
for(int k=0;k<4;k++){
int l=i+dx[k];
int m=j+dy[k];
if(l>=0 && m>=0 && l<mat.size() && m<mat[0].size() && mat[l][m]=='O')
change(mat,l,m);
}
return ;
}

vector<vector<char>> fill(int n, int m, vector<vector<char>> mat)
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(i==0 || j==0 || i==n-1 || j==m-1){
if(mat[i][j]=='O')
change(mat,i,j);
}
}

for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(mat[i][j]=='O')
mat[i][j]='X';
}

for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(mat[i][j]=='*')
mat[i][j]='O';
}

return mat;
}
};

// { Driver Code Starts.

int main(){
int t;
cin>>t;
while(t--){
int n, m;
cin>>n>>m;
vector<vector<char>> mat(n, vector<char>(m, '.'));
for(int i = 0;i < n;i++)
for(int j=0; j<m; j++)
cin>>mat[i][j];

Solution ob;
vector<vector<char>> ans = ob.fill(n, m, mat);
for(int i = 0;i < n;i++) {
for(int j = 0;j < m;j++) {
cout<<ans[i][j]<<" ";
}
cout<<"\n";
}
}
return 0;
} // } Driver Code Ends
45 changes: 45 additions & 0 deletions GFG/Union of two arrays - GFG/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Union of two arrays
## Easy
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given two arrays <strong>a[]</strong>&nbsp;and <strong>b[]</strong>&nbsp;of size <strong>n</strong>&nbsp;and <strong>m</strong> respectively. The task is to find union between these two arrays. </span></p>

<p><span style="font-size:18px">Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in the union.</span></p>

<p><span style="font-size:18px"><strong>Example 1:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:
</strong>5 3
1 2 3 4 5
1 2 3
<strong>Output:
</strong>5<strong>
Explanation:
</strong>1, 2, 3, 4 and 5 are the
elements which comes in the union set
of both arrays. So count is 5.</span>
</pre>

<p><span style="font-size:18px"><strong>Example 2:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:
</strong>6 2
85 25 1 32 54 6
85 2
<strong>Output:
</strong>7<strong>
Explanation:
</strong>85, 25, 1, 32, 54, 6, and
2 are the elements which comes in the
union set of both arrays. So count is 7.</span></pre>

<p><strong><span style="font-size:18px">Your Task:</span></strong><br>
<span style="font-size:18px">Complete <strong>doUnion </strong>funciton that takes<strong> a, n, b, m as parameters and returns</strong> the count of union elements of the&nbsp;two arrays. The <strong>printing </strong>is done by the <strong>driver </strong>code.</span></p>

<p><span style="font-size:18px"><strong>Constraints:</strong></span><br>
<span style="font-size:18px">1 ≤ n, m&nbsp;≤ 10<sup>5</sup><br>
0 ≤ a[i], b[i] &lt;&nbsp;10<sup>5</sup></span></p>

<p><span style="font-size:18px"><strong>Expected Time Complexity </strong>: O((n+m)log(n+m))<br>
<strong>Expected Auxilliary Space</strong> : O(n+m)</span></p>
<p></p>
</div>
45 changes: 45 additions & 0 deletions GFG/Union of two arrays - GFG/union-of-two-arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// { Driver Code Starts
//Initial template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
//User function template in C++

class Solution{
public:
//Function to return the count of number of elements in union of two arrays.
int doUnion(int a[], int n, int b[], int m) {
set<int> s;
for(int i=0;i<n;i++) s.insert(a[i]);
for(int i=0;i<m;i++) s.insert(b[i]);
return s.size();
}
};

// { Driver Code Starts.

int main() {

int t;
cin >> t;

while(t--){

int n, m;
cin >> n >> m;
int a[n], b[m];

for(int i = 0;i<n;i++)
cin >> a[i];

for(int i = 0;i<m;i++)
cin >> b[i];
Solution ob;
cout << ob.doUnion(a, n, b, m) << endl;

}

return 0;
} // } Driver Code Ends
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# DSA-Practice
# DSA-Practice

This repository contains the solution of the problems done on the Leetcode and the Geeksforgeeks. The sheet solved to practice the problems are as follows:

[Coding Sheet](https://docs.google.com/spreadsheets/d/1NmGtqSNSeLmErfFZF90V77SSf-RdKiIMbufBqfeA4Ms/edit#gid=0)

0 comments on commit b461bb6

Please sign in to comment.