diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 851d09b6..00000000 Binary files a/.DS_Store and /dev/null differ diff --git a/algorithms/dsu/naive_dsu.cpp b/algorithms/dsu/naive_dsu.cpp index 84ea37a9..5686fcc0 100644 --- a/algorithms/dsu/naive_dsu.cpp +++ b/algorithms/dsu/naive_dsu.cpp @@ -33,11 +33,11 @@ void make_set(int val) int find_parent(int val) { - if(val == parent[val]) - { - return val; + while (parent[val] != val) { + parent[val] = parent[parent[val]]; + val = parent[val]; } - return find_parent(parent[val]); + return val; } void union_set(int x, int y) diff --git a/algorithms/math/Prime_Factorization.cpp b/algorithms/math/Prime_Factorization.cpp new file mode 100644 index 00000000..f4ed245b --- /dev/null +++ b/algorithms/math/Prime_Factorization.cpp @@ -0,0 +1,75 @@ +#include +#include +using namespace std; +vectorget_factors(vector&primes,int n) +{ + vectorans; + ans.clear(); + for(int i=0;i*i<=n;i++) + { + if(n%primes[i]==0) + { + ans.push_back(primes[i]); + while(n%primes[i]==0) + { + n=n/primes[i]; + } + + } + } + if(n!=1) + { + ans.push_back(n); + } + return ans; +} +vectorget_prime(vector&prime,int n) +{ + for(int i=3;i<=1000;i+=2) + { + prime[i]=1; + } + //mark all the even number as non prime + for(int i=3;i*i<=1000;i+=2) + { + if(prime[i]==1) + { + for(int j=i*i;j<=1000;j+=i) + { + prime[j]=0; + } + } + } + prime[0]=prime[1]=0; + prime[2]=1; + vectorfectors; + for(int i=2;i<=1000;i++) + { + if(prime[i]==1) + { + fectors.push_back(i); + } + } +return fectors; + +} +int32_t main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + //first task to genrate prime fectors and store it + vectorp(1000,0); + //now we have all prime number till n + int n; + cin>>n; + //enter n (the number you want to get prime fector) + vectorprimes=get_prime(p,n); + vectorans=get_factors(primes,n); + cout<<"Prime Factors are :"; + for(auto i:ans) + { + cout< +#include +#include + +using namespace std; + +// Method : +// Firstly initialize two heaps, lower and upper such that +// abs(lower.size() - upper.size()) <= 1 && +// median >= lower[i] && median <= upper[i] +// The median is either the average of the two top values +// or the top value of the greatest heap. +// To add a value add it to the smallest heap and swap tops +// if lower and upper don't follow the first condition. +// To remove a value we find it, remove it and add the top of +// the other heap to the updated heap (sizes are now the same). +// We just remove and then add an item for each index of [0, N-k) +// to simulate a sliding window. +vector medianSubarray(vector& values, int k) { + // We use long long instead of int to avoid overflows + // median >= lower[i] && median <= upper[i] + multiset> lower; + multiset upper; + vector medians; + + // Initialize lower and upper + for (int i = 0; i < k; ++i) { + if (lower.size() <= upper.size()) + lower.insert(values[i]); + else + upper.insert(values[i]); + + // Swap values if heaps aren't balanced + if (!lower.empty() && !upper.empty() && + *lower.begin() > *upper.begin()) { + long long tmp = *lower.begin(); + lower.erase(lower.begin()); + lower.insert(*upper.begin()); + upper.erase(upper.begin()); + upper.insert(tmp); + } + } + +// To get the median inline +#define GET_MEDIAN (lower.size() == upper.size() ? \ + (*lower.begin() + (*upper.begin() - *lower.begin()) * .5) : \ + lower.size() > upper.size() ? *lower.begin() : *upper.begin()) + + medians.push_back(GET_MEDIAN); + + for (int i = 0; i < values.size() - k; ++i) { + // Remove the i item + bool removed = false; + if (!lower.empty() && values[i] <= *lower.begin()) { + // Remove it + auto target = lower.find(values[i]); + if (target != lower.end()) { + lower.erase(target); + removed = true; + + // Insert the top of the other heap to the updated heap + if (!upper.empty()) { + lower.insert(*upper.begin()); + upper.erase(upper.begin()); + } + } + } + + if (!removed) { + // Remove it + upper.erase(upper.find(values[i])); + + // Insert the top of the other heap to the updated heap + if (!lower.empty()) { + upper.insert(*lower.begin()); + lower.erase(lower.begin()); + } + } + + // Add the i + k item + if (lower.size() <= upper.size()) + lower.insert(values[i + k]); + else + upper.insert(values[i + k]); + + // Swap values if heaps aren't balanced + if (!lower.empty() && !upper.empty() && + *lower.begin() > *upper.begin()) { + long long tmp = *lower.begin(); + lower.erase(lower.begin()); + lower.insert(*upper.begin()); + upper.erase(upper.begin()); + upper.insert(tmp); + } + + medians.push_back(GET_MEDIAN); + } + + return medians; +} + +int main() { + vector>> dataset = { + {5, {5,2,2,7,3,7,9,0,2,3}}, + {3, {1,3,-1,-3,5,3,6,7}}, + {1, {1,2}}, + {2, {2147483647,2147483647}}, + }; + + // Should be : + // 3 3 7 7 3 3 + // 1 -1 -1 3 5 6 + // 1 2 + // 2.14748e+09 + for (auto test : dataset) { + auto y = medianSubarray(test.second, test.first); + for (double median : y) + cout << median << " "; + + cout << endl; + } + + return 0; +} diff --git a/algorithms/math/prime.cpp b/algorithms/math/prime.cpp index 02f55b28..1e4a0734 100644 --- a/algorithms/math/prime.cpp +++ b/algorithms/math/prime.cpp @@ -1,14 +1,17 @@ #include using namespace std; -int main() -{int n,flag; -cin>>n; -//to check whether a number is prime or not -for(int i=2;i0) - cout<<"number is not prime"<>n; + //to check whether a number is prime or not + bool isPrime=true; + for(int i = 2 ;i <= n/2; i++){ + if( n%i == 0){ + isPrime=false; + break; + } + } + if(isPrime) cout<<"Number is Prime"< +using namespace std; + +int factorial(int begin, int end) +{ + int num = 1; + for (int i = begin; i <= end; i++) + num *= i; + + return num; +} + +int square(int n) +{ + return factorial(n + 1, 2 * n) / factorial(1, n); +} + +int main() +{ + int n; + cout << "Enter the number :"; + cin >> n; + cout << "The Sum of Square is " << square(n) << endl; + return 0; +} + diff --git a/algorithms/matrix_linear.cpp b/algorithms/matrix_linear.cpp index d466729f..75ead31e 100644 --- a/algorithms/matrix_linear.cpp +++ b/algorithms/matrix_linear.cpp @@ -1,5 +1,5 @@ // represent linear equation in form of matrix -#include +#include #include using namespace std; diff --git a/algorithms/sorting/bubble_sort.cpp b/algorithms/sorting/bubble_sort.cpp index ed22d402..7b23d915 100644 --- a/algorithms/sorting/bubble_sort.cpp +++ b/algorithms/sorting/bubble_sort.cpp @@ -8,47 +8,80 @@ // Contributed by: Abhishek Jaiswal // Github: @Abhishek-iiit // -#include -#include - -using namespace std; - -void bubbleSort(int arr[], int n) -{ - int i, j; - bool changed; - for (i = 0; i < n-1; i++) - { - changed = false; - for (j = 0; j < n-i-1; j++) - { - if (arr[j] > arr[j+1]) - { - swap(arr[j],arr[j+1]); - changed = true; +// Refactoring by: Cigan Oliviu David +// Github: @CiganOliviu +// + +#include + + +unsigned int readLength() { + + unsigned int length; + + std::cin >> length; + + return length; +} + +void readArray(int array[], unsigned int length) { + + for (int i = 0; i < length; i++) + std::cin >> array[i]; +} + + +void bubbleSortArray(int array[], unsigned int length) { + + bool changed; + + length -= 1; + + for (int i = 0; i < length; i++) { + + changed = false; + + for (int j = 0; j < length - i; j++) { + + if (array[j] > array[j + 1]) { + + std::swap(array[j], array[j+1]); + changed = true; + } } - } - if (changed == false) - break; - } -} - -int main() -{ - int n; - cout<<"Input the total size :"<>n; - int arr[n]; - cout<<"Input the number one-by-one :"<>arr[i]; - } - bubbleSort(arr,n); - cout<<"Sorted array:"< +using namespace std; + +int Len(string &str1, string &str2) +{ + int len1 = str1.size(); + int len2 = str2.size(); + if (len1 < len2) + { + for (int i = 0 ; i < len2 - len1 ; i++) + str1 = '0' + str1; + return len2; + } + else if (len1 > len2) + { + for (int i = 0 ; i < len1 - len2 ; i++) + str2 = '0' + str2; + } + return len1; +} + +string add( string a, string b ) +{ + string result; + int len = Len(a, b); + + int carry = 0; + for (int i = len-1 ; i >= 0 ; i--) + { + int aBit = a.at(i) - '0'; + int bBit = b.at(i) - '0'; + int sum = (aBit ^ bBit ^ carry)+'0'; + + result = (char)sum + result; + carry = (aBit & bBit) | (bBit & carry) | (aBit & carry); + } + + if (carry) + result = '1' + result; + + return result; +} + +int main() +{ + string str1,str2; + cout<<"Enter the string 1 :"; + cin>>str1; + cout<<"Enter the string 2 :"; + cin>>str2; + cout << "Sum is " << add(str1, str2); + return 0; +} diff --git a/algorithms/strings/divide_string_in_n_equal_parts.cpp b/algorithms/strings/divide_string_in_n_equal_parts.cpp new file mode 100644 index 00000000..83e423fb --- /dev/null +++ b/algorithms/strings/divide_string_in_n_equal_parts.cpp @@ -0,0 +1,53 @@ +// C++ program to divide a string +// in n equal parts +#include +#include + +using namespace std; + +class string_division +{ + +// Function to print n equal parts of str +public: +void divide_String(char str[], int n) +{ + + int str_size = strlen(str); + int i; + int part_size; + + // Check if string can be divided in + // n equal parts + if (str_size % n != 0) + { + cout<<"Invalid Input: String size"; + cout<<" is not divisible by n"; + return; + } + + // Calculate the size of parts to + // find the division points + part_size = str_size / n; + for (i = 0; i< str_size; i++) + { + if (i % part_size == 0) + cout< +using namespace std; + +int main() { + string s; + int cnt; + cout << "Enter the string: "; + cin >> s; + + cnt = 0; + for ( char ch : s ) { + switch ( ch ) { + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + cnt++; + break; + default: + break; + } + } + + cout << "The string has " << cnt << " vowels.\n"; + + return 0; +} diff --git a/data-structures/Priority_Queues/kthLargestUsingPriorityQueue.cpp b/data-structures/Priority_Queues/kthLargestUsingPriorityQueue.cpp new file mode 100644 index 00000000..582d0118 --- /dev/null +++ b/data-structures/Priority_Queues/kthLargestUsingPriorityQueue.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +/* + +Given an array A of random integers and an integer k, find and return the kth largest element in the array. + +*/ + +int kthLargest (vector arr, int n, int k){ + + priority_queue, greater> pq; + + for(int i = 0; i < k; i++) { + pq.push(arr[i]); + } + + for(int i = k; i < n; i++) { + int val = pq.top(); + if(arr[i] > val) { + pq.pop(); + pq.push(arr[i]); + } + } + + return pq.top(); + +} + +int main() { + int n, k, s; + vector arr; + cin >> n; + + for (int i = 0; i < n; i++) { + cin >> s; + arr.push_back(s); + } + + cin >> k; + cout << kthLargest(arr, n, k) << endl; +} + +// Time complexity of this solution is O(nlogn). diff --git a/data-structures/avl.cpp b/data-structures/avl.cpp new file mode 100644 index 00000000..3be62efe --- /dev/null +++ b/data-structures/avl.cpp @@ -0,0 +1,175 @@ +#include +#include +using namespace std; + +struct node +{ + int data, height; + node *left, *right; +}; + +class avl +{ + node* root=NULL; +public: + int height(node* root){ + if(root) + return root->height; + return 0; + } + node* rotateLeft(node* root){ + node* newroot = root->right; + root->right = newroot->left; + newroot->left = root; + return newroot; + } + node* rotateRight(node* root){ + node* newroot = root->left; + root->left = newroot->right; + newroot->right = root; + return newroot; + } + void insert(int x){ + this->root = insert(this->root, x); + } + node* insert(node* root, int x){ + if(root==NULL){ + root = new node; + root->data = x; + root->height = 1; + root->left = NULL; + root->right = NULL; + return root; + } + if(x < root->data) + root->left = insert(root->left,x); + else if(x > root->data) + root->right = insert(root->right,x); + else + return root; + + int lh = height(root->left), rh = height(root->right); + root->height = 1 + max(lh,rh); + + lh -= rh; + if(lh>1){ + if(x > root->left->data) + root->left = rotateLeft(root->left); + return rotateRight(root); + } + else if(lh<-1){ + if(x < root->right->data) + root->right = rotateRight(root->right); + return rotateLeft(root); + } + return root; + } + int minValue(node* root){ + while(root->left) + root = root->left; + return root->data; + } + void remove(int x){ + this->root = remove(this->root, x); + } + node* remove(node* root, int x){ + if(root == NULL) + return NULL; + if(x < root->data) + root->left = remove(root->left, x); + else if(x > root->data) + root->right = remove(root->right, x); + else{ + if(root->left == NULL && root->right == NULL){ + delete root; + return NULL; + } + else if(root->right == NULL){ + node* temp = root; + root = root->left; + delete temp; + return root; + } + else if(root->left == NULL){ + node* temp = root; + root = root->right; + delete temp; + return root; + } + else{ + root->data = minValue(root->right); + root->right = remove(root->right, root->data); + } + } + + int lh = height(root->left), rh = height(root->right); + root->height = 1 + max(lh,rh); + + lh -= rh; + if(lh>1){ + if(x > root->left->data) + root->left = rotateLeft(root->left); + return rotateRight(root); + } + else if(lh<-1){ + if(x < root->right->data) + root->right = rotateRight(root->right); + return rotateLeft(root); + } + return root; + } + void printLevel(){ + printLevel(this->root); + } + void printLevel(node* root){ + if(root){ + queue q; + q.push(root); + q.push(NULL); + node* temp; + while(!q.empty()){ + temp = q.front(); + q.pop(); + if(temp){ + cout << temp->data << " "; + if(temp->left) + q.push(temp->left); + if(temp->right) + q.push(temp->right); + } + else if(q.front()){ + cout << endl; + q.push(NULL); + } + } + } + } + void print(){ + print(this->root); + } + void print(node* root){ + if(root){ + print(root->left); + cout << root->data << " "; + print(root->right); + } + } +}; + +int main() +{ + avl a; + a.insert(5); + a.insert(6); + a.insert(7); + a.insert(8); + a.insert(9); + a.insert(4); + a.printLevel(); + a.remove(7); + a.remove(5); + cout << endl; + a.printLevel(); + + return 0; +} \ No newline at end of file diff --git a/data-structures/binaryTreeMinTimeBurn.cpp b/data-structures/binaryTreeMinTimeBurn.cpp new file mode 100644 index 00000000..a039c389 --- /dev/null +++ b/data-structures/binaryTreeMinTimeBurn.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +class Node{ +public: + int data; + Node *left, *right; + Node(int x){ + this->data = x; + this->left = this->right = NULL; + } +}; + + +pair minTime(Node* node, int *result){ + if(node->left == NULL && node->right == NULL) + return {0,1}; + cout << "Node " << node->data << endl; + pair ansl={0,-1},ansr={0,-1}; + if(node->left) + ansl = minTime(node->left,result); + if(node->right) + ansr = minTime(node->right,result); + if(ansl.second == -1) + return {ansr.first+1,ansr.second+1}; + if(ansr.second == -1) + return {ansl.first+1,ansl.second+1}; + if(ansl.firstleft = new Node(1); + root->right = new Node(2); + root->left->left = new Node(3); + root->left->right = new Node(4); + root->right->left = new Node(5); + root->right->right = new Node(6); + root->left->left->left = new Node(10); + root->left->right->left = new Node(9); + root->right->right->left = new Node(7); + root->right->right->right = new Node(8); + + cout << "Exec Start" << endl; + int *result = new int; + minTime(root,result); + cout << *result << endl; + + return 0; +} \ No newline at end of file diff --git a/data-structures/linkedList.cpp b/data-structures/linkedList.cpp new file mode 100644 index 00000000..33ef17bc --- /dev/null +++ b/data-structures/linkedList.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +class Node { +public: + int data; + Node* next; + + Node(int data) { + this->data = data; + this->next = NULL; + } +}; + +class LinkedList { +public: + Node* head; + + LinkedList() { + this->head = NULL; + } + + void addNode(int data) { + Node* newNode = new Node(data); + if (this->head == NULL) { + this->head = newNode; + return; + } + Node* currentNode = this->head; + while (currentNode->next != NULL) { + currentNode = currentNode->next; + } + currentNode->next = newNode; + } + + void printList() { + Node* currentNode = this->head; + while (currentNode != NULL) { + cout << currentNode->data << " "; + currentNode = currentNode->next; + } + cout << endl; + } +}; + +int main() { + LinkedList list; + list.addNode(1); + list.addNode(2); + list.addNode(3); + list.addNode(4); + list.addNode(5); + list.printList(); + return 0; +} \ No newline at end of file