From b62940a75d4cd7a22fc5f7e94bb6dfb7413954a6 Mon Sep 17 00:00:00 2001 From: Ishika Sinha <69107978+ishikasinha-d@users.noreply.github.com> Date: Mon, 11 Oct 2021 18:29:35 +0530 Subject: [PATCH 1/4] add mountainSubarrayProblem.cpp --- Arrays-algos/mountainSubarrayProblem.cpp | 71 ++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Arrays-algos/mountainSubarrayProblem.cpp diff --git a/Arrays-algos/mountainSubarrayProblem.cpp b/Arrays-algos/mountainSubarrayProblem.cpp new file mode 100644 index 0000000..131f86b --- /dev/null +++ b/Arrays-algos/mountainSubarrayProblem.cpp @@ -0,0 +1,71 @@ +// https://practice.geeksforgeeks.org/problems/mountain-subarray-problem/1 + +#include +using namespace std; + +class Solution{ + public: + vector processQueries(int a[], int n, vector> &queries, + int q) { + + vectorans; + 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=0; i--) + { + if(a[i+1]> tc; + while (tc--) { + int n, i, q; + cin >> n; + int a[n]; + for (i = 0; i < n; i++) { + cin >> a[i]; + } + cin >> q; + vector> 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; +} \ No newline at end of file From c2efa3abb04b5a90ecbff2f842f38ae8221299f5 Mon Sep 17 00:00:00 2001 From: Ishika Sinha <69107978+ishikasinha-d@users.noreply.github.com> Date: Mon, 11 Oct 2021 18:42:51 +0530 Subject: [PATCH 2/4] add mirrorTree.cpp --- Binary Tree/mirrorTree.cpp | 123 +++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 Binary Tree/mirrorTree.cpp diff --git a/Binary Tree/mirrorTree.cpp b/Binary Tree/mirrorTree.cpp new file mode 100644 index 0000000..67f93d6 --- /dev/null +++ b/Binary Tree/mirrorTree.cpp @@ -0,0 +1,123 @@ +// https://practice.geeksforgeeks.org/problems/mirror-tree/1/?company[]=Google&company[]=Google&page=1&query=company[]Googlepage1company[]Google + +#include +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 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 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; +} \ No newline at end of file From 5a027feca60888de6a23b019dd66449f9feb3d3d Mon Sep 17 00:00:00 2001 From: Ishika Sinha <69107978+ishikasinha-d@users.noreply.github.com> Date: Mon, 11 Oct 2021 18:45:27 +0530 Subject: [PATCH 3/4] Delete mirrorTree.cpp --- Binary Tree/mirrorTree.cpp | 123 ------------------------------------- 1 file changed, 123 deletions(-) delete mode 100644 Binary Tree/mirrorTree.cpp diff --git a/Binary Tree/mirrorTree.cpp b/Binary Tree/mirrorTree.cpp deleted file mode 100644 index 67f93d6..0000000 --- a/Binary Tree/mirrorTree.cpp +++ /dev/null @@ -1,123 +0,0 @@ -// https://practice.geeksforgeeks.org/problems/mirror-tree/1/?company[]=Google&company[]=Google&page=1&query=company[]Googlepage1company[]Google - -#include -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 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 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; -} \ No newline at end of file From 0d721ecb73e874822ca11b57b33fa617af37ae5e Mon Sep 17 00:00:00 2001 From: Ishika Sinha <69107978+ishikasinha-d@users.noreply.github.com> Date: Mon, 11 Oct 2021 18:59:58 +0530 Subject: [PATCH 4/4] Create sortByFrequency.cpp --- Hashing/sortByFrequency.cpp | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Hashing/sortByFrequency.cpp diff --git a/Hashing/sortByFrequency.cpp b/Hashing/sortByFrequency.cpp new file mode 100644 index 0000000..2906bdb --- /dev/null +++ b/Hashing/sortByFrequency.cpp @@ -0,0 +1,77 @@ +//https://practice.geeksforgeeks.org/problems/sorting-elements-of-an-array-by-frequency-1587115621/1/ +// Sorting Elements of an Array by Frequency + +#include +using namespace std; + +class Solution{ + public: + + static bool cmp( pair &a, pair &b) + { + if(a.second==b.second) + return a.firstb.second; + } + + vector sortByFreq(int arr[],int n) + { + //Your code here + vectorans; + map m; + vector > A; + + for(int i=0; i> t; + + + while(t--){ + + + int n; + cin >> n; + + int a[n+1]; + + for(int i = 0;i> a[i]; + } + Solution obj; + vector v; + v = obj.sortByFreq(a,n); + for(int i:v) + cout<