-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ac94a7
commit cbaebdd
Showing
63 changed files
with
405 additions
and
113 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
59 changes: 59 additions & 0 deletions
59
GFG/Leaf at same level - GFG/Check for Balanced Tree - GFG/README.md
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,59 @@ | ||
# Check for Balanced Tree | ||
## Easy | ||
<div class="problem-statement"> | ||
<p></p><p><span style="font-size:18px">Given a binary tree, find if it is height balanced or not. <br> | ||
A tree is height balanced if difference between heights of left and right subtrees is <strong>not more than one</strong> for all nodes of tree. </span></p> | ||
|
||
<p><span style="font-size:18px"><strong>A height balanced tree</strong><br> | ||
1<br> | ||
/ \<br> | ||
10 39<br> | ||
/<br> | ||
5</span></p> | ||
|
||
<p><span style="font-size:18px"><strong>An unbalanced tree</strong><br> | ||
1<br> | ||
/ <br> | ||
10 <br> | ||
/<br> | ||
5</span></p> | ||
|
||
<p><span style="font-size:18px"><strong>Example 1:</strong></span></p> | ||
|
||
<pre><span style="font-size:18px"><strong>Input: | ||
</strong> 1 | ||
/ | ||
2 | ||
\ | ||
3 | ||
<strong>Output: </strong>0<strong> | ||
Explanation: </strong>The max difference in height | ||
of left subtree and right subtree is 2, | ||
which is greater than 1. Hence unbalanced</span> | ||
</pre> | ||
|
||
<p><span style="font-size:18px"><strong>Example 2:</strong></span></p> | ||
|
||
<pre><span style="font-size:18px"><strong>Input: | ||
</strong> 10 | ||
/ \ | ||
20 30 | ||
/ \ | ||
40 60 | ||
<strong>Output:</strong> 1<strong> | ||
Explanation: </strong>The max difference in height | ||
of left subtree and right subtree is 1. | ||
Hence balanced.<strong> </strong></span> | ||
</pre> | ||
|
||
<p><strong><span style="font-size:18px">Your Task:</span></strong><br> | ||
<span style="font-size:18px">You don't need to take input. Just complete the function<strong> isBalanced() </strong>that takes root <strong>node </strong>as parameter and returns <strong>true, </strong>if the tree is balanced else returns <strong>false</strong>.</span></p> | ||
|
||
<p><span style="font-size:18px"><strong>Constraints:</strong><br> | ||
1 <= Number of nodes <= 10<sup>5</sup><br> | ||
0 <= Data of a node <= 10<sup>6</sup></span></p> | ||
|
||
<p><span style="font-size:18px"><strong>Expected time complexity: </strong>O(N)</span><br> | ||
<span style="font-size:18px"><strong>Expected auxiliary space: </strong>O(h) , where h = height of tree</span></p> | ||
<p></p> | ||
</div> |
149 changes: 149 additions & 0 deletions
149
GFG/Leaf at same level - GFG/Check for Balanced Tree - GFG/check-for-balanced-tree.cpp
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,149 @@ | ||
// { Driver Code Starts | ||
//Initial Template for C++ | ||
|
||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
// Tree Node | ||
struct Node { | ||
int data; | ||
Node* left; | ||
Node* right; | ||
}; | ||
|
||
// Utility function to create a new Tree Node | ||
Node* newNode(int val) { | ||
Node* temp = new Node; | ||
temp->data = val; | ||
temp->left = NULL; | ||
temp->right = NULL; | ||
|
||
return temp; | ||
} | ||
|
||
|
||
// Function to Build Tree | ||
Node* buildTree(string str) { | ||
// Corner Case | ||
if (str.length() == 0 || str[0] == 'N') return NULL; | ||
|
||
// Creating vector of strings from input | ||
// string after spliting by space | ||
vector<string> ip; | ||
|
||
istringstream iss(str); | ||
for (string str; iss >> str;) ip.push_back(str); | ||
|
||
// Create the root of the tree | ||
Node* root = newNode(stoi(ip[0])); | ||
|
||
// Push the root to the queue | ||
queue<Node*> queue; | ||
queue.push(root); | ||
|
||
// Starting from the second element | ||
int i = 1; | ||
while (!queue.empty() && i < ip.size()) { | ||
|
||
// Get and remove the front of the queue | ||
Node* currNode = queue.front(); | ||
queue.pop(); | ||
|
||
// Get the current node's value from the string | ||
string currVal = ip[i]; | ||
|
||
// If the left child is not null | ||
if (currVal != "N") { | ||
|
||
// Create the left child for the current node | ||
currNode->left = newNode(stoi(currVal)); | ||
|
||
// Push it to the queue | ||
queue.push(currNode->left); | ||
} | ||
|
||
// For the right child | ||
i++; | ||
if (i >= ip.size()) break; | ||
currVal = ip[i]; | ||
|
||
// If the right child is not null | ||
if (currVal != "N") { | ||
|
||
// Create the right child for the current node | ||
currNode->right = newNode(stoi(currVal)); | ||
|
||
// Push it to the queue | ||
queue.push(currNode->right); | ||
} | ||
i++; | ||
} | ||
|
||
return root; | ||
} | ||
|
||
|
||
// } Driver Code Ends | ||
/* A binary tree node structure | ||
struct Node | ||
{ | ||
int data; | ||
struct Node* left; | ||
struct Node* right; | ||
Node(int x){ | ||
data = x; | ||
left = right = NULL; | ||
} | ||
}; | ||
*/ | ||
|
||
class Solution{ | ||
public: | ||
bool flag=0; | ||
|
||
int height(Node* root){ | ||
if(root==NULL) return 0; | ||
int h1=height(root->left); | ||
int h2=height(root->right); | ||
if(abs(h1-h2)>1){ | ||
flag=true; | ||
} | ||
return (max(h1,h2)+1); | ||
} | ||
|
||
//Function to check whether a binary tree is balanced or not. | ||
bool isBalanced(Node *root) | ||
{ | ||
int h = height(root); | ||
if(flag) return false; | ||
else return true; | ||
} | ||
}; | ||
|
||
|
||
// { Driver Code Starts. | ||
|
||
/* Driver program to test size function*/ | ||
|
||
|
||
|
||
int main() { | ||
|
||
|
||
int t; | ||
scanf("%d ", &t); | ||
while (t--) { | ||
string s, ch; | ||
getline(cin, s); | ||
|
||
Node* root = buildTree(s); | ||
Solution ob; | ||
cout << ob.isBalanced(root) << endl; | ||
} | ||
return 0; | ||
} | ||
// } Driver Code Ends |
52 changes: 52 additions & 0 deletions
52
...sible to convert one string into another with given constraints - GFG/README.md
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,52 @@ | ||
# Check if it is possible to convert one string into another with given constraints | ||
## Easy | ||
<div class="problems_problem_content__Xm_eO"><p>Given two strings S and T, which contains three characters i.e <strong>'A', 'B'</strong> and <strong>'#' </strong>only. Check whether it is possible to convert the first string into another string by performing following operations on string first.<br> | ||
1- A can move towards Left only<br> | ||
2- B can move towards Right only<br> | ||
3- Neither A nor B should cross each other<br> | ||
<strong>Note:</strong> Moving i'th character towards Left one step means swap i'th with (i-1)'th charecter [ i-1>=0 ]. Moving i'th character towards Right one step means swap i'th with (i+1)'th charecter [ i+1< string's length ]. </p> | ||
|
||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> | ||
S=#A#B#B# | ||
T=A###B#B | ||
<strong>Output:</strong> | ||
1 | ||
<strong>Explanation:</strong> | ||
A in S is right to the A in T | ||
so A of S can move easily towards | ||
the left because there is no B on | ||
its left positions and for first B | ||
in S is left to the B in T so B | ||
of T can move easily towards the | ||
right because there is no A on its | ||
right positions and it is same for | ||
next B so S can be easily converted | ||
into T.</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> | ||
S=#A#B# | ||
T=#B#A# | ||
<strong>Output:</strong> | ||
0 | ||
<strong>Explanation:</strong> | ||
Here first A in S is left to the | ||
A in T and according to the condition, | ||
A cant move towards right,so S cant | ||
be converted into T.</pre> | ||
|
||
<p><br> | ||
<strong>Your Task:</strong><br> | ||
You don't need to read input or print anything. Your task is to complete the function <strong>isItPossible() </strong>which takes the two strings S, T and their respective lengths M and N as input parameters and returns 1 if S can be converted into T. Otherwise, it returns 0.</p> | ||
|
||
<p><br> | ||
<strong>Expected Time Complexity: </strong>O(M+N) where M is size of string S and N is size of string T.<br> | ||
<strong>Expected Auxillary Space: </strong>O(1)<br> | ||
</p> | ||
|
||
<p><strong>Constraints:</strong><br> | ||
1<=M,N<=100000</p> | ||
</div> |
63 changes: 63 additions & 0 deletions
63
...GFG/check-if-it-is-possible-to-convert-one-string-into-another-with-given-constraints.cpp
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,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: | ||
int isItPossible(string S, string T, int M, int N) { | ||
int flag=0, ctA=0, ctB=0; | ||
for(int i=0;i<S.length();i++){ | ||
if(T[i]=='A') ctA++; | ||
if(S[i]=='A'){ | ||
if(ctA>0) ctA--; | ||
else return 0; | ||
} | ||
if(S[i]=='B'){ | ||
if(ctA>0) return 0; | ||
ctB++; | ||
} | ||
if(T[i]=='B'){ | ||
if(ctB>0) ctB--; | ||
else return 0; | ||
} | ||
} | ||
ctA=0, ctB=0; | ||
for(int i=S.length()-1;i>=0;i--){ | ||
if(T[i]=='B') ctB++; | ||
if(S[i]=='B'){ | ||
if(ctB>0) ctB--; | ||
else return 0; | ||
} | ||
if(S[i]=='A'){ | ||
if(ctB>0) return 0; | ||
ctA++; | ||
} | ||
if(T[i]=='A'){ | ||
if(ctA>0) ctA--; | ||
else return 0; | ||
} | ||
} | ||
return 1; | ||
} | ||
}; | ||
|
||
//{ Driver Code Starts. | ||
|
||
int main() { | ||
int t; | ||
cin >> t; | ||
while (t--) { | ||
string S, T; | ||
cin >> S >> T; | ||
int M = S.length(), N = T.length(); | ||
Solution ob; | ||
cout << ob.isItPossible(S, T, M, N); | ||
cout << "\n"; | ||
} | ||
} | ||
// } Driver Code Ends |
38 changes: 38 additions & 0 deletions
38
GFG/Leaf at same level - GFG/Container With Most Water - GFG/README.md
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,38 @@ | ||
# Container With Most Water | ||
## Medium | ||
<div class="problems_problem_content__Xm_eO"><p><span style="font-size:18px">Given <strong>N</strong> non-negative integers a<sub>1</sub>,a<sub>2</sub>,....a<sub>n</sub> where each represents a point at coordinate (i, a<sub>i</sub>). <strong>N </strong>vertical lines are drawn such that the two endpoints of line<strong> i </strong>is at (i, a<sub>i</sub>) and (i,0). Find two lines, which together with x-axis forms a container, such that it contains the most water. </span></p> | ||
|
||
<p><span style="font-size:18px">Note : In Case of single verticle line it will not be able to hold water.</span></p> | ||
|
||
<p><span style="font-size:18px"><strong>Example 1:</strong></span></p> | ||
|
||
<pre><span style="font-size:18px"><strong>Input: | ||
</strong>N = 4 | ||
a[] = {1,5,4,3} | ||
<strong>Output: </strong>6<strong> | ||
Explanation: </strong>5 and 3 are distance 2 apart. | ||
So the size of the base = 2. Height of | ||
container = min(5, 3) = 3. So total area | ||
= 3 * 2 = 6.</span></pre> | ||
|
||
<p><span style="font-size:18px"><strong>Example 2:</strong></span></p> | ||
|
||
<pre><span style="font-size:18px"><strong>Input: | ||
</strong>N = 5 | ||
a[] = {3,1,2,4,5} | ||
<strong>Output: </strong>12<strong> | ||
Explanation: </strong>5 and 3 are distance 4 apart. | ||
So the size of the base = 4. Height of | ||
container = min(5, 3) = 3. So total area | ||
= 4 * 3 = 12.</span></pre> | ||
|
||
<p><span style="font-size:18px"><strong>Your Task :</strong><br> | ||
You only need to<strong> </strong>implement the given function<strong> maxArea</strong></span><span style="font-size:18px">. Do not read input, instead use the arguments given in the function and return the desired output. </span></p> | ||
|
||
<p><span style="font-size:18px"><strong>Expected Time Complexity: </strong>O(N).<br> | ||
<strong>Expected Auxiliary Space: </strong>O(1).</span></p> | ||
|
||
<p><span style="font-size:18px"><strong>Constraints:</strong><br> | ||
1<=N<=10<sup>5</sup><br> | ||
1<=A[i]<=100</span></p> | ||
</div> |
Oops, something went wrong.