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"); + } +}