diff --git a/AVL.java b/AVL.java new file mode 100644 index 00000000..b876efe0 --- /dev/null +++ b/AVL.java @@ -0,0 +1,154 @@ +// Java program for insertion in AVL Tree +class Node { + int key, height; + Node left, right; + + Node(int d) { + key = d; + height = 1; + } +} + +class AVLTree { + + Node root; + + // A utility function to get the height of the tree + int height(Node N) { + if (N == null) + return 0; + + return N.height; + } + + // A utility function to get maximum of two integers + int max(int a, int b) { + return (a > b) ? a : b; + } + + // A utility function to right rotate subtree rooted with y + // See the diagram given above. + Node rightRotate(Node y) { + Node x = y.left; + Node T2 = x.right; + + // Perform rotation + x.right = y; + y.left = T2; + + // Update heights + y.height = max(height(y.left), height(y.right)) + 1; + x.height = max(height(x.left), height(x.right)) + 1; + + // Return new root + return x; + } + + // A utility function to left rotate subtree rooted with x + // See the diagram given above. + Node leftRotate(Node x) { + Node y = x.right; + Node T2 = y.left; + + // Perform rotation + y.left = x; + x.right = T2; + + // Update heights + x.height = max(height(x.left), height(x.right)) + 1; + y.height = max(height(y.left), height(y.right)) + 1; + + // Return new root + return y; + } + + // Get Balance factor of node N + int getBalance(Node N) { + if (N == null) + return 0; + + return height(N.left) - height(N.right); + } + + Node insert(Node node, int key) { + + /* 1. Perform the normal BST insertion */ + if (node == null) + return (new Node(key)); + + if (key < node.key) + node.left = insert(node.left, key); + else if (key > node.key) + node.right = insert(node.right, key); + else // Duplicate keys not allowed + return node; + + /* 2. Update height of this ancestor node */ + node.height = 1 + max(height(node.left), + height(node.right)); + + /* 3. Get the balance factor of this ancestor + node to check whether this node became + unbalanced */ + int balance = getBalance(node); + + // If this node becomes unbalanced, then there + // are 4 cases Left Left Case + if (balance > 1 && key < node.left.key) + return rightRotate(node); + + // Right Right Case + if (balance < -1 && key > node.right.key) + return leftRotate(node); + + // Left Right Case + if (balance > 1 && key > node.left.key) { + node.left = leftRotate(node.left); + return rightRotate(node); + } + + // Right Left Case + if (balance < -1 && key < node.right.key) { + node.right = rightRotate(node.right); + return leftRotate(node); + } + + /* return the (unchanged) node pointer */ + return node; + } + + // A utility function to print preorder traversal + // of the tree. + // The function also prints height of every node + void preOrder(Node node) { + if (node != null) { + System.out.print(node.key + " "); + preOrder(node.left); + preOrder(node.right); + } + } + + public static void main(String[] args) { + AVLTree tree = new AVLTree(); + + /* Constructing tree given in the above figure */ + tree.root = tree.insert(tree.root, 10); + tree.root = tree.insert(tree.root, 20); + tree.root = tree.insert(tree.root, 30); + tree.root = tree.insert(tree.root, 40); + tree.root = tree.insert(tree.root, 50); + tree.root = tree.insert(tree.root, 25); + + /* The constructed AVL Tree would be + 30 + / \ + 20 40 + / \ \ + 10 25 50 + */ + System.out.println("Preorder traversal" + + " of constructed tree is : "); + tree.preOrder(tree.root); + } +} +// This code has been contributed by Mayank Jaiswal diff --git a/Convex Hull OPTIMISATION/main.cpp b/ConvexHullOptimisation/main.cpp similarity index 100% rename from Convex Hull OPTIMISATION/main.cpp rename to ConvexHullOptimisation/main.cpp diff --git a/Graph/DFS.cpp b/Graph/DFS.cpp new file mode 100644 index 00000000..d60ceaaf --- /dev/null +++ b/Graph/DFS.cpp @@ -0,0 +1,27 @@ +#include +#define pb push_back +using namespace std; +const int LIM = 50; +int vis[LIM]; +vector >AdjList(LIM); +void dfs(int u){ + cout<>n>>m; + for(int i=0;i>u>>v; + AdjList[u].pb(v); + AdjList[v].pb(u); + } + dfs(1); + +} \ No newline at end of file diff --git a/Maths/EulerGCD.cpp b/Maths/EulerGCD.cpp new file mode 100644 index 00000000..a2921575 --- /dev/null +++ b/Maths/EulerGCD.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +int iterativeGcd(int a,int b){ + while(a!=0){ + int rem = b%a; + b = a; + a = rem; + } + return b; +} +int recursiveGcd(int a, int b){ + if(a==0)return b; + return recursiveGcd(b%a,a); +} +int main(){ + int a = 6,b=4; + cout< +#define ll long long +using namespace std; +ll fastExpo(ll base, ll power){ + if(power==0) + return 1; + ll temp = fastExpo(base,power/2); + if(power&1){ + return temp*temp * base; + } + return temp*temp; +} +ll fastExpoIterative(ll base, ll power){ + ll res = 1; + while(power >0){ + if(power&1){ + res*=base; + } + power/=2; + base*=base; + } + return res; + +} +int main(){ + ll base = 5; + ll power = 3; + cout< +
  • Don't add a problem's solution here, as that's not reusable.
  • +
  • Let's have generic snippets one can refer for learning algo/implementation
  • + diff --git a/Searching/BinarySearch.cpp b/Searching/BinarySearch.cpp new file mode 100644 index 00000000..1593eecb --- /dev/null +++ b/Searching/BinarySearch.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; +//Create a predicate to return T/F for an ind +bool predicate(vector&v, int ind, int search_token){ + return v[ind] >= search_token; +} +int binarySeach(vector&v, int search_token){ + int low = 0,high = v.size(),mid; + while(low&v, int search_token, int begin, int end){ + if(begin==end){ + if(v[begin]==search_token) + return begin; + return -1; + } + int mid = (begin+end)/2; + if(predicate(v,mid,search_token)){ + return recursiveBinarySearch(v,search_token,begin,mid); + } + return recursiveBinarySearch(v,search_token,mid+1,end); +} +int main(){ + vector v = {1,2,4,5,11,20,25}; + cout< arr[j]){ + //swap elements + temp = arr[j-1]; + arr[j-1] = arr[j]; + arr[j] = temp; + } + + } + } + } + public static void main(String[] args) { + int arr[] ={3,60,35,2,45,320,5}; + + System.out.println("Array Before Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + System.out.println(); + + bubbleSort(arr);//sorting array elements using bubble sort + + System.out.println("Array After Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + + } +} diff --git a/Sorting/HeapSort.cpp b/Sorting/HeapSort.cpp new file mode 100644 index 00000000..2d1d46fe --- /dev/null +++ b/Sorting/HeapSort.cpp @@ -0,0 +1,40 @@ +#include +#define sz(v) ((int)v.size()) +using namespace std; +void heapify(vector&v, int SIZE, int ind){ + int left_child = (ind<<1)+1; + int right_child = left_child+1; + int to_swap; + if(left_child v[ind]) + to_swap = left_child; + else + to_swap = ind; + if(right_child v[to_swap]) + to_swap = right_child; + if(to_swap !=ind){ + int temp = v[to_swap]; + v[to_swap] = v[ind]; + v[ind] = temp; + heapify(v,SIZE,to_swap); + } +} +void heapSort(vector&v){ + vectorheap(sz(v)); + for(int i=(sz(v)-2)/2;i>=0;i--){ + heapify(v,sz(v),i); + } + for(int i=sz(v)-1;i>0;i--){ + int temp = v[0]; + v[0] = v[i]; + v[i] = temp; + + heapify(v,i,0); + } +} +int main(){ + vectortemp = {1,3,2,12,444,5}; + heapSort(temp); + for(int v:temp){ + cout< +using namespace std; +void linearSort(vector&v){ + for(int i=0;itemp = {1,3,2,12,444,5}; + linearSort(temp); + for(int v:temp){ + cout< +using namespace std; +void merge(vector&v, int startIndex, int mid, int endIndex){ + vectortemp; + int i1 = startIndex; + int i2 = mid+1; + while(i1<=mid && i2<=endIndex){ + if(v[i1]<=v[i2]) + temp.push_back(v[i1]),i1++; + else + temp.push_back(v[i2]),i2++; + } + while(i1<=mid)temp.push_back(v[i1]),i1++; + while(i2<=endIndex)temp.push_back(v[i2]),i2++; + for(int i=0;i&v, int startIndex, int endIndex){ + if(startIndextemp = {1,3,2,12,444,5}; + mergeSort(temp,0, temp.size()-1); + for(int v:temp){ + cout< +using namespace std; +void kmpPreProcess(string pattern, vector&lps){ + int i = 0,j=-1; + lps[0] = -1; + while(i=0 && pattern[i]!=pattern[j])j = lps[j]; + i++,j++; + lps[i]=j; + } +} +vector kmpSearch(string text, string pattern){ + vector ans; + vector lps(pattern.size()+1); + kmpPreProcess(pattern, lps); + int i = 0, j=-1; + while(i=0 && pattern[j]!=text[i])j=lps[j]; + i++;j++; + if(j==pattern.size()){ + ans.push_back(i-pattern.size()); + j = lps[j]; + } + } + return ans; +} +int main(){ + + string text = "Hey there! this is a text string, within which we wanna search a pattern. \ + Note that pattern repeats itself, so search for pattern if you wanna see can this report multiple occurrences of pattern."; + string pattern = "pattern"; + vector indices = kmpSearch(text,pattern); + for(auto i:indices){ + cout< +using namespace std; +vector linearSearch(string text, string pattern){ + vectorans; + for(int i=0;i indices = linearSearch(text,pattern); + for(auto i:indices){ + cout< +using namespace std; +int main() +{ + char c = 'A'; + cout << "The ASCII value of " << c << " is " << int(c); + return 0; +} diff --git a/mirror inverse_java b/mirror inverse_java new file mode 100644 index 00000000..0f7c3b8c --- /dev/null +++ b/mirror inverse_java @@ -0,0 +1,28 @@ +// Java implementation of the approach +public class GFG { + + // Function that returns true if + // the array is mirror-inverse + static boolean isMirrorInverse(int arr[]) + { + for (int i = 0; i < arr.length; i++) { + + // If condition fails for any element + if (arr[arr[i]] != i) + return false; + } + + // Given array is mirror-inverse + return true; + } + + // Driver code + public static void main(String[] args) + { + int arr[] = { 1, 2, 3, 0 }; + if (isMirrorInverse(arr)) + System.out.println("Yes"); + else + System.out.println("No"); + } +}