From b3a1b6a5ff51fc54c680b44562161db1948fbc1c Mon Sep 17 00:00:00 2001 From: JuniorS6 <92072877+JuniorS6@users.noreply.github.com> Date: Wed, 20 Oct 2021 18:05:57 +0530 Subject: [PATCH 01/20] Update README.md --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 949a3ce..503bff6 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,18 @@ # AlgorithmBank Help us make a deposit of algorithms. -#Rules to Contribute- - +# Rules to Contribute -Check the issues section for new issues.
-Add alogorithms under Algorithm Name/Algorithm File.
-You can contribute in any language.
-You can also raise issues to verify with us.
-You can edit existing algorithms,if it is not right, But please raise an issue if you are doing this to verify with us. - +# Contributors + + + + If you need the PR for HacktoberFest 2021, please comment it and let us know. From 7fd2aa1fa5d4ae562470f13127c4e7c91e1c72ea Mon Sep 17 00:00:00 2001 From: Tejugowda1005 Date: Wed, 20 Oct 2021 23:55:03 +0530 Subject: [PATCH 02/20] Added fibonacci series cpp --- Fibonacci series/fibonacci.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Fibonacci series/fibonacci.cpp diff --git a/Fibonacci series/fibonacci.cpp b/Fibonacci series/fibonacci.cpp new file mode 100644 index 0000000..014ee91 --- /dev/null +++ b/Fibonacci series/fibonacci.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; +int main() { + int n1=0,n2=1,n3,i,number; + cout<<"Enter the number of elements: "; + cin>>number; + cout< Date: Thu, 21 Oct 2021 14:48:10 +0530 Subject: [PATCH 03/20] update readme readme updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 503bff6..c92c462 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Help us make a deposit of algorithms. -Add alogorithms under Algorithm Name/Algorithm File.
-You can contribute in any language.
-You can also raise issues to verify with us.
--You can edit existing algorithms,if it is not right, But please raise an issue if you are doing this to verify with us. +-You can edit existing algorithms,if it is not right. # Contributors From 39e426476e2376cce059fa8ebf95464cab3850fc Mon Sep 17 00:00:00 2001 From: Chayanin Kongsareekul <57552210+Aphrodicez@users.noreply.github.com> Date: Thu, 21 Oct 2021 18:22:42 +0700 Subject: [PATCH 04/20] Create Binary_Indexed_Tree.cpp --- .../Binary_Indexed_Tree.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Data Structure/Binary Indexed Tree ( Fenwick Tree )/Binary_Indexed_Tree.cpp diff --git a/Data Structure/Binary Indexed Tree ( Fenwick Tree )/Binary_Indexed_Tree.cpp b/Data Structure/Binary Indexed Tree ( Fenwick Tree )/Binary_Indexed_Tree.cpp new file mode 100644 index 0000000..1069d4c --- /dev/null +++ b/Data Structure/Binary Indexed Tree ( Fenwick Tree )/Binary_Indexed_Tree.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +#define Least_Significant_Bit(i) (i) & -(i) + +vector Binary_Indexed_Tree; + +int TREE_SIZE = 1e5 + 10; + +void update(int index, int value) { + while(index <= TREE_SIZE) { + Binary_Indexed_Tree[index] += value; + index += Least_Significant_Bit(index); + } +} + +int query(int index) { + int sum = 0; + while(index > 0) { + sum += Binary_Indexed_Tree[index]; + index -= Least_Significant_Bit(index); + } + return sum; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + Binary_Indexed_Tree.assign(TREE_SIZE, 0); + int n; + cin >> n; + for(int i = 1; i <= n; i++) { + string operation; + cin >> operation; + if(operation == "U") { + /// Update + int index, value; + cin >> index >> value; + update(index, value); + } + if(operation == "Q") { + /// Query + int index; + cin >> index; + cout << query(index) << "\n"; + } + } + return 0; +} From ab24b1ec4c4dd8b5d27fe8d79abaf671c8d4b966 Mon Sep 17 00:00:00 2001 From: Sneha438 Date: Thu, 21 Oct 2021 22:25:22 +0530 Subject: [PATCH 05/20] Added Radix sort cpp --- Radix sort/Radix_sort.cpp.txt | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Radix sort/Radix_sort.cpp.txt diff --git a/Radix sort/Radix_sort.cpp.txt b/Radix sort/Radix_sort.cpp.txt new file mode 100644 index 0000000..93ebc60 --- /dev/null +++ b/Radix sort/Radix_sort.cpp.txt @@ -0,0 +1,47 @@ +#include +#include +#include +using namespace std; +void display(int *array, int size) { + for(int i = 0; i pocket[10]; + for(i = 0; i< max; i++) { + m = pow(10, i+1); + p = pow(10, i); + for(j = 0; j> n; + cout << "Enter the maximum digit of elements: "; + cin >> max; + int arr[n]; + cout << "Enter elements:" << endl; + for(int i = 0; i> arr[i]; + } + cout << "Data before Sorting: "; + display(arr, n); + radixSort(arr, n, max); + cout << "Data after Sorting: "; + display(arr, n); +} \ No newline at end of file From 612bc46067d73b1dc96845cabd5af08189a988dc Mon Sep 17 00:00:00 2001 From: Caleb Felix Date: Fri, 22 Oct 2021 02:10:11 +0530 Subject: [PATCH 06/20] Create dijkstra.js --- Dijkstra's Algorithm/dijkstra.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Dijkstra's Algorithm/dijkstra.js diff --git a/Dijkstra's Algorithm/dijkstra.js b/Dijkstra's Algorithm/dijkstra.js new file mode 100644 index 0000000..9bb6e98 --- /dev/null +++ b/Dijkstra's Algorithm/dijkstra.js @@ -0,0 +1,30 @@ +djikstraAlgorithm(startNode) { + let distances = {}; + + // Stores the reference to previous nodes + let prev = {}; + let pq = new PriorityQueue(this.nodes.length * this.nodes.length); + + // Set distances to all nodes to be infinite except startNode + distances[startNode] = 0; + pq.enqueue(startNode, 0); + this.nodes.forEach(node => { + if (node !== startNode) distances[node] = Infinity; + prev[node] = null; + }); + + while (!pq.isEmpty()) { + let minNode = pq.dequeue(); + let currNode = minNode.data; + let weight = minNode.priority; + this.edges[currNode].forEach(neighbor => { + let alt = distances[currNode] + neighbor.weight; + if (alt < distances[neighbor.node]) { + distances[neighbor.node] = alt; + prev[neighbor.node] = currNode; + pq.enqueue(neighbor.node, distances[neighbor.node]); + } + }); + } + return distances; +} From 6208756768c7957a02a7f6842ac1c71cb2ebe14e Mon Sep 17 00:00:00 2001 From: BugHunters-bug <91728280+BugHunters-bug@users.noreply.github.com> Date: Sun, 24 Oct 2021 17:59:28 +0530 Subject: [PATCH 07/20] renamed file --- Radix sort/{Radix_sort.cpp.txt => Radix_sort.cpp} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Radix sort/{Radix_sort.cpp.txt => Radix_sort.cpp} (99%) diff --git a/Radix sort/Radix_sort.cpp.txt b/Radix sort/Radix_sort.cpp similarity index 99% rename from Radix sort/Radix_sort.cpp.txt rename to Radix sort/Radix_sort.cpp index 93ebc60..927cf97 100644 --- a/Radix sort/Radix_sort.cpp.txt +++ b/Radix sort/Radix_sort.cpp @@ -44,4 +44,4 @@ int main() { radixSort(arr, n, max); cout << "Data after Sorting: "; display(arr, n); -} \ No newline at end of file +} From 433eed065eb5134ee105787a3c54d2f044263f13 Mon Sep 17 00:00:00 2001 From: Bhoomika182 Date: Sun, 24 Oct 2021 18:23:37 +0530 Subject: [PATCH 08/20] added 0-1 knapsack.cpp --- 0-1 knapsack.cpp/0-1 knapsack.cpp.txt | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0-1 knapsack.cpp/0-1 knapsack.cpp.txt diff --git a/0-1 knapsack.cpp/0-1 knapsack.cpp.txt b/0-1 knapsack.cpp/0-1 knapsack.cpp.txt new file mode 100644 index 0000000..cd8b16e --- /dev/null +++ b/0-1 knapsack.cpp/0-1 knapsack.cpp.txt @@ -0,0 +1,46 @@ +#include +using namespace std; + +// A utility function that returns +// maximum of two integers +int max(int a, int b) { return (a > b) ? a : b; } + +// Returns the maximum value that +// can be put in a knapsack of capacity W +int knapSack(int W, int wt[], int val[], int n) +{ + + // Base Case + if (n == 0 || W == 0) + return 0; + + // If weight of the nth item is more + // than Knapsack capacity W, then + // this item cannot be included + // in the optimal solution + if (wt[n - 1] > W) + return knapSack(W, wt, val, n - 1); + + // Return the maximum of two cases: + // (1) nth item included + // (2) not included + else + return max( + val[n - 1] + + knapSack(W - wt[n - 1], + wt, val, n - 1), + knapSack(W, wt, val, n - 1)); +} + +// Driver code +int main() +{ + int val[] = { 60, 100, 120 }; + int wt[] = { 10, 20, 30 }; + int W = 50; + int n = sizeof(val) / sizeof(val[0]); + cout << knapSack(W, wt, val, n); + return 0; +} + +// This \ No newline at end of file From 5153f4840d1d3d17bac09815812575017233072b Mon Sep 17 00:00:00 2001 From: Mohit <58560983+mohit-003@users.noreply.github.com> Date: Sun, 24 Oct 2021 18:34:49 +0530 Subject: [PATCH 09/20] create linearsearch.js linear search in java script. --- Linear Search/linearsearch.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Linear Search/linearsearch.js diff --git a/Linear Search/linearsearch.js b/Linear Search/linearsearch.js new file mode 100644 index 0000000..5252e70 --- /dev/null +++ b/Linear Search/linearsearch.js @@ -0,0 +1,8 @@ +function linearSearch(arr, key){ + for(let i = 0; i < arr.length; i++){ + if(arr[i] === key){ + return i + } + } + return -1 +} From fa2194a415a34636ffbc1fa8e93b6010786a274b Mon Sep 17 00:00:00 2001 From: Mohit <58560983+mohit-003@users.noreply.github.com> Date: Sun, 24 Oct 2021 18:37:44 +0530 Subject: [PATCH 10/20] create binarysearch.js Binary search in java script --- BinarySearch/binarysearch.js | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 BinarySearch/binarysearch.js diff --git a/BinarySearch/binarysearch.js b/BinarySearch/binarysearch.js new file mode 100644 index 0000000..c2796fb --- /dev/null +++ b/BinarySearch/binarysearch.js @@ -0,0 +1,37 @@ + From 561f0e756d73b6d8d1cabe9af939ec261f8462fb Mon Sep 17 00:00:00 2001 From: Mohit <58560983+mohit-003@users.noreply.github.com> Date: Sun, 24 Oct 2021 18:40:10 +0530 Subject: [PATCH 11/20] create fibonacci.js Fibonacci series in Java-Script --- Fibonacci series/fibonacci.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Fibonacci series/fibonacci.js diff --git a/Fibonacci series/fibonacci.js b/Fibonacci series/fibonacci.js new file mode 100644 index 0000000..00dbeac --- /dev/null +++ b/Fibonacci series/fibonacci.js @@ -0,0 +1,14 @@ + From 8300e298dd48a91a4dae1a636aed0de864287ca4 Mon Sep 17 00:00:00 2001 From: Mohit <58560983+mohit-003@users.noreply.github.com> Date: Sun, 24 Oct 2021 18:43:51 +0530 Subject: [PATCH 12/20] create armstron_number.js Armstrong number in java script --- Armstrong Number/armstrong_number.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Armstrong Number/armstrong_number.js diff --git a/Armstrong Number/armstrong_number.js b/Armstrong Number/armstrong_number.js new file mode 100644 index 0000000..bfdbd18 --- /dev/null +++ b/Armstrong Number/armstrong_number.js @@ -0,0 +1,23 @@ +// program to check an Armstrong number of three digits + +let sum = 0; +const number = prompt('Enter a three-digit positive integer: '); + +// create a temporary variable +let temp = number; +while (temp > 0) { + // finding the one's digit + let remainder = temp % 10; + + sum += remainder * remainder * remainder; + + // removing last digit from the number + temp = parseInt(temp / 10); // convert float into integer +} +// check the condition +if (sum == number) { + console.log(`${number} is an Armstrong number`); +} +else { + console.log(`${number} is not an Armstrong number.`); +} From c7988984a88e3e36c4387ff4355bd33d259ac36e Mon Sep 17 00:00:00 2001 From: BugHunters-bug <91728280+BugHunters-bug@users.noreply.github.com> Date: Sun, 24 Oct 2021 18:47:33 +0530 Subject: [PATCH 13/20] renamed file --- 0-1 knapsack.cpp/{0-1 knapsack.cpp.txt => 0-1 knapsack.cpp} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename 0-1 knapsack.cpp/{0-1 knapsack.cpp.txt => 0-1 knapsack.cpp} (99%) diff --git a/0-1 knapsack.cpp/0-1 knapsack.cpp.txt b/0-1 knapsack.cpp/0-1 knapsack.cpp similarity index 99% rename from 0-1 knapsack.cpp/0-1 knapsack.cpp.txt rename to 0-1 knapsack.cpp/0-1 knapsack.cpp index cd8b16e..12459c2 100644 --- a/0-1 knapsack.cpp/0-1 knapsack.cpp.txt +++ b/0-1 knapsack.cpp/0-1 knapsack.cpp @@ -43,4 +43,4 @@ int main() return 0; } -// This \ No newline at end of file +// This From d870f995fe90f00bf564d79d9bed859bd28b470d Mon Sep 17 00:00:00 2001 From: PrabhaKumar Date: Sun, 24 Oct 2021 21:57:38 +0530 Subject: [PATCH 14/20] added hufman code --- hufman code/New Text Document.txt | 0 hufman code/hufman_code.c | 181 ++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 hufman code/New Text Document.txt create mode 100644 hufman code/hufman_code.c diff --git a/hufman code/New Text Document.txt b/hufman code/New Text Document.txt new file mode 100644 index 0000000..e69de29 diff --git a/hufman code/hufman_code.c b/hufman code/hufman_code.c new file mode 100644 index 0000000..1972c04 --- /dev/null +++ b/hufman code/hufman_code.c @@ -0,0 +1,181 @@ +// Huffman Coding in C + +#include +#include + +#define MAX_TREE_HT 50 + +struct MinHNode { + char item; + unsigned freq; + struct MinHNode *left, *right; +}; + +struct MinHeap { + unsigned size; + unsigned capacity; + struct MinHNode **array; +}; + +// Create nodes +struct MinHNode *newNode(char item, unsigned freq) { + struct MinHNode *temp = (struct MinHNode *)malloc(sizeof(struct MinHNode)); + + temp->left = temp->right = NULL; + temp->item = item; + temp->freq = freq; + + return temp; +} + +// Create min heap +struct MinHeap *createMinH(unsigned capacity) { + struct MinHeap *minHeap = (struct MinHeap *)malloc(sizeof(struct MinHeap)); + + minHeap->size = 0; + + minHeap->capacity = capacity; + + minHeap->array = (struct MinHNode **)malloc(minHeap->capacity * sizeof(struct MinHNode *)); + return minHeap; +} + +// Function to swap +void swapMinHNode(struct MinHNode **a, struct MinHNode **b) { + struct MinHNode *t = *a; + *a = *b; + *b = t; +} + +// Heapify +void minHeapify(struct MinHeap *minHeap, int idx) { + int smallest = idx; + int left = 2 * idx + 1; + int right = 2 * idx + 2; + + if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq) + smallest = left; + + if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq) + smallest = right; + + if (smallest != idx) { + swapMinHNode(&minHeap->array[smallest], &minHeap->array[idx]); + minHeapify(minHeap, smallest); + } +} + +// Check if size if 1 +int checkSizeOne(struct MinHeap *minHeap) { + return (minHeap->size == 1); +} + +// Extract min +struct MinHNode *extractMin(struct MinHeap *minHeap) { + struct MinHNode *temp = minHeap->array[0]; + minHeap->array[0] = minHeap->array[minHeap->size - 1]; + + --minHeap->size; + minHeapify(minHeap, 0); + + return temp; +} + +// Insertion function +void insertMinHeap(struct MinHeap *minHeap, struct MinHNode *minHeapNode) { + ++minHeap->size; + int i = minHeap->size - 1; + + while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) { + minHeap->array[i] = minHeap->array[(i - 1) / 2]; + i = (i - 1) / 2; + } + minHeap->array[i] = minHeapNode; +} + +void buildMinHeap(struct MinHeap *minHeap) { + int n = minHeap->size - 1; + int i; + + for (i = (n - 1) / 2; i >= 0; --i) + minHeapify(minHeap, i); +} + +int isLeaf(struct MinHNode *root) { + return !(root->left) && !(root->right); +} + +struct MinHeap *createAndBuildMinHeap(char item[], int freq[], int size) { + struct MinHeap *minHeap = createMinH(size); + + for (int i = 0; i < size; ++i) + minHeap->array[i] = newNode(item[i], freq[i]); + + minHeap->size = size; + buildMinHeap(minHeap); + + return minHeap; +} + +struct MinHNode *buildHuffmanTree(char item[], int freq[], int size) { + struct MinHNode *left, *right, *top; + struct MinHeap *minHeap = createAndBuildMinHeap(item, freq, size); + + while (!checkSizeOne(minHeap)) { + left = extractMin(minHeap); + right = extractMin(minHeap); + + top = newNode('$', left->freq + right->freq); + + top->left = left; + top->right = right; + + insertMinHeap(minHeap, top); + } + return extractMin(minHeap); +} + +void printHCodes(struct MinHNode *root, int arr[], int top) { + if (root->left) { + arr[top] = 0; + printHCodes(root->left, arr, top + 1); + } + if (root->right) { + arr[top] = 1; + printHCodes(root->right, arr, top + 1); + } + if (isLeaf(root)) { + printf(" %c | ", root->item); + printArray(arr, top); + } +} + +// Wrapper function +void HuffmanCodes(char item[], int freq[], int size) { + struct MinHNode *root = buildHuffmanTree(item, freq, size); + + int arr[MAX_TREE_HT], top = 0; + + printHCodes(root, arr, top); +} + +// Print the array +void printArray(int arr[], int n) { + int i; + for (i = 0; i < n; ++i) + printf("%d", arr[i]); + + printf("\n"); +} + +int main() { + char arr[] = {'A', 'B', 'C', 'D'}; + int freq[] = {5, 1, 6, 3}; + + int size = sizeof(arr) / sizeof(arr[0]); + + printf(" Char | Huffman code "); + printf("\n--------------------\n"); + + HuffmanCodes(arr, freq, size); +} \ No newline at end of file From 41368fb99c780dff8f8f272a4016c7346ad74dd2 Mon Sep 17 00:00:00 2001 From: vaibhav mishra <89639461+VaibhavM01@users.noreply.github.com> Date: Mon, 25 Oct 2021 10:55:25 +0530 Subject: [PATCH 15/20] added jumpSearch.js Implemented Jumpsearch in javaScript --- Jump Search/jumpSearch.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Jump Search/jumpSearch.js diff --git a/Jump Search/jumpSearch.js b/Jump Search/jumpSearch.js new file mode 100644 index 0000000..af2b978 --- /dev/null +++ b/Jump Search/jumpSearch.js @@ -0,0 +1,26 @@ +function jumpSearch(arr, target) { + + let len = arr.length + let step = Math.floor(Math.sqrt(len)); + let blockStart = 0, currentStep = step; + + while (arr[Math.min(currentStep, len) - 1] < target) { + blockStart = currentStep; + currentStep += step; + + if (blockStart >= len) + return -1; + } + + while (arr[blockStart] < target) + { + blockStart++; + if (blockStart == Math.min(currentStep, len)) + return -1; + } + + if (arr[blockStart] == target) + return blockStart + else + return -1; +} From b23f4378337d0a0d79f4e2ad258ad23b31bfdbcf Mon Sep 17 00:00:00 2001 From: BugHunters-bug <91728280+BugHunters-bug@users.noreply.github.com> Date: Thu, 28 Oct 2021 12:54:46 +0530 Subject: [PATCH 16/20] delete newtextdocument.txt --- hufman code/New Text Document.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 hufman code/New Text Document.txt diff --git a/hufman code/New Text Document.txt b/hufman code/New Text Document.txt deleted file mode 100644 index e69de29..0000000 From da3c67b8256bed4d5b90ab8c26a9ff1dd939113c Mon Sep 17 00:00:00 2001 From: Izuku29 <89025886+Izuku29@users.noreply.github.com> Date: Thu, 28 Oct 2021 22:57:40 +0530 Subject: [PATCH 17/20] fibonacci in c using a recursive function --- Fibonacci series/fibonacci in c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Fibonacci series/fibonacci in c diff --git a/Fibonacci series/fibonacci in c b/Fibonacci series/fibonacci in c new file mode 100644 index 0000000..b59d44f --- /dev/null +++ b/Fibonacci series/fibonacci in c @@ -0,0 +1,14 @@ +#include +int fib(int n) +{ + if(n<=1) + return n; + return fib(n-1)+fib(n-2); +} +int main() +{ + int a; + scanf("%d",&a); + printf("%d",fib(a)); + return 0; +} From e45968b56c0ed146584911baa3a595ed0aaa1a69 Mon Sep 17 00:00:00 2001 From: BugHunters-bug <91728280+BugHunters-bug@users.noreply.github.com> Date: Fri, 29 Oct 2021 12:51:30 +0530 Subject: [PATCH 18/20] rename --- Fibonacci series/{fibonacci in c => fibonacci.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Fibonacci series/{fibonacci in c => fibonacci.c} (100%) diff --git a/Fibonacci series/fibonacci in c b/Fibonacci series/fibonacci.c similarity index 100% rename from Fibonacci series/fibonacci in c rename to Fibonacci series/fibonacci.c From fb2c501bf9ad0d4ee13a0d3ac78cfafccf5bb31b Mon Sep 17 00:00:00 2001 From: chakeson <80765479+chakeson@users.noreply.github.com> Date: Fri, 29 Oct 2021 14:24:11 +0200 Subject: [PATCH 19/20] Added Luhns algorithm --- Luhn algorithm/Luhn.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Luhn algorithm/Luhn.py diff --git a/Luhn algorithm/Luhn.py b/Luhn algorithm/Luhn.py new file mode 100644 index 0000000..c267c9b --- /dev/null +++ b/Luhn algorithm/Luhn.py @@ -0,0 +1,23 @@ +""" + Luhn algorithm or Luhn formula + Checksum algorithm. + Source:https://en.wikipedia.org/wiki/Luhn_algorithm + +""" + +def luhn(input_number): + + input_number = [int(i) for i in str(input_number)] + input_number.reverse() + + sum_answer = 0 + for i, number in enumerate(input_number): + temp = number + if i % 2 == 0: + temp = number * 2 + if temp > 9: + temp -= 9 + + sum_answer = sum_answer + temp + + return sum_answer % 10 == 0 From 130ae49ed6b093ff83a41989575b0537b2032c44 Mon Sep 17 00:00:00 2001 From: cheemra <93471584+cheemra@users.noreply.github.com> Date: Sun, 31 Oct 2021 21:52:37 +0530 Subject: [PATCH 20/20] Create tree_dsa.cpp --- Tree/tree_dsa.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Tree/tree_dsa.cpp diff --git a/Tree/tree_dsa.cpp b/Tree/tree_dsa.cpp new file mode 100644 index 0000000..5b96ed8 --- /dev/null +++ b/Tree/tree_dsa.cpp @@ -0,0 +1,129 @@ +#include +using namespace std; +#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); +typedef long long ll; +const ll mod=1e9+7; +//<------disjoint set-union------> +vectorparent,size; +void make_set(int v) { + parent[v] = v; + size[v] = 1; +} + +void union_sets(int a, int b) { + a = find_set(a); + b = find_set(b); + if (a != b) { + if (size[a] < size[b]) + swap(a, b); + parent[b] = a; + size[a] += size[b]; + } +} + +int find_set(int v) { + if (v == parent[v]) + return v; + return find_set(parent[v]); +} + +//<-----------fenwick tree---------> +struct FenwickTree { + vector bit; // binary indexed tree + int n; + + FenwickTree(int n) { + this->n = n; + bit.assign(n, 0); + } + + FenwickTree(vector a) : FenwickTree(a.size()) { + for (size_t i = 0; i < a.size(); i++) + add(i, a[i]); + } + + int sum(int r) { + int ret = 0; + for (; r >= 0; r = (r & (r + 1)) - 1) + ret += bit[r]; + return ret; + } + + int sum(int l, int r) { + return sum(r) - sum(l - 1); + } + + void add(int idx, int delta) { + for (; idx < n; idx = idx | (idx + 1)) + bit[idx] += delta; + } +}; + +//<-------------square root decomposition--------------> + +void sqrt_decmpO(){ +//input data +int n; +vector a (n); + +// preprocessing +int len = (int) sqrt (n + .0) + 1; // size of the block and the number of blocks +vector b (len); +for (int i=0; i +int MAXN; +int n; +vectort(4*MAXN); +void build(int a[], int v, int tl, int tr) { + if (tl == tr) { + t[v] = a[tl]; + } else { + int tm = (tl + tr) / 2; + build(a, v*2, tl, tm); + build(a, v*2+1, tm+1, tr); + t[v] = t[v*2] + t[v*2+1]; + } +} +int sum(int v, int tl, int tr, int l, int r) { + if (l > r) + return 0; + if (l == tl && r == tr) { + return t[v]; + } + int tm = (tl + tr) / 2; + return sum(v*2, tl, tm, l, min(r, tm)) + + sum(v*2+1, tm+1, tr, max(l, tm+1), r); +} +void update(int v, int tl, int tr, int pos, int new_val) { + if (tl == tr) { + t[v] = new_val; + } else { + int tm = (tl + tr) / 2; + if (pos <= tm) + update(v*2, tl, tm, pos, new_val); + else + update(v*2+1, tm+1, tr, pos, new_val); + t[v] = t[v*2] + t[v*2+1]; + } +}