Skip to content

Commit

Permalink
files structured
Browse files Browse the repository at this point in the history
  • Loading branch information
akshatnema committed Dec 29, 2022
1 parent 6ac94a7 commit cbaebdd
Show file tree
Hide file tree
Showing 63 changed files with 405 additions and 113 deletions.
File renamed without changes.
File renamed without changes.
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.&nbsp;<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.&nbsp;</span></p>

<p><span style="font-size:18px"><strong>A height balanced tree</strong><br>
&nbsp; &nbsp; &nbsp; &nbsp; 1<br>
&nbsp; &nbsp;&nbsp; /&nbsp;&nbsp;&nbsp;&nbsp; \<br>
&nbsp;&nbsp; 10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 39<br>
&nbsp; /<br>
5</span></p>

<p><span style="font-size:18px"><strong>An unbalanced tree</strong><br>
&nbsp; &nbsp; &nbsp; &nbsp; 1<br>
&nbsp; &nbsp;&nbsp; /&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp; 10&nbsp;&nbsp;&nbsp;<br>
&nbsp; /<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1
&nbsp;&nbsp;&nbsp;/
&nbsp;&nbsp; 2
&nbsp; &nbsp;\
&nbsp; &nbsp; 3&nbsp;
<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10
&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp;20&nbsp;&nbsp; 30
&nbsp;&nbsp;/&nbsp;&nbsp; \
40&nbsp;&nbsp; 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 &lt;= Number of nodes &lt;= 10<sup>5</sup><br>
0 &lt;= Data of a node &lt;= 10<sup>6</sup></span></p>

<p><span style="font-size:18px"><strong>Expected time complexity:&nbsp;</strong>O(N)</span><br>
<span style="font-size:18px"><strong>Expected auxiliary space:&nbsp;</strong>O(h) , where h = height of tree</span></p>
<p></p>
</div>
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
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&nbsp;contains three characters i.e <strong>'A', 'B'</strong> and <strong>'#'&nbsp;</strong>only. Check&nbsp;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&nbsp;cross each other<br>
<strong>Note:</strong> Moving i'th character&nbsp;towards Left one step means swap i'th with (i-1)'th charecter [ i-1&gt;=0 ].&nbsp;Moving i'th character&nbsp;towards Right one step means swap i'th with (i+1)'th charecter [ i+1&lt; string's length ].&nbsp;</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:&nbsp;</strong>O(M+N) where M is size of string S and N is size of string T.<br>
<strong>Expected Auxillary Space:&nbsp;</strong>O(1)<br>
&nbsp;</p>

<p><strong>Constraints:</strong><br>
1&lt;=M,N&lt;=100000</p>
</div>
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
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>&nbsp;where each represents a point at coordinate (i, a<sub>i</sub>).&nbsp;<strong>N </strong>vertical lines are drawn such that the two endpoints of line<strong> i </strong>is at&nbsp;(i, a<sub>i</sub>)&nbsp;and (i,0). Find two lines, which together with x-axis forms a container, such that it&nbsp;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.&nbsp;</span></p>

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

<p><span style="font-size:18px"><strong>Constraints:</strong><br>
1&lt;=N&lt;=10<sup>5</sup><br>
1&lt;=A[i]&lt;=100</span></p>
</div>
Loading

0 comments on commit cbaebdd

Please sign in to comment.