Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Arrays-algos/mountainSubarrayProblem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// https://practice.geeksforgeeks.org/problems/mountain-subarray-problem/1

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

class Solution{
public:
vector<bool> processQueries(int a[], int n, vector<pair<int, int>> &queries,
int q) {

vector<bool>ans;
int left[n], right[n];
left[0]=0;
right[n-1]=n-1;
int k=0;

// left[i] stores the last index on left side which is increasing
// i.e. greater than its previous element
for(int i=1; i<n; i++)
{
if(a[i-1]<a[i])
k=i;
left[i]=k;
}
// right[i] will store the first index on the right side which is decreasing
// i.e. greater than its next element.
for(int i=n-2; i>=0; i--)
{
if(a[i+1]<a[i])
k=i;
right[i]=k;

}

for(auto x: queries)
{
int l= x.first;
int r= x.second;

if(left[r]<=right[l])
ans.push_back(true);
else
ans.push_back(false);
}
return ans;
}
};

int main() {
int tc;
cin >> tc;
while (tc--) {
int n, i, q;
cin >> n;
int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
cin >> q;
vector<pair<int, int>> queries(q);
for (i = 0; i < q; i++) {
cin >> queries[i].first >> queries[i].second;
}
Solution obj;
auto v = obj.processQueries(a, n, queries, q);
for (bool u : v) {
cout << (u ? "Yes\n" : "No\n");
}
}
return 0;
}
123 changes: 123 additions & 0 deletions Binary Tree/mirrorTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// https://practice.geeksforgeeks.org/problems/mirror-tree/1/?company[]=Google&company[]=Google&page=1&query=company[]Googlepage1company[]Google

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

// A binary tree node has data, pointer to left child and a pointer to right child

struct Node {
int data;
struct Node *left;
struct Node *right;

Node(int x) {
data = x;
left = right = NULL;
}
};

// 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 = new Node(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 = new Node(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 = new Node(stoi(currVal));

// Push it to the queue
queue.push(currNode->right);
}
i++;
}

return root;
}

/* Helper function to test mirror(). Given a binary
search tree, print out its data elements in
increasing sorted order.*/
void inOrder(struct Node *node) {
if (node == NULL) return;

inOrder(node->left);
printf("%d ", node->data);

inOrder(node->right);
}

class Solution {
public:

// Function to convert a binary tree into its mirror tree.
void mirror(Node* node) {
// code here
if(node==NULL) return;

mirror(node->left);
mirror(node->right);

//swapping pointers
Node* t= node->left;
node->left= node->right;
node->right= t;
}
};

int main() {
int tc;
scanf("%d ", &tc);
while (tc--) {
string str;
getline(cin, str);
Node *root = buildTree(str);
Solution ob;
ob.mirror(root);
inOrder(root);
cout << "\n";
}

return 0;
}