├── hacktoberfest.png ├── Pattern in C++ ├── StarPatternImg.png ├── AlphabetPatternImg.png ├── NumberPatternImg.png ├── README.md └── AlphabetPattern.cpp ├── Codeforces_800_Difficulty ├── A_Domino_piling.cpp ├── A_Quirky_Quantifiers.cpp ├── A_Word_Capitalization.cpp ├── A_Soldier_and_Bananas.cpp ├── A_Calculating_Function.cpp ├── A_Ultra_Fast_Mathematician.cpp ├── A_Bear_and_Big_Brother.cpp ├── A_Translation.cpp ├── A_Elephant.cpp ├── A_George_and_Accommodation.cpp ├── A_Watermelon.cpp ├── A_Magnets.cpp ├── A_Tram.cpp ├── A_Wrong_Subtraction.cpp ├── A_Vanya_and_Fence.cpp ├── A_Bit_.cpp ├── A_Helpful_Maths.cpp ├── A_Hulk.cpp ├── A_In_Search_of_an_Easy_Problem.cpp ├── B_Queue_at_the_School.cpp ├── A_Nearly_Lucky_Number.cpp ├── A_Stones_on_the_Table.cpp ├── A_Petya_and_Strings.cpp ├── A_Team.cpp ├── A_Beautiful_Matrix.cpp ├── A_Way_Too_Long_Words.cpp ├── A_Beautiful_Year.cpp ├── A_Presents.cpp ├── A_Word.cpp ├── A_Anton_and_Danik.cpp ├── A_Next_Round.cpp ├── A_Boy_or_Girl.cpp └── A_Arrival_of_the_General.cpp ├── SUMofGCD.cpp ├── FIBONACCI.cpp ├── Decimal to binary.cpp ├── .cph └── .Fibonacci_Number_O(n).cpp_82cd9304c66554f98698c11aa2195573.prob ├── Josephus Problem └── josephus.cpp ├── ReverseString.cpp ├── WordCounter.cpp ├── Pi.cpp ├── Prime Sieve ├── Count Inversion Problem ├── Floyd's triangle.cpp ├── diamond PATTERN with sides joined.cpp ├── is_palindrom ├── Fibonacci_Number_O(n).cpp ├── PrimeNo.cpp ├── frequency.cpp ├── Dyanamic programing ├── painting_fence.c++ ├── Fibbionaci.c++ ├── min_no_of_coin.c++ ├── subset_sum.c++ ├── LCS Problem.cpp ├── unbounded_knapsack.c++ ├── Readme.md ├── O-1 Knapsack.c++ └── EggDroppingProblem.cpp ├── Binary_Exponentiation_Recursively.cpp ├── merge.cpp ├── CatalanNumber.cpp ├── find_frequency .cpp ├── Anagram.cpp ├── LCM.cpp ├── Insertion_Sort.cpp ├── TSP using DP.cpp ├── RecursiveBinarySearch.cpp ├── Binary_search.cpp ├── 10001PrimeNumber.cpp ├── Bubble_sort.cpp ├── 0-1_Knapsack.cpp ├── Dutch_National_Flag.cpp ├── Dutch_National_Flag_algo.cpp ├── magicnum.cpp ├── Matrix multiplication ├── Selection_sort.cpp ├── Armstrong.cpp ├── LinearSearch.cpp ├── Permutation_Sequence.cpp ├── Deletion_inbetween_DoublyLinkedList.cpp ├── Graph ├── BFSByMatrix.cpp ├── GraphRepresentation.cpp └── DFSbyMatrixAndRecursion.cpp ├── dijktras.cpp ├── TowerOfHanoi.cpp ├── stack_implementation.cpp ├── sliding_window.cpp ├── transposeMatrix.cpp ├── Heap_sort.cpp ├── nQueen.cpp ├── Edit_Distance_DP.cpp ├── shell_sort.cpp ├── SMALLEST PRIME NUMBER MISSING FROM THE ARRAY.cpp ├── Smallest Prime Number Missing From The Array.cpp ├── Simple Text Editor.cpp ├── Find Original Array From Doubled Array.cpp ├── variadicFunction.cpp ├── Linked list ├── Reverseknodes.cpp ├── linkedintro.cpp ├── EvenAfterOddLinkedList.cpp ├── IntersectionOfLinkedlist.cpp ├── DeletionInLinkedList2.cpp ├── DoublyLinkedList.cpp ├── AppendLastknodes.cpp ├── DetectionAndRemoval.cpp ├── reversing_linkedlist_using_stack.cpp ├── ReverseLinkedList3.cpp ├── CircularLinkedList.cpp └── MergeLinkedList.cpp ├── merge_sort.cpp ├── Pancake_sort.cpp ├── QuickSort.cpp ├── radix_sort.cpp ├── delete_circular_linkedlist_hacktoberfest.cpp ├── TwoSum.cpp ├── Kruskal's_MST.cpp ├── peakElement.cpp ├── Sum_of_leaf_nodes_at_each_horizontal_level_in_a_binary_tree.cpp ├── circularqueue.cpp ├── DIGITREM.cpp ├── Median of Two Sorted Arrays.cpp ├── BookAllocationProblem.cpp ├── Kadane_Algorithm.cpp ├── Reverse_Circular_Doubly_Linklist.cpp ├── Multiple_Paranthesis_Using_Stack.cpp ├── dequeue.cpp ├── The Knight’s tour problem_Backtracking.cpp ├── calculator.cpp ├── Inorder_Traversal.cpp ├── Preorder_Traversal.cpp ├── StrassensMM.cpp ├── Min_Spanning_Tree_using_Kruskal_Algorithm_.cpp ├── README.md ├── Quiz Game ├── Bloom_filter_.cpp ├── Primes_Algorithm.cpp ├── Traveling Salesman Problem using Branch And Bound.cpp └── HashTable.cpp /hacktoberfest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imnurav/Hactoberfest2021-Cpp/HEAD/hacktoberfest.png -------------------------------------------------------------------------------- /Pattern in C++/StarPatternImg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imnurav/Hactoberfest2021-Cpp/HEAD/Pattern in C++/StarPatternImg.png -------------------------------------------------------------------------------- /Pattern in C++/AlphabetPatternImg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imnurav/Hactoberfest2021-Cpp/HEAD/Pattern in C++/AlphabetPatternImg.png -------------------------------------------------------------------------------- /Pattern in C++/NumberPatternImg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imnurav/Hactoberfest2021-Cpp/HEAD/Pattern in C++/NumberPatternImg.png -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Domino_piling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int m,n; 5 | cin>>m>>n; 6 | cout<<(m*n)/2<<"\n"; 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Quirky_Quantifiers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin>>s; 7 | transform(s.begin(), s.begin()+1, s.begin(), ::toupper); 8 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int k,n,w; 6 | cin>>k>>n>>w; 7 | int cost=((w*(w+1))/2)*k; 8 | (cost>n)?(cout< 2 | using namespace std; 3 | int main(){ 4 | long long int n; 5 | cin>>n; 6 | long long int o=(n+1)/2; 7 | long long int e=n/2; 8 | cout<<(e*(e+1))-(o*o)< 2 | using namespace std; 3 | int main(){ 4 | string s1,s2; 5 | cin>>s1>>s2; 6 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int a,b,count=0; 6 | cin>>a>>b; 7 | while(a<=b){ 8 | a*=3; 9 | b*=2; 10 | count++; 11 | } 12 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | string s1,s2; 6 | cin>>s1>>s2; 7 | reverse(s1.begin(),s1.end()); 8 | if(s1==s2){ 9 | cout<<"YES\n"; 10 | } 11 | else{ 12 | cout<<"NO\n"; 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Elephant.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int x; 6 | cin>>x; 7 | if(x<=5){ 8 | cout<<"1\n"; 9 | } 10 | else if(x%5==0){ 11 | cout< 2 | using namespace std; 3 | typedef long long int ll; 4 | int gcd(int a, int b) 5 | { 6 | return b == 0 ? a : gcd(b, a % b); 7 | } 8 | int main() 9 | { 10 | ll n,sum=0; 11 | cin>>n; 12 | for(int i=1;i<=n;i++) 13 | { 14 | sum+=i*gcd(i,n); 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,count=0; 6 | cin>>n; 7 | while(n--){ 8 | int p,q; 9 | cin>>p>>q; 10 | if(q-p>=2){ 11 | count++; 12 | } 13 | } 14 | cout< 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(0); 6 | cout.tie(0); 7 | int num; 8 | cin>>num; 9 | if(num%2!=0 || num<4){ 10 | cout<<"NO\n"; 11 | } 12 | else{ 13 | cout<<"YES\n"; 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Magnets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,prev=0,count=0; 6 | cin>>n; 7 | while(n--){ 8 | int a; 9 | cin>>a; 10 | if(a!=prev){ 11 | count++; 12 | } 13 | prev=a; 14 | } 15 | cout< 2 | using namespace std; 3 | int fib (int n) 4 | { 5 | if(n<2) 6 | { 7 | return 1; 8 | } 9 | return fib(n-2) + fib(n-1); 10 | } 11 | int main() 12 | { 13 | int a; 14 | cout<<"Enter your number : "<>a; 16 | cout<<"The fibonacci sequence of the number will be : "< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,p=0,mp=0; 6 | cin>>n; 7 | while(n--){ 8 | int a,b; 9 | cin>>a>>b; 10 | if(b 2 | using namespace std; 3 | 4 | int main(){ 5 | int num,k; 6 | cin>>num>>k; 7 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,h,w=0; 6 | cin>>n>>h; 7 | while(n--){ 8 | int a; 9 | cin>>a; 10 | if(a>h){ 11 | w+=2; 12 | } 13 | else{ 14 | w++; 15 | } 16 | } 17 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int a[10], n, i; 6 | cout<<"Enter the number to convert: "; 7 | cin>>n; 8 | for(i=0; n>0; i++) 9 | { 10 | a[i]=n%2; 11 | n= n/2; 12 | } 13 | cout<<"Binary of the given number= "; 14 | for(i=i-1 ;i>=0 ;i--) 15 | { 16 | cout< 2 | using namespace std; 3 | int main(){ 4 | int test,add=0,sub=0; 5 | cin>>test; 6 | for(int i=0; i>s; 9 | if(s[1]=='+'){ 10 | add++; 11 | } 12 | else if(s[1]=='-'){ 13 | sub++; 14 | } 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin>>s; 7 | vector v; 8 | for(int i=0; i 2 | using namespace std; 3 | 4 | int jos(int n,int k) 5 | { 6 | if(n==1) 7 | return 0; 8 | 9 | return (jos(n-1,k)+k)%n; 10 | 11 | } 12 | 13 | 14 | 15 | int main() 16 | { 17 | int n,k; 18 | cin>>n>>k; 19 | 20 | //circle starting from 0 21 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | for(int i=1; i<=n; i++){ 8 | if(i==1){ 9 | cout<<"I hate "; 10 | } 11 | else if(i%2!=0){ 12 | cout<<"that I hate "; 13 | } 14 | else{ 15 | cout<<"that I love "; 16 | } 17 | } 18 | cout<<"it"; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_In_Search_of_an_Easy_Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | bool b=true; 7 | while(n--){ 8 | int a; 9 | cin>>a; 10 | if(a==1){ 11 | b=false; 12 | } 13 | } 14 | if(b){ 15 | cout<<"EASY\n"; 16 | } 17 | else{ 18 | cout<<"HARD\n"; 19 | } 20 | 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /ReverseString.cpp: -------------------------------------------------------------------------------- 1 | // A Simple C++ program to reverse a string 2 | #include 3 | using namespace std; 4 | void 5 | reverseStr (string & str) 6 | { 7 | 8 | int n = str.length (); 9 | for (int i = 0; i < n / 2; i++) 10 | 11 | swap (str[i], str[n -i-1]); 12 | 13 | } 14 | int 15 | main () 16 | { 17 | string str = "Varunkumar"; 18 | reverseStr (str); 19 | 20 | cout << str; 21 | 22 | return 0; 23 | 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/B_Queue_at_the_School.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,t; 6 | cin>>n>>t; 7 | string s; 8 | cin>>s; 9 | while(t--){ 10 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | long long int n; 6 | int count=0; 7 | cin>>n; 8 | while(n){ 9 | if(n%10==4 || n%10==7){ 10 | count++; 11 | } 12 | n/=10; 13 | } 14 | if(count==4 || count==7){ 15 | cout<<"YES\n"; 16 | } 17 | else{ 18 | cout<<"NO\n"; 19 | } 20 | 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Stones_on_the_Table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int num, count=0; 6 | cin>>num; 7 | string s; 8 | cin>>s; 9 | if(num<2){ 10 | cout<<"0\n"; 11 | } 12 | else{ 13 | for(int i=0; i 2 | using namespace std; 3 | int main(void) 4 | { 5 | char str1[] = "Hello, this is a \nWord Counter\n Program "; 6 | char *str=str1; 7 | int state = 0, wc = 0; 8 | while (*str) 9 | { 10 | if (*str == ' ' || *str == '\n' || *str == '\t') 11 | state = 0; 12 | else if (state == 0) 13 | { 14 | state = 1; 15 | ++wc; 16 | } 17 | ++str; 18 | } 19 | cout<<"No of words : "< 2 | using namespace std; 3 | 4 | int main(){ 5 | string s1,s2; 6 | cin>>s1>>s2; 7 | transform(s1.begin(), s1.end(), s1.begin(), ::tolower); 8 | transform(s2.begin(), s2.end(), s2.begin(), ::tolower); 9 | if(s1 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,count=0; 6 | cin>>n; 7 | vector v(3,0); 8 | for(int i=0; i>a; 13 | sum+=a; 14 | } 15 | if(sum>1){ 16 | count++; 17 | } 18 | } 19 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int arr[5][5],z,r,c; 6 | for(int i=0; i<5; i++){ 7 | for(int j=0; j<5; j++){ 8 | int a; 9 | cin>>a; 10 | arr[i][j]=a; 11 | if(a==1){ 12 | r=i+1; 13 | c=j+1; 14 | } 15 | } 16 | } 17 | int moves=abs(r-3)+abs(c-3); 18 | cout< 13 | 14 | ## Number Pattern 15 | ![Number Pattern](./NumberPatternImg.png) 16 | 17 |
18 | 19 | ## Alphabet Pattern 20 | ![Alphabet Pattern](./AlphabetPatternImg.png) 21 | -------------------------------------------------------------------------------- /Pi.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include "bits/stdc++.h" 5 | using namespace std; 6 | 7 | // Function that prints the 8 | // value of pi 9 | void printValueOfPi() 10 | { 11 | 12 | // Find value of pi using 13 | // acos() function 14 | double pi = 2 * acos(0.0); 15 | 16 | // Print value of pi 17 | printf("%f\n", pi); 18 | } 19 | 20 | // Driver Code 21 | int main() 22 | { 23 | // Function that prints 24 | // the value of pi 25 | printValueOfPi(); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Way_Too_Long_Words.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(0); 6 | cout.tie(0); 7 | int test; 8 | cin>>test; 9 | for(int i=0; i>s; 12 | if(s.length()<=10){ 13 | cout< 2 | using namespace std; 3 | 4 | const int N = 100; 5 | 6 | bool tachado[N+1]; 7 | vector prime; 8 | 9 | void primeSieve(){ 10 | for(long long i = 2; i < N; i++){ 11 | if(tachado[i] == false){ 12 | prime.push_back(i); 13 | for(long long j = i+i; j < N; j+=i){ 14 | tachado[j] = 1; 15 | } 16 | } 17 | } 18 | } 19 | 20 | int main(){ 21 | primeSieve(); 22 | for(auto i: prime){ 23 | cout << i << endl; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Count Inversion Problem: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getInvCount(int arr[], int n) 5 | { 6 | int count = 0; 7 | for (int i = 0; i < n - 1; i++) 8 | for (int j = i + 1; j < n; j++) 9 | if (arr[i] > arr[j]) 10 | count++; 11 | 12 | return count; 13 | } 14 | 15 | int main() 16 | { 17 | int arr[] = { 1, 20, 6, 4, 5 }; 18 | int n = sizeof(arr) / sizeof(arr[0]); 19 | cout << " Number of inversions are " 20 | << getInvCount(arr, n); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Floyd's triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"enter number of label upto which u want to print ,basically number of rows"<>n; 7 | int count=1; 8 | for(int i=1;i<=n;i++){ 9 | //we are defining one more loop because main work will be done from this basically column work 10 | for(int j=1;j<=i;j++){ 11 | cout< 2 | using namespace std; 3 | 4 | bool check(int n){ 5 | int d1=n%10; 6 | int d2=(n/10)%10; 7 | int d3=(n/100)%10; 8 | int d4=(n/1000)%10; 9 | if(d1!=d2 && d1!=d3 && d1!=d4 && d2!=d3 && d2!=d4 && d3!=d4){ 10 | return true; 11 | } 12 | else{ 13 | return false; 14 | } 15 | } 16 | 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | n++; 21 | while(!check(n)){ 22 | n++; 23 | } 24 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int arr[n]; 9 | for(int i=0; i>arr[i]; 11 | } 12 | int ans[n]; 13 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin>>s; 7 | int u=0,l=0; 8 | for(int i=0; i=97){ 10 | l++; 11 | } 12 | else{ 13 | u++; 14 | } 15 | } 16 | if(u>l){ 17 | transform(s.begin(), s.end(), s.begin(), ::toupper); 18 | } 19 | else{ 20 | transform(s.begin(), s.end(), s.begin(), ::tolower); 21 | } 22 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,a=0,d=0; 6 | cin>>n; 7 | string s; 8 | cin>>s; 9 | for(int i=0; id){ 18 | cout<<"Anton\n"; 19 | } 20 | else if(a 2 | using namespace std; 3 | int main() 4 | { 5 | int i, j, n; 6 | cin >> n; // ‘n’ must be odd 7 | int num1 = n / 2 * 3; 8 | for(i = 0; i < n; i++) 9 | { 10 | for(j = 0; j < n; j++) 11 | { 12 | // center horizontal, center vertical, upper left diagonal, bottom left diagonal, upper right diagonal, bottom right diagonal 13 | if(i == n / 2 || j == n / 2 || i + j == n / 2 || i - j == n / 2 || j - i == n / 2 || i + j == num1) 14 | cout << "*"; 15 | else 16 | cout << " "; 17 | } 18 | cout << "\n"; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /is_palindrom: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool is_palindrom(string str) 4 | { 5 | int start=0; 6 | int end=str.length()-1; 7 | while(start>str; 27 | cout< 2 | typedef long long int ll; 3 | #define M 1000000007 4 | using namespace std; 5 | 6 | ll dp[100005]; 7 | 8 | ll fib(ll n) 9 | { 10 | if (n <= 1) 11 | { 12 | return n; 13 | } 14 | if (dp[n] != -1) 15 | { 16 | return dp[n]; 17 | } 18 | 19 | return dp[n] = fib(n - 1) + fib(n - 2); 20 | } 21 | 22 | int main() 23 | { 24 | ios_base::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | 27 | int n; 28 | cin >> n; 29 | memset(dp, -1, sizeof(dp)); 30 | cout << fib(n) << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Next_Round.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios::sync_with_stdio(false); 6 | cin.tie(0); 7 | cout.tie(0); 8 | int n,k,count=0; 9 | cin>>n>>k; 10 | vector v; 11 | for(int i=0; i>a; 14 | v.push_back(a); 15 | } 16 | int z=v[k-1]; 17 | for(int i=0; i=z && v[i]!=0){ 19 | count++; 20 | } 21 | else{ 22 | break; 23 | } 24 | } 25 | cout< 2 | using namespace std; 3 | bool verifyPrime(int n); 4 | int main() 5 | { 6 | int T, n; 7 | cin>>T; 8 | while(T--) 9 | { 10 | cin>>n; 11 | if (verifyPrime(n)) 12 | cout<<"PRIME"<=c*c) 22 | { 23 | if(n%c==0) 24 | { 25 | return false; 26 | } 27 | c=c+1; 28 | } 29 | return true; 30 | 31 | } -------------------------------------------------------------------------------- /frequency.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string str; 8 | cout<<"Enter a String : "; 9 | getline(cin,str); 10 | char checkCharacter; 11 | cout<<"\nEnter a character to check its frequency in the given string : "; 12 | cin>>checkCharacter; 13 | int count = 0; 14 | 15 | for (int i = 0; i < str.size(); i++) 16 | { 17 | if (str[i] == checkCharacter) 18 | { 19 | ++ count; 20 | } 21 | } 22 | 23 | cout << "Number of " << checkCharacter << " = " << count; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Boy_or_Girl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin>>s; 7 | string s2=""; 8 | for(int i=0; i 3 | using namespace std; 4 | //no of ways 5 | long count_Ways(int n, int k) 6 | { 7 | long dp[n + 1]; 8 | memset(dp, 0, sizeof(dp)); 9 | long long mod = 1000000007; 10 | 11 | dp[1] = k; 12 | dp[2] = k * k; 13 | 14 | for (int i = 3; i <= n; i++) { 15 | dp[i] = ((k - 1) * (dp[i - 1] + dp[i - 2])) % mod; 16 | } 17 | 18 | return dp[n]; 19 | } 20 | 21 | 22 | int main() 23 | { 24 | 25 | int n; 26 | cin>>n; 27 | int k; 28 | cin>>k; 29 | //n is the number of fences and k is the number of colors 30 | cout << count_Ways(n, k) << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Binary_Exponentiation_Recursively.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | #define M 1000000007 4 | using namespace std; 5 | 6 | //This function will help you to calculate x to the power y in O(logn). 7 | 8 | ll power(ll x, ll y, ll m) 9 | { 10 | ll temp; 11 | if (y == 0) 12 | { 13 | return 1; 14 | } 15 | else 16 | { 17 | temp = power(x, y / 2, m); 18 | temp = (temp * temp) % m; 19 | } 20 | if (y % 2 == 1) 21 | { 22 | temp = (temp * x) % m; 23 | } 24 | return temp; 25 | } 26 | 27 | int main() 28 | { 29 | ll x, y; 30 | cin >> x >> y; 31 | cout << power(x, y, M) << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Dyanamic programing/Fibbionaci.c++: -------------------------------------------------------------------------------- 1 | //Fibionacci is a popular sequnece f(n)=f(n-1)+f(n-2) 2 | #include 3 | using namespace std; 4 | int fibbionaci_rec(int n) 5 | { 6 | if(n==0||n==1) 7 | { 8 | return n; 9 | } 10 | return fibbionaci_rec(n-1)+fibbionaci_rec(n-2); 11 | } 12 | 13 | int fibbionaci_bottomup_dp(int n) 14 | { 15 | int dp[n+1]; 16 | dp[0]=0; 17 | dp[1]=1; 18 | for(int i=2;i<=n;i++) 19 | { 20 | dp[i]=dp[i-1]+dp[i-2]; 21 | } 22 | return dp[n]; 23 | } 24 | 25 | int main() 26 | { 27 | int n; 28 | cin>>n; 29 | //recursive fibbionaci sequence 30 | cout< 2 | //inputting required header files 3 | #include 4 | #include 5 | using namespace std; 6 | int main(){ 7 | //two unsorted arrays 8 | int a[2]={6,4}; 9 | int b[3]={8,3,9}; 10 | //implementing sort function and sorting the two arrays 11 | sort(a,a+2); 12 | sort(b,b+3); 13 | int c[5],in=0; 14 | //merging the two arrays 15 | for(int i=0;i<2;i++){ 16 | c[in]=a[i]; 17 | in++; 18 | } 19 | for(int i=0;i<3;i++){ 20 | c[in]=b[i]; 21 | in++; 22 | } 23 | //sorting the merged array and printing 24 | sort(c,c+5); 25 | for(int i=0;i<5;i++){ 26 | cout< 5 | using namespace std; 6 | 7 | // A recursive function to find nth catalan number 8 | long int catalan(int n) 9 | { 10 | // Base case 11 | if (n <= 1) 12 | return 1; 13 | 14 | // catalan(n) is sum (catalan(i)*catalan(n-i-1)) 15 | long int ans = 0; 16 | for (int i=0; i 2 | using namespace std; 3 | int find_frequency(vector arr,int num) 4 | { 5 | int count=0; 6 | for(auto x: arr) 7 | { 8 | if(x==num) 9 | { 10 | count++; 11 | } 12 | } 13 | return count; 14 | } 15 | int main() 16 | { 17 | int size,value,num; 18 | cout<<"please enter array size:-"; 19 | cin>>size; 20 | vector arr; 21 | cout<<"please enter the values"<>value; 25 | arr.push_back(value); 26 | } 27 | cout<<"please enter the number whose frequency is required:-"; 28 | cin>>num; 29 | cout< 3 | using namespace std; 4 | 5 | 6 | int minCoins(int coins[], int m, int V) 7 | { 8 | 9 | if (V == 0) return 0; 10 | 11 | 12 | int res = INT_MAX; 13 | 14 | 15 | for (int i=0; i>m; 35 | int coins[m]; 36 | for(int i=0;i>coins[i]; 39 | } 40 | int V; 41 | cin>>V; 42 | cout << "Minimum coins required is " 43 | << minCoins(coins, m, V); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Codeforces_800_Difficulty/A_Arrival_of_the_General.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(0); 6 | cout.tie(0); 7 | int n; 8 | cin>>n; 9 | vector v(n); 10 | for(auto &it:v){ 11 | cin>>it; 12 | } 13 | int max_index = max_element(v.begin(),v.end()) - v.begin(); 14 | int min_term = *min_element(v.begin(), v.end()); 15 | int min_index = min_element(v.begin(),v.end()) - v.begin(); 16 | for(int i=min_index; imin_index){ 22 | min_index++; 23 | } 24 | cout< 2 | using namespace std; 3 | 4 | 5 | const int CHAR = 256; 6 | bool areanagram(string &s1, string &s2){ 7 | if(s1.length()!= s2.length()){ 8 | return false; 9 | } 10 | else{ 11 | int count[CHAR] = {0}; 12 | for(int i=0;i 8 | using namespace std; 9 | unsigned long long int gcd(unsigned long long int num1,unsigned long long int num2){ 10 | if(num2 == 0){ 11 | return num1; 12 | } 13 | else{ 14 | gcd(num2,num1%num2); 15 | } 16 | } 17 | unsigned long long int lcm(int a,int b){ 18 | unsigned long long int x=gcd(a,b); 19 | unsigned long long int result=((a/x)*(b)); 20 | return result; 21 | } 22 | int main() 23 | { 24 | unsigned long long int n1,n2;cin>>n1>>n2; 25 | cout< 2 | using namespace std; 3 | 4 | int main () 5 | { 6 | int arr[5]; 7 | for (int a = 0; a < 5; a++) 8 | { 9 | cout << "Enter " << a+1 << " element : "; 10 | cin >> arr[a]; 11 | } 12 | cout<<"\nInput list is :\n"; 13 | for(int i=0 ; i<5 ; i++) 14 | { 15 | cout << arr[i] << "\t"; 16 | } 17 | for(int k = 1 ; k < 5 ; k++) 18 | { 19 | int temp = arr[k]; 20 | int j= k-1; 21 | while(j>=0 && temp <= arr[j]) 22 | { 23 | arr[j+1] = arr[j]; 24 | j = j-1; 25 | } 26 | arr[j+1] = temp; 27 | } 28 | cout<<"\n\nSorted list is :\n"; 29 | for(int i = 0 ; i < 5 ; i++) 30 | { 31 | cout << arr[i] << "\t"; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /TSP using DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int graph[][4] = { { 0, 100, 150, 200 }, 6 | { 100, 0, 350, 250 }, 7 | { 150, 350, 0, 300 }, 8 | { 200, 250, 300, 0 } }; 9 | int s = 0, min_path = INT_MAX;; 10 | vector vertex; 11 | for (int i = 0; i < 4; i++) 12 | if (i != s) 13 | vertex.push_back(i); 14 | 15 | while (next_permutation(vertex.begin(), vertex.end())) 16 | { 17 | int current_pathweight = 0, k = s; 18 | for (int i = 0; i < vertex.size(); i++) 19 | { 20 | current_pathweight += graph[k][vertex[i]]; 21 | k = vertex[i]; 22 | } 23 | min_path = min(min_path, (current_pathweight += graph[k][s])); 24 | } 25 | cout <<" Min Path Distance : " < 3 | using namespace std; 4 | int binarysearch(int arr[],int l,int r,int x){ 5 | if( r >= l){ 6 | int mid =l + (r - 1)/2; 7 | 8 | if(arr[mid]==x) 9 | return mid; 10 | 11 | if(arr[mid] > x) 12 | return binarysearch(arr,l,mid -1 ,x); 13 | 14 | return binarysearch(arr,mid+1,r,x); 15 | } 16 | return -1; 17 | } 18 | int main() 19 | { 20 | int a[] = { 2, 3, 4, 10, 40 }; 21 | int x = 10; 22 | int n = sizeof(a) / sizeof(a[0]); 23 | int result = binarysearch(a, 0, n - 1, x); 24 | (result == -1) ? cout << "Element is not present in array" 25 | : cout << "Element is present at index " << result; 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Binary_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int Binary_Search(int arr[],int n,int key) 4 | { 5 | int s =0; 6 | int e = n-1; 7 | while(s<=e) 8 | { 9 | int mid = (s+e)/2; 10 | if(arr[mid]==key) 11 | { 12 | return mid; 13 | } 14 | 15 | else if(arr[mid] > key) 16 | { 17 | e = mid-1; 18 | } 19 | else { 20 | s = mid+1; 21 | } 22 | } 23 | 24 | return -1; 25 | } 26 | 27 | 28 | int main() 29 | { 30 | int n,k; 31 | cout<<"Enter size of array"; 32 | cin>>n; 33 | cout<<"Enter Elements"; 34 | int a[n]; 35 | for(int i=0;i>a[i]; 37 | cout<<"Enter Key"; 38 | cin>>k; 39 | cout< 10 | using namespace std; 11 | 12 | int main(){ 13 | const int N=10001; 14 | int array[N]; 15 | int PrimeNumber; 16 | int j; 17 | 18 | array[0]=2; 19 | array[1]=3; 20 | 21 | for(int i=2;i 2 | using namespace std; 3 | 4 | void Bubblesort(int A[],int n) 5 | { 6 | int pass,i,temp; 7 | int swapped=1; 8 | for(pass=n-1;pass>=0&&swapped;pass--) 9 | { 10 | swapped=0; 11 | for(i=0;i<=pass-1;i++) 12 | { 13 | if (A[i]>A[i+1]) 14 | { 15 | temp=A[i]; 16 | A[i]=A[i+1]; 17 | A[i+1]=temp; 18 | swapped=1; 19 | } 20 | } 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int n; 27 | cout<<"Enter the size of array\n"; 28 | cin>>n; 29 | int arr[n]; 30 | cout<<"Enter the elements of array:"; 31 | for(int i=0;i>arr[i]; 34 | } 35 | Bubblesort(arr,n); 36 | cout<<"Elements after sorting are:"; 37 | for(int i=0;i 3 | using namespace std; 4 | 5 | 6 | bool isSubsetSum(int set[], int n, int sum) 7 | { 8 | bool subset[n + 1][sum + 1]; 9 | 10 | 11 | for (int i = 0; i <= n; i++) 12 | subset[i][0] = true; 13 | for (int i = 1; i <= sum; i++) 14 | subset[0][i] = false; 15 | 16 | for (int i = 1; i <= n; i++) { 17 | for (int j = 1; j <= sum; j++) { 18 | if (j < set[i - 1]) 19 | subset[i][j] = subset[i - 1][j]; 20 | if (j >= set[i - 1]) 21 | subset[i][j] = subset[i - 1][j] 22 | || subset[i - 1][j - set[i - 1]]; 23 | } 24 | } 25 | 26 | return subset[n][sum]; 27 | } 28 | int main(){ 29 | 30 | int set[] = { 3, 34, 4, 12, 5, 2 }; 31 | int sum = 9; 32 | int n = sizeof(set) / sizeof(set[0]); 33 | if (isSubsetSum(set, n, sum) == true) 34 | cout <<"Found a subset with given sum"; 35 | else 36 | cout <<"No subset with given sum"; 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /0-1_Knapsack.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Memoization 3 | 4 | #include 5 | 6 | using namespace std; 7 | int knapsackrec(int W,int w[],int v[],int i,int **dp) 8 | { 9 | if(i<0) return 0; 10 | if(dp[i][W]!=-1) return dp[i][W]; 11 | if (w[i]>W) return dp[i][W]= knapsackrec(W,w,v,i-1,dp); 12 | else { 13 | 14 | dp[i][W]= max(v[i]+knapsackrec(W-w[i],w,v,i-1,dp),knapsackrec(W,w,v,i-1,dp)); 15 | } 16 | return dp[i][W]; 17 | } 18 | 19 | int knapsack(int W,int w[],int v[],int n) 20 | { 21 | int **dp; 22 | dp= new int*[n]; 23 | for(int i=0;i 2 | using namespace std; 3 | void dnf_sort(int *arr ,int n) 4 | { 5 | int low =0; 6 | int mid =0; 7 | int high =n-1; 8 | while(mid <= high) 9 | { 10 | if(arr[mid]==0) 11 | { 12 | swap(arr[low],arr[mid]); 13 | low++; 14 | mid++; 15 | } 16 | else{ 17 | if(arr[mid]==1) 18 | { 19 | mid++; 20 | } 21 | else{ 22 | swap(arr[mid],arr[high]); 23 | high--; 24 | } 25 | } 26 | 27 | } 28 | } 29 | void swap(int *arr ,int i,int j) 30 | { 31 | int temp = arr[i]; 32 | arr[i] = arr[j]; 33 | arr[j] = temp; 34 | } 35 | 36 | int main() 37 | { 38 | int arr[] ={1,0,2,2,1,0,0}; 39 | dnf_sort(arr,7); 40 | 41 | for(int i=0;i<7;i++) 42 | { 43 | cout< 34 | using namespace std; 35 | int main() 36 | { 37 | int arr[] ={1,0,2,2,1,0,0}; 38 | dnf_sort(*arr,7); 39 | 40 | for(int i=0;i<7;i++) 41 | { 42 | cout< 4 | using namespace std; 5 | 6 | int magic(int n) //calculating the sum of each digit 7 | { 8 | int s,r; 9 | while(n>9) // loop is running untill we get a single digit sum 10 | { 11 | s=0; 12 | while(n!=0) 13 | { 14 | r=n%10; 15 | s=s+r; 16 | n=n/10; 17 | } 18 | n=s; 19 | } 20 | 21 | return s; 22 | } 23 | 24 | int main() { 25 | 26 | int n,sum; 27 | cout<<"Enter the number to be checked : "; 28 | cin>>n; 29 | if(n>9) 30 | sum=magic(n); // sum is returned to the main method 31 | else 32 | sum=n; 33 | if(sum==1) // sum is then compared with 1 34 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; 6 | cout<<"enter the number of row="; 7 | cin>>r; 8 | cout<<"enter the number of column="; 9 | cin>>c; 10 | cout<<"enter the first matrix element=\n"; 11 | for(i=0;i>a[i][j]; 16 | } 17 | } 18 | cout<<"enter the second matrix element=\n"; 19 | for(i=0;i>b[i][j]; 24 | } 25 | } 26 | cout<<"multiply of the matrix=\n"; 27 | for(i=0;i 4 | using namespace std; 5 | 6 | void selectionsort(int arr[],int n); 7 | 8 | //Program By Siddharth Bhamare 9 | 10 | int main() 11 | { 12 | 13 | int i=0,n; 14 | cout<<"Enter size of Array :"; 15 | cin >> n ; 16 | int arr[n]; 17 | cout<<"Enter elements of Array : "; 18 | while(i>arr[i]; 21 | i++; 22 | } 23 | selectionsort(arr,sizeof(arr)/sizeof(int)); 24 | return 0; 25 | } 26 | 27 | void selectionsort(int arr[],int n) 28 | { 29 | int i,j,temp,min; 30 | 31 | for(i=0;i 2 | using namespace std; 3 | int LCS(string str1, string str2) 4 | { 5 | int len1 = str1.size(); 6 | int len2 = str2.size(); 7 | int dp[len1 + 1][len2 + 1]; 8 | for (int i = 0; i <= len1; i++) 9 | { 10 | for (int j = 0; j <= len2; j++) 11 | { 12 | if (i == 0 || j == 0) 13 | { 14 | dp[i][j] = 0; 15 | } 16 | else if (str1[len1 - i] == str2[len2 - j]) 17 | { 18 | dp[i][j] = 1 + dp[i - 1][j - 1]; / 19 | } 20 | else 21 | { 22 | int a = dp[i - 1][j]; 23 | int b = dp[i][j - 1]; 24 | int c = dp[i - 1][j - 1]; 25 | dp[i][j] = max(a, max(b, c)); 26 | } 27 | } 28 | } 29 | return dp[len1][len2]; 30 | } 31 | int main() 32 | { 33 | string str1, str2; 34 | cout<< "String 1:"; 35 | cin >> str1; 36 | cout<< "String 2:"; 37 | cin >> str2; 38 | cout << "LCS of " << str1 << " and " << str2 << " is " << LCS(str1, str2) << endl; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Dyanamic programing/unbounded_knapsack.c++: -------------------------------------------------------------------------------- 1 | //this is a same probelem as 0-1 knapsack but here we can choose item any number of time 2 | //the famous problem is :Given a knapsack weight W and a set of n items with certain value vali and weight wti, we need to calculate the maximum amount that could make up this quantity exactly. 3 | /* W = 8 4 | val[] = {10, 40, 50, 70} 5 | wt[] = {1, 3, 4, 5} 6 | ans-> 110*/ 7 | 8 | #include 9 | using namespace std; 10 | 11 | 12 | int unboundedKnapsack(int W, int n, 13 | int val[], int wt[]) 14 | { 15 | 16 | int dp[W+1]; 17 | memset(dp, 0, sizeof dp); 18 | 19 | 20 | for (int i=0; i<=W; i++) 21 | for (int j=0; j>n; 32 | int val[n]; 33 | int wt[n]; 34 | for(int i=0;i>val[i]; 37 | } 38 | for(int i=0;i>wt[i]; 41 | } 42 | 43 | int W; 44 | cin>>W; 45 | cout << unboundedKnapsack(W, n, val, wt); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Armstrong.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to calculate x raised to the power y // 5 | int power(int x, unsigned int y) 6 | { 7 | if( y == 0) 8 | return 1; 9 | if (y%2 == 0) 10 | return power(x, y/2)*power(x, y/2); 11 | return x*power(x, y/2)*power(x, y/2); 12 | } 13 | 14 | // Function to calculate order of the number // 15 | int order(int x) 16 | { 17 | int n = 0; 18 | while (x) 19 | { 20 | n++; 21 | x = x/10; 22 | } 23 | return n; 24 | } 25 | 26 | // Function to check whether the given number is 27 | // Armstrong number or not 28 | bool isArmstrong(int x) 29 | { 30 | // Calling order function 31 | int n = order(x); 32 | int temp = x, sum = 0; 33 | while (temp) 34 | { 35 | int rem = temp%10; 36 | sum += power(rem, n); 37 | temp = temp/10; 38 | } 39 | 40 | // If satisfies Armstrong condition 41 | return (sum == x); 42 | } 43 | 44 | 45 | int main() 46 | { 47 | int x = 407; 48 | cout << isArmstrong(x) << endl; 49 | x = 1542; 50 | cout << isArmstrong(x) << endl; 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /LinearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Function for searching the Element present in Array or not. 5 | void LinearSearch(int a[],int m,int n) { 6 | int temp = -1; 7 | 8 | for (int i = 0; i >m; 25 | //Entering the elements of Array. 26 | cout << "Please enter " <> arr[i]; 29 | } 30 | //taking input number for searching. 31 | cout << "Please enter an element to search" << endl; 32 | int num; 33 | cin >> num; 34 | /*Here we are calling the Function which will perform searching 35 | function and return the index where number is present. 36 | Here we pass Three Arguments first one is array itself second is size of the array and last one is the element 37 | which is to be searched*/ 38 | LinearSearch(arr,m,num); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Dyanamic programing/Readme.md: -------------------------------------------------------------------------------- 1 |

Dyamamic Programming

2 |

-----------------------

3 |

Dyanamic Programming is a technique which we can use memorisation over recursion which store the subproblem answer to compute next problem of same sub problem

4 | 5 |

Dp Problem List

6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 |
0-1 KnapSackRecursion+Bottomup Dp dp✔️
FibionacciRecursion+Bottomup Dp ✔️
Unbounded KnpasackRecursion+Bottomup Dp✔️
Minimum no of CoinRecursion+Bottomup Dp✔️
Subset SumRecursion+Bottomup Dp✔️
Painting Fence AlgoRecursion+Bottomup Dp✔️
Longest Common SubstringRecursion +Bottomup Dp✔️
-------------------------------------------------------------------------------- /Permutation_Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string getPermutation(int n, int k) { 7 | vector nums; 8 | int fact = 1; 9 | 10 | for(int i = 1; i < n; i++) 11 | { 12 | fact = fact * i; 13 | nums.push_back(i); 14 | } 15 | nums.push_back(n); 16 | 17 | string ans = ""; 18 | k = k - 1; 19 | 20 | while(true) 21 | { 22 | ans = ans + to_string(nums[k / fact]); 23 | 24 | nums.erase(nums.begin() + k / fact); 25 | 26 | if(nums.size() == 0) 27 | break; 28 | 29 | k = k % fact; 30 | 31 | fact = fact / nums.size(); 32 | } 33 | return ans; 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | int n, k; 40 | 41 | cout<<"Enter a number here :- "; 42 | cin>>n; 43 | 44 | cout<<"Enter the value of K :- "; 45 | cin>>k; 46 | 47 | Solution obj; 48 | 49 | string ans = obj.getPermutation(n, k); 50 | 51 | cout< 4 | using namespace std; 5 | struct node{ 6 | int data; 7 | node *ptr,*pre; 8 | }; 9 | node *head=NULL; 10 | void insertathead(int a){ 11 | node *p=new node; 12 | p->data=a; 13 | p->pre=p->ptr=NULL; 14 | if(head==NULL){ 15 | head=p; 16 | } 17 | else{ 18 | head->pre=p; 19 | p->ptr=head; 20 | head=p; 21 | } 22 | } 23 | void deleteinbetween(int a){ 24 | node *p=head; 25 | while(p && p->data!=a){ 26 | p=p->ptr; 27 | } 28 | if(p->ptr==NULL) 29 | cout<<"NOT FOUND"; 30 | else{ 31 | p->pre->ptr=p->ptr; 32 | p->ptr->pre=p->pre; 33 | } 34 | p=NULL; 35 | } 36 | void displayfromhead(){ 37 | node *p=head; 38 | while(p!=NULL){ 39 | cout<data<ptr; 41 | } 42 | } 43 | int main(){ 44 | insertathead(40); 45 | insertathead(30); 46 | insertathead(20); 47 | insertathead(10); 48 | insertathead(5); 49 | cout<<"BEFORE DELETION\n"; 50 | displayfromhead(); 51 | deleteinbetween(30); 52 | cout<<"AFTER DELETION in between\n"; 53 | displayfromhead(); 54 | return 0; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Graph/BFSByMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define forall(i,a,b) for(int i=a;i> adjm){ 6 | bool visited[n+1]; 7 | 8 | forall(i,0,n+1){ 9 | visited[i]=0; 10 | } 11 | queue q; 12 | q.push(startEle); 13 | cout<>n>>m; 29 | vector>adjm(n+1,vector(n+1,0)); 30 | // 6 6 31 | // 1 2 32 | // 1 3 33 | // 2 4 34 | // 3 4 35 | // 4 5 36 | // 4 6 37 | // 1 38 | forall(i,0,m){ 39 | int x,y; 40 | cin>>x>>y; 41 | adjm[x][y]=1; 42 | adjm[y][x]=1; 43 | } 44 | int startEle; 45 | cin>>startEle; 46 | BFS(startEle,n,adjm); 47 | // forall(i,1,n+1){ 48 | // forall(j,1,n+1){ 49 | // cout< 2 | using namespace std; 3 | 4 | const int INF = 1000000000; 5 | vector>> adj; 6 | 7 | void dijkstra(int s, vector &d, vector &p) 8 | { 9 | int n = adj.size(); 10 | d.assign(n, INF); 11 | p.assign(n, -1); 12 | vector u(n, false); 13 | 14 | d[s] = 0; 15 | for (int i = 0; i < n; i++) 16 | { 17 | int v = -1; 18 | for (int j = 0; j < n; j++) 19 | { 20 | if (!u[j] && (v == -1 || d[j] < d[v])) 21 | v = j; 22 | } 23 | 24 | if (d[v] == INF) 25 | break; 26 | 27 | u[v] = true; 28 | for (auto edge : adj[v]) 29 | { 30 | int to = edge.first; 31 | int len = edge.second; 32 | 33 | if (d[v] + len < d[to]) 34 | { 35 | d[to] = d[v] + len; 36 | p[to] = v; 37 | } 38 | } 39 | } 40 | } 41 | 42 | int main() 43 | { 44 | int n, m, a, b, w; 45 | cin >> n >> m; 46 | 47 | while (m--) 48 | { 49 | cin >> a >> b >> w; 50 | adj[a].push_back({b, w}); 51 | adj[b].push_back({a, w}); 52 | } 53 | 54 | vector d, p; 55 | dijkstra(1, d, p); 56 | 57 | for (int i = 1; i <= n; i++) 58 | { 59 | cout << d[i] << " "; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /TowerOfHanoi.cpp: -------------------------------------------------------------------------------- 1 | // Tower of Hanoi is a mathematical puzzle in which we have three rods and n disks. 2 | // The objective of the puzzle is to move the entire stack to another rod, obeying the following rules: 3 | // * Only one disk can be moved at a time. 4 | // * Each move consists of taking the upper disk (top most) from one of the stacks and placing it on top of another stack. 5 | // * larger disk cannot be placed on top of a smaller disk. 6 | 7 | #include 8 | using namespace std; 9 | 10 | void TOH(int x,char a ,char c,char b) 11 | { 12 | if(x==1) 13 | { cout<<"Move the disc 1 from "<>x; 36 | 37 | TOH(x,a,c,b); 38 | 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /stack_implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 100 3 | using namespace std; 4 | int stk[max],top=-1; 5 | void push() 6 | { 7 | int x; 8 | if(top>max) 9 | { 10 | cout<<"Overflow Condition\n"; 11 | return; 12 | } 13 | top++; 14 | cout<<"Enter the element\n"; 15 | cin>>x; 16 | stk[top]=x; 17 | } 18 | void pop() 19 | { 20 | if(top<0) 21 | { 22 | cout<<"Stack is empty\n"; 23 | return; 24 | } 25 | cout<<"Pop element:"; 26 | cout<>op; 62 | switch(op) 63 | { 64 | case 1:push(); 65 | break; 66 | case 2:pop(); 67 | break; 68 | case 3:peep(); 69 | break; 70 | case 4:size(); 71 | break; 72 | case 0:cout<<"BYE \n"; 73 | break; 74 | default: 75 | cout<<"Invalid option\n"; 76 | } 77 | }while(op!=0); 78 | } 79 | -------------------------------------------------------------------------------- /sliding_window.cpp: -------------------------------------------------------------------------------- 1 | //Given an array of integers Arr of size N and a number K. Return the maximum sum of a subarray of size K. 2 | 3 | /*Instead of applying brute-force logic of nested for loop we will use one intelligent algorithm named "Window-sliding Technique" 4 | The main idea is to convert two nested loops into a single loop, hence time-complexity of this code is reduced from O(n^2) to O(n) 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | // Returns maximum sum in a subarray of size k. 11 | int maxSum(int arr[], int n, int k) 12 | { 13 | // size of the subarray should not exceed length of the original array 14 | if (n < k) { 15 | cout << "Invalid"; 16 | return -1; 17 | } 18 | // loop to find sum of first window of size k. 19 | int max_sum = 0; 20 | for (int i = 0; i < k; i++) 21 | max_sum += arr[i]; 22 | 23 | /* Here is the main modification of brute-force approach. We will compute sums of remaining windows by 24 | removing first element of previous window and adding last element of current window.*/ 25 | 26 | int window_sum = max_sum; 27 | for (int i = k; i < n; i++) { 28 | window_sum += arr[i] - arr[i - k]; 29 | max_sum = max(max_sum, window_sum); 30 | } 31 | return max_sum; 32 | } 33 | 34 | int main() 35 | { 36 | int arr[] = {12,28,45,90,10,68,27,8,18,20,34,74}; 37 | int k = 4; 38 | int n = sizeof(arr) / sizeof(arr[0]); 39 | cout << maxSum(arr, n, k); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /transposeMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int arr[10][10], transpose[10][10], row, column, i, j; 6 | 7 | cout << "Enter number of rows and columns of matrix: "; 8 | cin >> row >> column; 9 | 10 | cout << "\nEnter elements of matrix: " << endl; 11 | 12 | // Store the matrix elements 13 | for (int i = 0; i < row; ++i) 14 | { 15 | for (int j = 0; j < column; ++j) 16 | { 17 | cout << "Enter element, a" << i + 1 << j + 1 << ": "; //print as a11,a12,etc. 18 | cin >> arr[i][j]; 19 | } 20 | } 21 | 22 | // Printing the matrix taken as input 23 | cout << "\nEntered Matrix: " << endl; 24 | for (int i = 0; i < row; ++i) { 25 | for (int j = 0; j < column; ++j) { 26 | cout << " " << arr[i][j]; 27 | if (j == column - 1) 28 | cout << endl << endl; 29 | } 30 | } 31 | 32 | // find out transpose of the matrix 33 | for (int i = 0; i < row; ++i) 34 | for (int j = 0; j < column; ++j) { 35 | transpose[j][i] = arr[i][j]; 36 | } 37 | 38 | // Printing the transpose matrix 39 | cout << "\nTranspose of Matrix: " << endl; 40 | for (int i = 0; i < column; ++i) 41 | for (int j = 0; j < row; ++j) { 42 | cout << " " << transpose[i][j]; 43 | if (j == row - 1) 44 | cout << endl << endl; 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Heap_sort.cpp: -------------------------------------------------------------------------------- 1 | //Implementation of Heap Sort 2 | 3 | #include 4 | using namespace std; 5 | 6 | //Program by Siddharth Bhamare 7 | 8 | void convertHeap(int arr[], int len, int x) 9 | { 10 | int largest = x; 11 | int left = 2 * x + 1; 12 | int right = 2 * x + 2; 13 | 14 | if (left < len && arr[left] > arr[largest]) 15 | largest = left; 16 | 17 | if (right < len && arr[right] > arr[largest]) 18 | largest = right; 19 | 20 | if (largest != x) 21 | { 22 | swap(arr[x], arr[largest]); 23 | 24 | convertHeap(arr, len, largest); 25 | } 26 | } 27 | 28 | void HeapSort(int arr[], int len) 29 | { 30 | for (int i = len / 2 - 1; i >= 0; i--) 31 | convertHeap(arr, len, i); 32 | 33 | for (int i = len - 1; i >= 0; i--) 34 | { 35 | swap(arr[0], arr[i]); 36 | convertHeap(arr, i, 0); 37 | } 38 | } 39 | 40 | void show(int arr[], int len) 41 | { 42 | cout << "\nSorted Array is : "; 43 | for (int i = 0; i < len; ++i) 44 | cout << arr[i] << " "; 45 | cout << endl; 46 | } 47 | 48 | int main() 49 | { 50 | int i=0,n; 51 | cout <<"Enter size of Array : "; 52 | cin >>n; 53 | int arr[n]; 54 | cout <<"Enter elements of Array : "; 55 | while(i>arr[i]; 58 | i++; 59 | } 60 | int m = sizeof(arr) / sizeof(int); 61 | 62 | HeapSort(arr, m); 63 | show(arr, m); 64 | } -------------------------------------------------------------------------------- /nQueen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int chess[11][11]; 5 | 6 | // checking if is it right place to put queen there? 7 | 8 | bool isPossible(int n,int row,int col){ 9 | // same column 10 | for(int i=row-1;i>=0;i--) 11 | { if(chess[i][col]==1) 12 | { 13 | return false; 14 | } 15 | } 16 | //upper left diagonal 17 | for(int i=row-1,j=col-1;i>=0 && j>=0; i--,j-- ){ 18 | if(chess[i][j]==1){ 19 | return false; 20 | } 21 | } 22 | //Upper right Diagonal 23 | for(int i=row-1,j=col+1;i>=0&&j 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int dp[102][102]; 8 | /* 9 | The possible operations are permitted: 10 | Insert a character at any position of the string. 11 | Remove any character from the string. 12 | Replace any character from the string with any other character. 13 | */ 14 | int solve(string s1, string s2) 15 | { 16 | for (int i = 0; i <= s1.size(); i++) 17 | { 18 | for (int j = 0; j <= s2.size(); j++) 19 | { 20 | if (i == 0) 21 | dp[i][j] = j; 22 | else if (j == 0) 23 | dp[i][j] = i; 24 | else if (s1[i - 1] == s2[j - 1]) 25 | dp[i][j] = dp[i - 1][j - 1]; 26 | else 27 | dp[i][j] = 1 + min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])); 28 | } 29 | } 30 | return dp[s1.size()][s2.size()]; 31 | } 32 | int editDistance(string s, string t) 33 | { 34 | 35 | return solve(s, t); 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | //calculate minimum number of operations required to convert s to t. 42 | string s, t; 43 | cin >> s >> t; 44 | Solution ob; 45 | int ans = ob.editDistance(s, t); 46 | cout << ans << "\n"; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /shell_sort.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of Shell Sort 2 | #include 3 | using namespace std; 4 | 5 | /* function to sort arr using shellSort */ 6 | int shellSort(int arr[], int n) 7 | { 8 | // Start with a big gap, then reduce the gap 9 | for (int gap = n/2; gap > 0; gap /= 2) 10 | { 11 | // Do a gapped insertion sort for this gap size. 12 | // The first gap elements a[0..gap-1] are already in gapped order 13 | // keep adding one more element until the entire array is 14 | // gap sorted 15 | for (int i = gap; i < n; i += 1) 16 | { 17 | // add a[i] to the elements that have been gap sorted 18 | // save a[i] in temp and make a hole at position i 19 | int temp = arr[i]; 20 | 21 | // shift earlier gap-sorted elements up until the correct 22 | // location for a[i] is found 23 | int j; 24 | for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) 25 | arr[j] = arr[j - gap]; 26 | 27 | // put temp (the original a[i]) in its correct location 28 | arr[j] = temp; 29 | } 30 | } 31 | return 0; 32 | } 33 | 34 | void printArray(int arr[], int n) 35 | { 36 | for (int i=0; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a[100],size,f,i,j,k,l,p=0,m=1; 7 | cout<<"Enter size of array : "; 8 | cin>>size; 9 | cout<<"Enter the elements of array : "; 10 | for(i=0;i>a[i]; } 12 | l=a[0]; 13 | 14 | //Loop to store the largest element in l 15 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a[100],size,f,i,j,k,l,p=0,m=1; 7 | cout<<"Enter size of array : "; 8 | cin>>size; 9 | cout<<"Enter the elements of array : "; 10 | for(i=0;i>a[i]; } 12 | l=a[0]; 13 | 14 | //Loop to store the largest element in l 15 | for(i=0;i 11 | using namespace std; 12 | int main() { 13 | int q; 14 | cin>>q; 15 | string s;string r,e; 16 | stack st; 17 | while(q--) 18 | { 19 | int t; 20 | cin>>t; 21 | st.push(t); 22 | if(t==1) 23 | { 24 | cin>>r; 25 | s=s+r; 26 | } 27 | else if (t==2) { 28 | int k; 29 | cin>>k; 30 | for(int i=s.length()-1;i>=s.length()-k;i--) 31 | {e=s[i]+e; 32 | s[i]='\0'; 33 | } 34 | } 35 | else if(t==3) 36 | { 37 | int l; 38 | cin>>l; 39 | cout<=s.length()-r.length();i--) 45 | { 46 | s[i]='\0'; 47 | } 48 | st.pop(); 49 | } 50 | else if(st.top()==2) 51 | { 52 | s=s+e; 53 | st.pop(); 54 | } 55 | } 56 | 57 | } 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Find Original Array From Doubled Array.cpp: -------------------------------------------------------------------------------- 1 | //KHUSHBOO DEV 2 | 3 | // An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, 4 | // and then randomly shuffling the resulting array. Find the original array. 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | // function to find the original array. 11 | 12 | vector findOriginalArray(vector& changed) { 13 | int n=changed.size(); 14 | vectorans; 15 | if(n%2==1) 16 | return ans; 17 | sort(changed.begin(),changed.end()); 18 | unordered_mapm; 19 | for(int i=0;ichanged={1,3,4,2,6,8}; //given vector of integers 48 | 49 | vectororiginal=findOriginalArray(changed);//function to find the original array. 50 | 51 | for(int i=0;i 5 | using namespace std; 6 | #include 7 | 8 | void sum(int count, ...) 9 | { 10 | int sum=0; 11 | va_list args; 12 | va_start(args, count); 13 | for(int i=0; imaxima) 44 | maxima=max; 45 | } 46 | va_end( args ); 47 | cout<<"\nMaximum value: "< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node* next; 8 | 9 | node(int val){ 10 | data=val; 11 | next=NULL; 12 | } 13 | 14 | 15 | }; 16 | void insertAttail(node* &head,int val){ 17 | node* n=new node(val); 18 | if(head==NULL){ 19 | head=n; 20 | return; 21 | } 22 | node* temp=head; 23 | 24 | while (temp!=NULL) 25 | { 26 | temp=temp->next; 27 | } 28 | temp->next=n; 29 | 30 | } 31 | void display(node* head){ 32 | node* temp=head; 33 | while (temp!=NULL) 34 | { 35 | cout<data<<" "; 36 | temp=temp->next; 37 | } 38 | 39 | } 40 | node* reverseknode(node* &head,int k){ 41 | node* prevptr=NULL; 42 | node* currptr=head; 43 | node* nextptr=head->next; 44 | int count =0; 45 | while (currptr!=NULL && countnext; 48 | currptr->next=prevptr; 49 | prevptr=currptr; 50 | currptr=nextptr; 51 | count++; 52 | } 53 | 54 | if (nextptr!=NULL) 55 | { 56 | head->next=reverseknode(nextptr,k); 57 | } 58 | return prevptr; 59 | 60 | } 61 | int main(){ 62 | node* head=NULL; 63 | insertAttail(head,1); 64 | insertAttail(head,2); 65 | insertAttail(head,3); 66 | insertAttail(head,5); 67 | display(head); 68 | cout< 2 | using namespace std; 3 | 4 | class node 5 | { 6 | public: 7 | int data; 8 | node *next; 9 | 10 | node(int val) 11 | { 12 | data = val; 13 | next = NULL; 14 | } 15 | }; 16 | void insertAthead(node *&head, int val) 17 | { 18 | node *n = new node(val); 19 | n->next = head; 20 | head = n; 21 | } 22 | void insertAtTail(node *&head, int val) 23 | { 24 | node *n = new node(val); 25 | if (head == NULL) 26 | { 27 | head = n; 28 | return; 29 | } 30 | 31 | node *temp = head; 32 | while (temp->next != NULL) 33 | { 34 | temp = temp->next; 35 | } 36 | temp->next = n; 37 | return; 38 | } 39 | 40 | 41 | void display(node *head) 42 | { 43 | node *temp = head; 44 | { 45 | while (temp != NULL) 46 | { 47 | cout << temp->data << " "; 48 | temp = temp->next; 49 | } 50 | cout << "NULL" << endl; 51 | } 52 | } 53 | bool search(node *head, int key) 54 | { 55 | node *temp = head; 56 | while (temp != NULL) 57 | { 58 | if (temp->data == key) 59 | { 60 | return true; 61 | } 62 | temp = temp->next; 63 | } 64 | return false; 65 | } 66 | 67 | int main() 68 | { 69 | 70 | insertAtTail(head, 1);node *head = NULL; 71 | insertAtTail(head, 2); 72 | insertAtTail(head, 3); 73 | display(head); 74 | insertAthead(head, 5); 75 | display(head); 76 | cout << search(head, 5); 77 | return 0; 78 | } -------------------------------------------------------------------------------- /merge_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int arr[], int l, int m, int r) 5 | { 6 | int n1 = m - l + 1; 7 | int n2 = r - m; 8 | 9 | int L[n1], R[n2]; 10 | 11 | 12 | for (int i = 0; i < n1; i++) 13 | L[i] = arr[l + i]; 14 | for (int j = 0; j < n2; j++) 15 | R[j] = arr[m + 1 + j]; 16 | 17 | 18 | int i = 0; 19 | int j = 0; 20 | int k = l; 21 | 22 | while (i < n1 && j < n2) { 23 | if (L[i] <= R[j]) { 24 | arr[k] = L[i]; 25 | i++; 26 | } 27 | else { 28 | arr[k] = R[j]; 29 | j++; 30 | } 31 | k++; 32 | } 33 | 34 | while (i < n1) { 35 | arr[k] = L[i]; 36 | i++; 37 | k++; 38 | } 39 | 40 | while (j < n2) { 41 | arr[k] = R[j]; 42 | j++; 43 | k++; 44 | } 45 | } 46 | 47 | void mergeSort(int arr[],int l,int r){ 48 | if(l>=r){ 49 | return; 50 | } 51 | int m = (l+r-1)/2; 52 | mergeSort(arr,l,m); 53 | mergeSort(arr,m+1,r); 54 | merge(arr,l,m,r); 55 | } 56 | 57 | void printArray(int A[], int size) 58 | { 59 | for (int i = 0; i < size; i++) 60 | cout << A[i] << " "; 61 | } 62 | 63 | int main() 64 | { 65 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 66 | int arr_size = sizeof(arr) / sizeof(arr[0]); 67 | 68 | cout << "Given array is \n"; 69 | printArray(arr, arr_size); 70 | 71 | mergeSort(arr, 0, arr_size - 1); 72 | 73 | cout << "\nSorted array is \n"; 74 | printArray(arr, arr_size); 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Pancake_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool isSorted(vector& arr) 4 | { 5 | if(arr.size()==0 || arr.size()==1) 6 | { 7 | return 1; 8 | } 9 | for(int i=1;iarr[i]) 12 | { 13 | return 0; 14 | } 15 | } 16 | return 1; 17 | } 18 | void flip(vector& arr,int k) 19 | { 20 | int i=0,j=k-1; 21 | while(i pancakeSort(vector& arr) { 31 | vector res; 32 | int k=0,j=0,index=-1,large=INT_MIN,n=arr.size(); 33 | while(!isSorted(arr)) 34 | { 35 | for(int i=0;i>n; 61 | vector v; 62 | cout<<"Enter elements in array : "; 63 | for(int i=0;i>x; 67 | v.push_back(x); 68 | } 69 | pancakeSort(v); 70 | cout<<"\nSorted array : "; 71 | for(auto u: v) 72 | { 73 | cout< 2 | using namespace std; 3 | 4 | class node 5 | { 6 | public: 7 | int data; 8 | node *next; 9 | node(int val) 10 | { 11 | data = val; 12 | next = NULL; 13 | } 14 | }; 15 | void insertAttail(node *&head, int val) 16 | { 17 | node *n = new node(val); 18 | if (head == NULL) 19 | { 20 | head = n; 21 | return; 22 | } 23 | node *temp = head; 24 | 25 | while (temp != NULL) 26 | { 27 | temp = temp->next; 28 | } 29 | temp->next = n; 30 | return; 31 | } 32 | void insertAthead(node *&head, int val) 33 | { 34 | node *n = new node(val); 35 | n->next = head; 36 | head = n; 37 | } 38 | void display(node *head) 39 | { 40 | node *temp = head; 41 | while (temp != NULL) 42 | { 43 | cout << temp->data << " "; 44 | temp = temp->next; 45 | } 46 | } 47 | void evenAfterOdd(node *&head) 48 | { 49 | node *odd = head; 50 | node *even = head->next; 51 | node *evenStart = even; 52 | while (odd->next != NULL && even->next != NULL) 53 | { 54 | odd->next = even->next; 55 | odd = odd->next; 56 | even->next = odd->next; 57 | even = even->next; 58 | } 59 | odd->next = evenStart; 60 | if (odd->next = NULL) 61 | { 62 | even->next = NULL; 63 | } 64 | } 65 | int main() 66 | { 67 | node *head = NULL; 68 | insertAttail(head, 1); 69 | insertAttail(head, 2); 70 | insertAttail(head, 3); 71 | insertAttail(head, 4); 72 | insertAttail(head, 5); 73 | insertAttail(head, 6); 74 | display(head); 75 | evenAfterOdd(head); 76 | display(head); 77 | return 0; 78 | } -------------------------------------------------------------------------------- /Linked list/IntersectionOfLinkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | using namespace std; 5 | 6 | class node{ 7 | public: 8 | int data; 9 | node* next; 10 | node(int val){ 11 | data=val; 12 | next=NULL; 13 | } 14 | }; 15 | void insertAttail(node* &head,int val){ 16 | node* n=new node(val); 17 | if(head==NULL){ 18 | head=n; 19 | return; 20 | } 21 | node* temp=head; 22 | 23 | while (temp!=NULL) 24 | { 25 | temp=temp->next; 26 | } 27 | temp->next=n; 28 | return; 29 | 30 | } 31 | void insertAthead(node* &head,int val){ 32 | node* n=new node(val); 33 | n->next=head; 34 | head=n; 35 | 36 | } 37 | void display(node* head){ 38 | node* temp=head; 39 | while (temp!=NULL) 40 | { 41 | cout<data<<" "; 42 | temp=temp->next; 43 | } 44 | 45 | } 46 | int length(node* &head){ 47 | node* temp=head; 48 | int l=0; 49 | while (temp!=NULL) 50 | { 51 | l++; 52 | temp=temp->next; 53 | } 54 | return l; 55 | 56 | } 57 | bool intersect(node* &head1,node* &head2){; 58 | node* ptr1; 59 | node* temp1=head1; 60 | node* temp2=head2; 61 | node* ptr2; 62 | 63 | while(temp1!=NULL){ 64 | 65 | } 66 | int l1=length(head1); 67 | int l2=length(head2); 68 | int d=0; 69 | int l=max(l1,l2) 70 | if(l1>l2){ 71 | d=l1-l2; 72 | ptr1=head1; 73 | ptr2=head2; 74 | } 75 | else{ 76 | d=l2-l1; 77 | ptr1=head2; 78 | ptr2=head1; 79 | } 80 | while (d!=0)) 81 | { 82 | ptr1= 83 | } 84 | 85 | } 86 | } -------------------------------------------------------------------------------- /QuickSort.cpp: -------------------------------------------------------------------------------- 1 | //QuickSort is a Divide and Conquer algorithm. 2 | //Comments in the code: if pivot was taken as last element(the changes that need to be done, in the code.) 3 | //This code is for pivot,taken as starting element. 4 | 5 | /* 6 | Time Complexity(Best Case): O(n*logn) 7 | Time Complexity(Worst Case): O(n^2) 8 | Space Complexity: O(log n) 9 | */ 10 | #include 11 | 12 | using namespace std; 13 | void swap(int &x,int &y) 14 | { 15 | int t=x; 16 | x=y; 17 | y=t; 18 | } 19 | void Display(int a[],int n) 20 | { 21 | for (int i = 0; i < n; i++) 22 | { 23 | cout << "a[" << i << "]:" << a[i] << endl; 24 | } 25 | cout << endl; 26 | } 27 | 28 | //Partition of Arrays Logic 29 | int Partition(int a[],int l,int h) 30 | { 31 | int pivot=a[l]; //int pivot=a[h]; 32 | int i=l,j=h; 33 | while(ipivot) //(a[j]>=pivot) 38 | j--; 39 | if(i>n; 62 | cout<<"Enter elements"<>a[i]; 68 | } 69 | cout < 2 | using namespace std; 3 | #define fastio() \ 4 | ios_base::sync_with_stdio(false); \ 5 | cin.tie(NULL); \ 6 | cout.tie(NULL); 7 | typedef long long ll; 8 | typedef long double ld; 9 | #define forall(i, a, b) for (ll i = a; i < b; i++) 10 | #define en "\n" 11 | #define deb(x) cout << #x << " " << x << "\n" 12 | #define pb push_back 13 | #define ppb pop_back 14 | #define mp make_pair 15 | #define eb emplace_back 16 | #define bg begin() 17 | #define ed end() 18 | #define sz size() 19 | #define vi vector 20 | #define yes cout<<"YES"<>n>>m; 31 | vector> adjm(n+1,vector(n+1,0)); 32 | forall(i,0,m){ 33 | int x,y;cin>>x>>y; 34 | adjm[x][y]=1; 35 | adjm[y][x]=1; 36 | } 37 | forall(i,1,n+1){ 38 | forall(j,1,n+1){ 39 | cout<>n>>m; 48 | forall(i,0,m){ 49 | int x,y;cin>>x>>y; 50 | adj[x].pb(y); 51 | adj[y].pb(x); 52 | } 53 | forall(i,1,n+1){ 54 | cout< "; 55 | forall(j,0,adj[i].size()){ 56 | cout<> t; 67 | while (t--) 68 | { 69 | solve(); 70 | } 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Linked list/DeletionInLinkedList2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node* next; 8 | 9 | node(int val){ 10 | data=val; 11 | next=NULL; 12 | } 13 | 14 | 15 | }; 16 | 17 | void insertAttail(node* &head,int val){ 18 | node* n=new node(val); 19 | if(head==NULL){ 20 | head=n; 21 | return; 22 | } 23 | node* temp=head; 24 | 25 | while (temp!=NULL) 26 | { 27 | temp=temp->next; 28 | } 29 | temp->next=n; 30 | return; 31 | 32 | } 33 | void deleteAthead(node* head){ 34 | node* todelete=head; 35 | head=head->next; 36 | delete todelete; 37 | } 38 | void deletion(node* &head,int val){ 39 | if(head==NULL){ 40 | return; 41 | } 42 | if(head->next==NULL){ 43 | deleteAthead(head); 44 | return; 45 | } 46 | node *temp=head; 47 | while (temp->next->data!=val) 48 | { 49 | temp=temp->next; 50 | } 51 | node* todelete=temp->next; 52 | temp->next=temp->next->next; 53 | delete todelete; 54 | 55 | } 56 | 57 | void insertAthead(node* &head,int val){ 58 | node* n=new node(val); 59 | n->next=head; 60 | head=n; 61 | 62 | } 63 | void display(node* head){ 64 | node* temp=head; 65 | while (temp!=NULL) 66 | { 67 | cout<data<<" "; 68 | temp=temp->next; 69 | } 70 | 71 | } 72 | int main(){ 73 | node* head=NULL; 74 | insertAttail(head,1); 75 | insertAttail(head,2); 76 | insertAttail(head,3); 77 | // display(head); 78 | insertAthead(head,5); 79 | display(head); 80 | 81 | // deletion(head,3); 82 | deleteAthead(head); 83 | display(head); 84 | 85 | 86 | 87 | 88 | return 0; 89 | } -------------------------------------------------------------------------------- /radix_sort.cpp: -------------------------------------------------------------------------------- 1 | // Radix Sort in C++ Programming 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Function to get the largest element from an array 7 | int getMax(int array[], int n) { 8 | int max = array[0]; 9 | for (int i = 1; i < n; i++) 10 | if (array[i] > max) 11 | max = array[i]; 12 | return max; 13 | } 14 | 15 | // Using counting sort to sort the elements in the basis of significant places 16 | void countingSort(int array[], int size, int place) { 17 | const int max = 10; 18 | int output[size]; 19 | int count[max]; 20 | 21 | for (int i = 0; i < max; ++i) 22 | count[i] = 0; 23 | 24 | // Calculate count of elements 25 | for (int i = 0; i < size; i++) 26 | count[(array[i] / place) % 10]++; 27 | 28 | // Calculate cumulative count 29 | for (int i = 1; i < max; i++) 30 | count[i] += count[i - 1]; 31 | 32 | // Place the elements in sorted order 33 | for (int i = size - 1; i >= 0; i--) { 34 | output[count[(array[i] / place) % 10] - 1] = array[i]; 35 | count[(array[i] / place) % 10]--; 36 | } 37 | 38 | for (int i = 0; i < size; i++) 39 | array[i] = output[i]; 40 | } 41 | 42 | // Main function to implement radix sort 43 | void radixsort(int array[], int size) { 44 | // Get maximum element 45 | int max = getMax(array, size); 46 | 47 | // Apply counting sort to sort elements based on place value. 48 | for (int place = 1; max / place > 0; place *= 10) 49 | countingSort(array, size, place); 50 | } 51 | 52 | // Print an array 53 | void printArray(int array[], int size) { 54 | int i; 55 | for (i = 0; i < size; i++) 56 | cout << array[i] << " "; 57 | cout << endl; 58 | } 59 | 60 | // Driver code 61 | int main() { 62 | int array[] = {154, 432, 54, 23, 12, 45, 788}; 63 | int n = sizeof(array) / sizeof(array[0]); 64 | radixsort(array, n); 65 | printArray(array, n); 66 | } 67 | 68 | //submitted by ANUSHKA NIGAM 69 | -------------------------------------------------------------------------------- /Graph/DFSbyMatrixAndRecursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define fastio() \ 4 | ios_base::sync_with_stdio(false); \ 5 | cin.tie(NULL); \ 6 | cout.tie(NULL); 7 | typedef long long ll; 8 | typedef long double ld; 9 | #define forall(i, a, b) for (ll i = a; i < b; i++) 10 | #define en "\n" 11 | #define deb(x) cout << #x << " " << x << "\n" 12 | #define pb push_back 13 | #define ppb pop_back 14 | #define mp make_pair 15 | #define eb emplace_back 16 | #define bg begin() 17 | #define ed end() 18 | #define sz size() 19 | #define vi vector 20 | #define yes cout<<"YES"<> adjm,bool visited[]){ 28 | if(visited[startEle]==0){ 29 | visited[startEle]=1; 30 | cout<>n>>m; 42 | vector>adjm(n+1,vector(n+1,0)); 43 | // 6 6 44 | // 1 2 45 | // 1 3 46 | // 2 4 47 | // 3 4 48 | // 4 5 49 | // 4 6 50 | forall(i,0,m){ 51 | int x,y; 52 | cin>>x>>y; 53 | adjm[x][y]=1; 54 | adjm[y][x]=1; 55 | } 56 | ll startEle; 57 | cin>>startEle; 58 | bool visited[n+1]; 59 | forall(i,0,n){ 60 | visited[i]=0; 61 | } 62 | DFS(startEle,n,adjm,visited); 63 | } 64 | 65 | int main() 66 | { 67 | fastio(); 68 | int t = 1; 69 | // cin >> t; 70 | while (t--) 71 | { 72 | solve(); 73 | } 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Linked list/DoublyLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node 5 | { 6 | public: 7 | int data; 8 | node *next; 9 | node *prev; 10 | node(int val) 11 | { 12 | data = val; 13 | next = NULL; 14 | prev = NULL; 15 | } 16 | }; 17 | void insertAthead(node *&head,int val) 18 | { 19 | 20 | node *n = new node(val); 21 | n->next = head; 22 | if (head != NULL) 23 | { 24 | head->prev = n; 25 | } 26 | head = n; 27 | } 28 | void insertAtTail(node *&head, int val) 29 | { 30 | 31 | if (head == NULL) 32 | { 33 | insertAthead(head,val); 34 | return; 35 | } 36 | node *n = new node(val); 37 | node *temp = head; 38 | 39 | while (temp->next != NULL) 40 | { 41 | temp = temp->next; 42 | } 43 | temp->next = n; 44 | n->prev = temp; 45 | } 46 | void display(node* &head){ 47 | node* temp=head; 48 | while(temp!=NULL){ 49 | cout<data<<" "; 50 | temp=temp->next; 51 | } 52 | } 53 | void deleteAthead(node* &head){ 54 | node* todelete=head; 55 | head=head->next; 56 | head->prev=NULL; 57 | delete todelete; 58 | } 59 | void deletion(node* &head,int key){ 60 | if(key=head->data){ 61 | deleteAthead(head); 62 | return; 63 | } 64 | node* temp=head; 65 | while(temp->data!=key){ 66 | temp=temp->next; 67 | 68 | } 69 | temp->prev->next=temp->next; 70 | if(temp->next!=NULL){ 71 | temp->next->prev=temp->prev;} 72 | delete temp; 73 | } 74 | 75 | int main() 76 | { 77 | node* head=NULL; 78 | insertAtTail(head,1); 79 | insertAtTail(head,2); 80 | insertAtTail(head,3); 81 | insertAtTail(head,4); 82 | display(head); 83 | cout< 2 | using namespace std; 3 | class Node { 4 | public: 5 | int data; 6 | Node* next; 7 | }; 8 | 9 | void push(Node** head_ref, int data) 10 | { 11 | Node* ptr1 = new Node(); 12 | ptr1->data = data; 13 | ptr1->next = *head_ref; 14 | 15 | if (*head_ref != NULL) 16 | { 17 | Node* temp = *head_ref; 18 | while (temp->next != *head_ref) 19 | temp = temp->next; 20 | temp->next = ptr1; 21 | } 22 | 23 | else 24 | ptr1->next = ptr1; 25 | 26 | *head_ref = ptr1; 27 | } 28 | 29 | void printList(Node* head) 30 | { 31 | Node* temp = head; 32 | if (head != NULL) { 33 | do { 34 | cout << temp->data << " "; 35 | temp = temp->next; 36 | } while (temp != head); 37 | } 38 | 39 | cout << endl; 40 | } 41 | 42 | void deleteNode(Node** head, int key) 43 | { 44 | 45 | if (*head == NULL) 46 | return; 47 | 48 | if((*head)->data==key && (*head)->next==*head) 49 | { 50 | free(*head); 51 | *head=NULL; 52 | return; 53 | } 54 | 55 | Node *last=*head,*d; 56 | if((*head)->data==key) 57 | { 58 | while(last->next!=*head) 59 | last=last->next; 60 | 61 | last->next=(*head)->next; 62 | free(*head); 63 | *head=last->next; 64 | } 65 | 66 | while(last->next!=*head&&last->next->data!=key) 67 | { 68 | last=last->next; 69 | } 70 | 71 | if(last->next->data==key) 72 | { 73 | d=last->next; 74 | last->next=d->next; 75 | free(d); 76 | } 77 | else 78 | cout<<"no such keyfound"; 79 | } 80 | 81 | int main() 82 | 83 | { 84 | Node* head = NULL; 85 | push(&head, 2); 86 | push(&head, 5); 87 | push(&head, 7); 88 | push(&head, 8); 89 | push(&head, 10); 90 | 91 | cout << "List Before Deletion: "; 92 | printList(head); 93 | 94 | deleteNode(&head, 7); 95 | 96 | cout << "List After Deletion: "; 97 | printList(head); 98 | 99 | return 0; 100 | } -------------------------------------------------------------------------------- /Dyanamic programing/O-1 Knapsack.c++: -------------------------------------------------------------------------------- 1 | /*O-1 knapsack probelm state that to get maximum profit of a product you have only two 2 | option either you have to choose it or reject it*/ 3 | //Recursion 4 | #include 5 | using namespace std; 6 | int knapSack_bottomup_dp(int W, int wt[], int val[], int n) 7 | { 8 | int i, w; 9 | vector> K(n + 1, vector(W + 1)); 10 | 11 | 12 | 13 | for(i = 0; i <= n; i++) 14 | { 15 | for(w = 0; w <= W; w++) 16 | { 17 | if (i == 0 || w == 0) 18 | K[i][w] = 0; 19 | else if (wt[i - 1] <= w) 20 | K[i][w] = max(val[i - 1] + 21 | K[i - 1][w - wt[i - 1]], 22 | K[i - 1][w]); 23 | else 24 | K[i][w] = K[i - 1][w]; 25 | } 26 | } 27 | return K[n][W]; 28 | } 29 | 30 | ///Recursive code 31 | int knapSack_rec(int W, int wt[], int val[], int n) 32 | { 33 | 34 | //Base case 35 | if (n == 0 || W == 0) 36 | return 0; 37 | 38 | //Rejecting element 39 | if (wt[n - 1] > W) 40 | return knapSack_rec(W, wt, val, n - 1); 41 | 42 | //accepting and find maximum 43 | else 44 | return max( 45 | val[n - 1] 46 | + knapSack_rec(W - wt[n - 1], 47 | wt, val, n - 1), 48 | knapSack_rec(W, wt, val, n - 1)); 49 | } 50 | 51 | 52 | int main() 53 | { 54 | //no of item 55 | int n; 56 | cin>>n; 57 | //value of item 58 | int val[n]; 59 | //weight of item 60 | int wt[n]; 61 | //maximum weight can hold by the bag 62 | int W; 63 | for(int i=0;i>val[i]; 66 | } 67 | for(int i=0;i>wt[i]; 70 | } 71 | 72 | cin>>W; 73 | ///Recursive Code 74 | cout << knapSack_rec(W, wt, val, n)< 9 | using namespace std; 10 | 11 | vector> threeSum(vector& num) { 12 | vector> res; 13 | sort(num.begin(), num.end()); 14 | 15 | // moves for a 16 | for (int i = 0; i < (int)(num.size())-2; i++) { 17 | 18 | if (i == 0 || (i > 0 && num[i] != num[i-1])) { 19 | 20 | int lo = i+1, hi = (int)(num.size())-1, sum = 0 - num[i]; 21 | 22 | while (lo < hi) { 23 | if (num[lo] + num[hi] == sum) { 24 | 25 | vector temp; 26 | temp.push_back(num[i]); 27 | temp.push_back(num[lo]); 28 | temp.push_back(num[hi]); 29 | res.push_back(temp); 30 | 31 | while (lo < hi && num[lo] == num[lo+1]) lo++; 32 | while (lo < hi && num[hi] == num[hi-1]) hi--; 33 | 34 | lo++; hi--; 35 | } 36 | else if (num[lo] + num[hi] < sum) lo++; 37 | else hi--; 38 | } 39 | } 40 | } 41 | return res; 42 | } 43 | int main(){ 44 | int n; 45 | cout<<"Enter Size of array: "; 46 | cin>>n; 47 | int k; 48 | cout<<"Enter the target sum: "; 49 | cin>>k; 50 | cout<<"Enter the array elements: "; 51 | vectorarr(n); 52 | for(int i=0;i>arr[i]; 54 | vectorres=threeSum(arr); 55 | for(int i=0;i 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node* next; 8 | node(int val){ 9 | data=val; 10 | next=NULL; 11 | } 12 | }; 13 | void insertAttail(node* &head,int val){ 14 | node* n=new node(val); 15 | if(head==NULL){ 16 | head=n; 17 | return; 18 | } 19 | node* temp=head; 20 | 21 | while (temp!=NULL) 22 | { 23 | temp=temp->next; 24 | } 25 | temp->next=n; 26 | return; 27 | 28 | } 29 | void insertAthead(node* &head,int val){ 30 | node* n=new node(val); 31 | n->next=head; 32 | head=n; 33 | 34 | } 35 | void display(node* head){ 36 | node* temp=head; 37 | while (temp!=NULL) 38 | { 39 | cout<data<<" "; 40 | temp=temp->next; 41 | } 42 | 43 | } 44 | int length(node* &head){ 45 | node* temp=head; 46 | int l=0; 47 | while (temp!=NULL) 48 | { 49 | l++; 50 | temp=temp->next; 51 | } 52 | return l; 53 | 54 | } 55 | void AppendLastKnodes(node* &head,int k){ 56 | node* temp=head; 57 | int m=0; 58 | node* newTail; 59 | node* Tail; 60 | node* newHead; 61 | int l=length(head); 62 | 63 | while(m!=l){ 64 | if(m==l-k){ 65 | newTail=temp; 66 | } 67 | if(m==l-k+1){ 68 | newHead=temp; 69 | } 70 | if(m==l){ 71 | Tail=temp; 72 | } 73 | m++; 74 | temp=temp->next; 75 | } 76 | newTail->next=NULL; 77 | Tail->next=head; 78 | head=newHead; 79 | } 80 | 81 | int main(){ 82 | node* head=NULL; 83 | insertAttail(head,1); 84 | insertAttail(head,2); 85 | insertAttail(head,3); 86 | insertAttail(head,4); 87 | insertAttail(head,5); 88 | insertAttail(head,6); 89 | display(head); 90 | cout< 14 | using namespace std; 15 | 16 | int egg,flr; 17 | int **table= NULL; 18 | int min_probes=0; 19 | 20 | void input() 21 | { 22 | cin>>egg; 23 | cin>>flr; 24 | table=new int*[flr+1]; //Initializing memory for DP Table 25 | for(int i = 0; i <= egg; ++i) 26 | table[i] = new int[flr+1]; 27 | 28 | } 29 | int find_min_probes(int e,int f) 30 | { 31 | 32 | if(e==1) 33 | return f; 34 | 35 | if(f==0||f==1) 36 | return f; 37 | 38 | int probes=INT_MAX; 39 | for(int k=1;k<=f;k++) 40 | { 41 | int temp=1+max(find_min_probes(e-1,k-1),find_min_probes(e,f-k)); 42 | probes=min(probes,temp); 43 | } 44 | return probes; 45 | } 46 | 47 | void display() 48 | { 49 | cout<<"*************** Egg Droping Problem *****************"< 11 | 12 | using namespace std; 13 | 14 | const int MAX =1e5 ; 15 | int id[MAX], nodes, edges ; 16 | pair> p[MAX] ; 17 | 18 | void initialize() 19 | { 20 | for(int i=0 ; i> nodes >> edges; 60 | for(int i = 0;i < edges;++i) 61 | { 62 | cin >> x >> y >> weight; 63 | p[i] = make_pair(weight, make_pair(x, y)); 64 | } 65 | minimumCost = kruskal(edges); 66 | cout << minimumCost << endl; 67 | return 0 ; 68 | } 69 | 70 | /* EXAMPLE : 71 | 6 9 72 | 1 2 2 73 | 1 4 1 74 | 1 5 4 75 | 2 3 3 76 | 2 4 3 77 | 2 6 7 78 | 3 6 8 79 | 3 4 5 80 | 4 5 9 81 | OUTPUT : 17 82 | */ 83 | -------------------------------------------------------------------------------- /Linked list/DetectionAndRemoval.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node* next; 8 | node(int val){ 9 | data=val; 10 | next=NULL; 11 | } 12 | }; 13 | 14 | void insertAtTail(node* &head,int val){ 15 | node* n=new node(val); 16 | if(head==NULL){ 17 | head=n; 18 | return; 19 | } 20 | node* temp=head; 21 | while (temp!=NULL) 22 | { 23 | temp=temp->next; 24 | } 25 | temp->next=n; 26 | 27 | } 28 | void display(node* head){ 29 | node* temp=head; 30 | while(temp!=NULL){ 31 | cout<data<<" "; 32 | temp=temp->next; 33 | } 34 | } 35 | void makeCycle(node* &head,int pos){ 36 | node* temp=head; 37 | node* startnode; 38 | int count=1; 39 | while (temp->next!=NULL) 40 | { 41 | if(count==pos){ 42 | startnode=temp; 43 | } 44 | temp=temp->next; 45 | count++; 46 | } 47 | temp->next=startnode; 48 | 49 | } 50 | void revoveCycle(node* &head){ 51 | node* slow=head; 52 | node* fast=head; 53 | bool flag=false; 54 | 55 | while (fast->next==slow->next) 56 | { 57 | if(!flag){ 58 | slow=slow->next; 59 | fast=fast->next->next; 60 | if(slow!=fast){ 61 | flag=true; 62 | fast=head; 63 | }} 64 | if(flag){ 65 | slow=slow->next; 66 | fast=fast->next; 67 | } 68 | } 69 | slow->next=NULL; 70 | } 71 | bool detectCycle(node* &head){ 72 | node* slow=head; 73 | node* fast=head; 74 | 75 | while (fast!=NULL && fast->next!=NULL) 76 | { 77 | slow=slow->next; 78 | fast=fast->next->next; 79 | if(slow==fast){ 80 | return true; 81 | } 82 | } 83 | return false; 84 | 85 | } 86 | 87 | int main(){ 88 | node* head=NULL; 89 | insertAtTail(head,1); 90 | insertAtTail(head,2); 91 | insertAtTail(head,3); 92 | insertAtTail(head,4); 93 | makeCycle(head,2); 94 | // display(head); 95 | detectCycle(head); 96 | 97 | 98 | } -------------------------------------------------------------------------------- /Linked list/reversing_linkedlist_using_stack.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for above approach 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // Create a class Node to enter 7 | // values and address in the list 8 | class Node 9 | { 10 | public: 11 | int data; 12 | Node* next; 13 | }; 14 | 15 | // Function to reverse the 16 | // linked list 17 | void reverseLL(Node** head) 18 | { 19 | 20 | // Create a stack "s" 21 | // of Node type 22 | stack s; 23 | Node* temp = *head; 24 | while (temp->next != NULL) 25 | { 26 | 27 | // Push all the nodes 28 | // in to stack 29 | s.push(temp); 30 | temp = temp->next; 31 | } 32 | *head = temp; 33 | 34 | while (!s.empty()) 35 | { 36 | 37 | // Store the top value of 38 | // stack in list 39 | temp->next = s.top(); 40 | 41 | // Pop the value from stack 42 | s.pop(); 43 | 44 | // update the next pointer in the 45 | // in the list 46 | temp = temp->next; 47 | } 48 | temp->next = NULL; 49 | } 50 | 51 | // Function to Display 52 | // the elements in List 53 | void printlist(Node* temp) 54 | { 55 | while (temp != NULL) 56 | { 57 | cout << temp->data << " "; 58 | temp = temp->next; 59 | } 60 | } 61 | 62 | // Program to insert back of the 63 | // linked list 64 | void insert_back(Node** head, int value) 65 | { 66 | 67 | // we have used insertion at back method 68 | // to enter values in the list.(eg: 69 | // head->1->2->3->4->Null) 70 | Node* temp = new Node(); 71 | temp->data = value; 72 | temp->next = NULL; 73 | 74 | // If *head equals to NULL 75 | if (*head == NULL) 76 | { 77 | *head = temp; 78 | return; 79 | } 80 | else 81 | { 82 | Node* last_node = *head; 83 | while (last_node->next != NULL) 84 | { 85 | last_node = last_node->next; 86 | } 87 | last_node->next = temp; 88 | return; 89 | } 90 | } 91 | 92 | // Driver Code 93 | int main() 94 | { 95 | Node* head = NULL; 96 | 97 | insert_back(&head, 1); 98 | insert_back(&head, 2); 99 | insert_back(&head, 3); 100 | insert_back(&head, 4); 101 | cout << "Given linked list\n"; 102 | printlist(head); 103 | reverseLL(&head); 104 | cout << "\nReversed linked list\n"; 105 | printlist(head); 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /peakElement.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of integers. Find a peak element in it. An array element is a peak if it is NOT smaller than its neighbours. For corner elements, we need to consider only one neighbour. 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | int peak_element(int arr[], int n) 9 | { 10 | //declaring and initializing two pointers l and r 11 | //pointing to the leftmost and rightmost element respectively. 12 | int l = 0, r = n - 1; 13 | //iterating the elements until l is less than equal to r 14 | while (l <= r) { 15 | //computing mid 16 | // (l+r)/2 directly not used as in some cases int overflow may occur 17 | int mid = l + (r - l) / 2; 18 | //if mid lies between 2nd element and last 2nd element(both inclusive) 19 | if(mid>0 && midarr[mid-1] && arr[mid]>arr[mid+1]) 21 | return mid; 22 | //element on left is greater then ignore the right half and vice versa 23 | else if(arr[mid-1]>arr[mid]) 24 | r=mid-1; 25 | else 26 | l=mid+1; 27 | } 28 | //if 1st element is the mid 29 | else if(mid==0) 30 | { 31 | if(arr[0]>arr[1]) 32 | return 0; 33 | else return 1; 34 | } 35 | // if last element is the mid 36 | else if(mid==n-1){ 37 | if(arr[n-1]>arr[n-2]) 38 | return n-1; 39 | else return n-2; 40 | } 41 | } 42 | return -1; 43 | } 44 | 45 | int main() 46 | { 47 | //taking input from user 48 | int n; 49 | cout << "Enter size of array: "; 50 | cin >> n; 51 | 52 | int arr[n]; 53 | cout << "Enter array Elements: "; 54 | for (int i = 0; i < n; i++) 55 | cin >> arr[i]; 56 | cout << "Peak element index: "; 57 | cout << peak_element(arr, n); 58 | } 59 | 60 | /* 61 | Test Cases: 62 | * Input :- 63 | Enter size of array: 7 64 | Enter array Elements: 10 20 15 2 23 90 67 65 | 66 | * Output :- 67 | Peak element index: 1 68 | 69 | * Input :- 70 | Enter size of array: 6 71 | Enter array Elements: 60 70 80 20 40 50 72 | 73 | * Output :- 74 | Peak element index: 2 75 | 76 | * Time complexity: O(Log n) 77 | * Space Complexity: O(1) 78 | */ -------------------------------------------------------------------------------- /Sum_of_leaf_nodes_at_each_horizontal_level_in_a_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Tree node structure 5 | class Node { 6 | public: 7 | int data; 8 | Node *left, *right; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | left = right = NULL; 14 | } 15 | }; 16 | 17 | // Function to print the sum of leaf nodes 18 | // at each horizontal level 19 | void printLevelSum(Node* root) 20 | { 21 | if (root == NULL) { 22 | cout << "No nodes present\n"; 23 | return; 24 | } 25 | 26 | // Map to hold sum at each level 27 | map mp; 28 | 29 | // Queue to hold tree node with level 30 | queue > q; 31 | 32 | // Root node is at level 1 33 | q.push({ root, 1 }); 34 | 35 | pair p; 36 | 37 | // Level Order Traversal of tree 38 | while (!q.empty()) { 39 | p = q.front(); 40 | q.pop(); 41 | 42 | // Create a key for each level 43 | // in the map 44 | if (mp.find(p.second) == mp.end()) { 45 | mp[p.second] = 0; 46 | } 47 | 48 | // If current node is a leaf node 49 | if (p.first->left == NULL 50 | && p.first->right == NULL) { 51 | 52 | // Adding value in the map 53 | // corresponding to its level 54 | mp[p.second] += p.first->data; 55 | } 56 | 57 | if (p.first->left) 58 | q.push({ p.first->left, p.second + 1 }); 59 | if (p.first->right) 60 | q.push({ p.first->right, p.second + 1 }); 61 | } 62 | 63 | // Print the sum at each level 64 | for (auto i : mp) { 65 | cout << i.second << endl; 66 | } 67 | } 68 | 69 | // Driver Code 70 | int main() 71 | { 72 | Node* root = new Node(1); 73 | root->left = new Node(2); 74 | root->right = new Node(3); 75 | root->left->left = new Node(4); 76 | root->left->right = new Node(5); 77 | root->right->left = new Node(6); 78 | root->right->right = new Node(7); 79 | root->left->left->right = new Node(8); 80 | root->left->right->right = new Node(9); 81 | root->right->right->left = new Node(10); 82 | root->right->right->right = new Node(11); 83 | root->left->left->right->right = new Node(12); 84 | 85 | printLevelSum(root); 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /circularqueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int rear = -1; 4 | int n = 5; 5 | int front = n; 6 | 7 | int insert(int arr[], int num) 8 | { 9 | if (front == 0) 10 | { 11 | printf("Overflow\n\n"); 12 | } 13 | else 14 | { 15 | arr[n - 2 - rear] = num; 16 | rear++; 17 | front--; 18 | printf("The number inserted is %d\n\n", num); 19 | } 20 | } 21 | 22 | int del(int arr[]) 23 | { 24 | if (rear == -1) 25 | { 26 | printf("UnderFlow\n\n"); 27 | } 28 | else 29 | { 30 | printf("The deleted number is %d\n\n", arr[n - 1]); 31 | for (int i = n - 1; i >= front; i--) 32 | { 33 | arr[i] = arr[i - 1]; 34 | } 35 | front++; 36 | rear--; 37 | } 38 | } 39 | bool present = false; 40 | int x = 0; 41 | 42 | int peep(int arr[], int m) 43 | { 44 | if (rear == -1) 45 | { 46 | printf("Queue is empty\n\n"); 47 | } 48 | else 49 | { 50 | for (int i = front; i < n; i++) 51 | { 52 | if (arr[i] == m) 53 | { 54 | present = true; 55 | x = i - front; 56 | break; 57 | } 58 | } 59 | if (present) 60 | { 61 | printf("\nIt is present at index %d\n\n", x); 62 | } 63 | else 64 | { 65 | printf("\nIt is not present in the queue.\n\n"); 66 | } 67 | } 68 | } 69 | 70 | int display(int arr[]) 71 | { 72 | if (rear == -1) 73 | { 74 | printf("Queue is Empty\n\n"); 75 | } 76 | else 77 | { 78 | for (int i = front; i < n; i++) 79 | { 80 | printf("%d ", arr[i]); 81 | } 82 | printf("\n\n"); 83 | } 84 | } 85 | 86 | int main() 87 | { 88 | int arr[n]; 89 | int in; 90 | int num; 91 | while (in != -1) 92 | { 93 | printf("Enter 0 for insert, 1 for delete , 2 for peep, 3 for display:"); 94 | scanf("%d", &in); 95 | if (in == 0) 96 | { 97 | printf("Enter the number you want to insert:"); 98 | scanf("%d", &num); 99 | insert(arr, num); 100 | } 101 | if (in == 1) 102 | { 103 | del(arr); 104 | } 105 | if (in == 2) 106 | { 107 | printf("Enter the number you want to peep:"); 108 | scanf("%d", &num); 109 | peep(arr, num); 110 | } 111 | if (in == 3) 112 | { 113 | display(arr); 114 | } 115 | } 116 | return 0; 117 | 118 | // insert(arr,100); 119 | // insert(arr,20); 120 | // insert(arr,30); 121 | // insert(arr,40); 122 | // insert(arr,2); 123 | // del(arr); 124 | // insert(arr,88); 125 | // display(arr); 126 | // peep(arr,99); 127 | } 128 | -------------------------------------------------------------------------------- /DIGITREM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int mod=1e9+7; 5 | #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 6 | typedef long long int ll; 7 | 8 | int main() { 9 | fast; 10 | int t; 11 | cin>>t; 12 | while(t--){ 13 | 14 | 15 | ll n,d; 16 | 17 | cin>>n>>d; 18 | string s= to_string(n); 19 | ll len = s.length(); 20 | 21 | if(d==0){ 22 | int a= len; 23 | for(int i = 0; i=0;j--){ 46 | if(s[j]<='7'){ 47 | s[j]++; 48 | b=j; 49 | goto cvv; 50 | } 51 | } 52 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | double findMedianSortedArrays(vector& A, vector& B) { 7 | int n1 = A.size(); 8 | int n2 = B.size(); 9 | 10 | // select the minimum sized array out of A & B 11 | if(n2 < n1) 12 | return findMedianSortedArrays(B, A); 13 | 14 | int l = 0, h = n1; 15 | 16 | while(l <= h) 17 | { 18 | int cut1 = l + (h - l) / 2; 19 | 20 | int cut2 = (n1 + n2) / 2 - cut1; 21 | 22 | int l1 = cut1 == 0 ? INT_MIN : A[cut1 - 1]; 23 | 24 | int l2 = cut2 == 0 ? INT_MIN : B[cut2 - 1]; 25 | 26 | int r1 = cut1 == n1 ? INT_MAX : A[cut1]; 27 | 28 | int r2 = cut2 == n2 ? INT_MAX : B[cut2]; 29 | 30 | if(l1 > r2) 31 | { 32 | h = cut1 - 1; 33 | } 34 | else if(l2 > r1) 35 | { 36 | l = cut1 + 1; 37 | } 38 | else 39 | { 40 | return (n1+n2)%2 == 0 ? ((double)max(l1,l2) + (double)min(r1,r2))/2.0 : min(r1,r2); 41 | } 42 | } 43 | return -1; 44 | } 45 | }; 46 | 47 | int main() 48 | { 49 | vector A, B; 50 | 51 | int n1,n2; 52 | 53 | cout<<"Enter the Size of First Array : "; 54 | cin>>n1; 55 | 56 | cout<<"Enter the Size of second Array : "; 57 | cin>>n2; 58 | 59 | 60 | cout<<"\nEnter the Elements of First Array \n"; 61 | for(int i = 0; i < n1; i++) 62 | { 63 | int t; 64 | cin>>t; 65 | A.push_back(t); 66 | } 67 | 68 | cout<<"\nEnter the Elements of second Array \n"; 69 | for(int i = 0; i < n2; i++) 70 | { 71 | int t; 72 | cin>>t; 73 | B.push_back(t); 74 | } 75 | 76 | sort(A.begin(), A.end()); 77 | 78 | sort(B.begin(), B.end()); 79 | 80 | cout<<"\nElements of first Array :- \n"; 81 | 82 | for(int t : A) 83 | cout< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node* next; 8 | 9 | node(int val){ 10 | data=val; 11 | next=NULL; 12 | } 13 | 14 | 15 | }; 16 | 17 | void insertAttail(node* &head,int val){ 18 | node* n=new node(val); 19 | if(head==NULL){ 20 | head=n; 21 | return; 22 | } 23 | node* temp=head; 24 | 25 | while (temp->next!=NULL) 26 | { 27 | temp=temp->next; 28 | } 29 | temp->next=n; 30 | return; 31 | 32 | } 33 | void deleteAthead(node* head){ 34 | node* todelete=head; 35 | head=head->next; 36 | delete todelete; 37 | } 38 | void deletion(node* &head,int val){ 39 | if(head==NULL){ 40 | return; 41 | } 42 | if(head->next==NULL){ 43 | deleteAthead(head); 44 | return; 45 | } 46 | node*temp=head; 47 | while (temp->next->data!=val) 48 | { 49 | temp=temp->next; 50 | } 51 | node* todelete=temp->next; 52 | temp->next=temp->next->next; 53 | delete todelete; 54 | 55 | } 56 | 57 | void insertAthead(node* &head,int val){ 58 | node* n=new node(val); 59 | n->next=head; 60 | head=n; 61 | 62 | } 63 | void display(node* head){ 64 | node* temp=head; 65 | while (temp!=NULL) 66 | { 67 | cout<data<<" "; 68 | temp=temp->next; 69 | } 70 | 71 | } 72 | node* reverse(node* &head){ 73 | node* prevptr=NULL; 74 | node* currptr=head; 75 | node* nextptr=head->next; 76 | while (currptr!=NULL) 77 | { 78 | nextptr=currptr->next; 79 | currptr->next=prevptr; 80 | prevptr=currptr; 81 | currptr=nextptr; 82 | } 83 | head=prevptr; 84 | return head; 85 | } 86 | node* reverseByRecursion(node* &head){ 87 | if(head==NULL || head->next==NULL){ 88 | return head; 89 | } 90 | node* newhead=reverseByRecursion(head->next); 91 | head->next->next=head; 92 | head->next=NULL; 93 | return newhead; 94 | } 95 | int main(){ 96 | node* head=NULL; 97 | insertAttail(head,1); 98 | insertAttail(head,2); 99 | insertAttail(head,3); 100 | insertAttail(head,4); 101 | 102 | node* newhead=reverse(head); 103 | display(newhead); 104 | 105 | 106 | } -------------------------------------------------------------------------------- /Linked list/CircularLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node 4 | { 5 | public: 6 | int data; 7 | node *next; 8 | node(int val) 9 | { 10 | data = val; 11 | next = NULL; 12 | } 13 | }; 14 | void insertAthead(node *&head, int val) 15 | { 16 | 17 | node *n = new node(val); 18 | if (head == NULL) 19 | { 20 | n->next = n; 21 | head = n; 22 | return; 23 | } 24 | node *temp = head; 25 | 26 | while (temp->next != head) 27 | { 28 | temp = temp->next; 29 | } 30 | temp->next = n; 31 | n->next = head; 32 | head = n; 33 | return; 34 | } 35 | void insertAttail(node *&head, int val) 36 | { 37 | if (head == NULL) 38 | { 39 | insertAthead(head, val); 40 | return; 41 | } 42 | node *n = new node(val); 43 | 44 | node *temp = head; 45 | 46 | while (temp->next != head) 47 | { 48 | temp = temp->next; 49 | } 50 | temp->next = n; 51 | n->next = head; 52 | return; 53 | } 54 | 55 | void display(node *head) 56 | { 57 | node *temp = head; 58 | do 59 | { 60 | cout << temp->data << " "; 61 | temp = temp->next; 62 | } while (temp != head); 63 | cout << endl; 64 | } 65 | void deleteAthead(node *&head) 66 | { 67 | node *temp = head; 68 | node *todelete = head; 69 | while (temp->next != head) 70 | { 71 | temp = temp->next; 72 | } 73 | temp->next = head->next; 74 | head = head->next; 75 | delete todelete; 76 | } 77 | void deletion(node *&head, int pos) 78 | { 79 | if (pos == 1) 80 | { 81 | deleteAthead(head); 82 | return; 83 | } 84 | node *temp = head; 85 | int count = 1; 86 | 87 | while (count != pos - 1) 88 | { 89 | temp = temp->next; 90 | count++; 91 | } 92 | node *todelete = temp->next; 93 | temp->next = temp->next->next; 94 | delete todelete; 95 | } 96 | 97 | int main() 98 | { 99 | node *head = NULL; 100 | insertAttail(head, 1); 101 | insertAttail(head, 2); 102 | insertAttail(head, 3); 103 | insertAttail(head, 4); 104 | insertAttail(head, 5); 105 | insertAttail(head, 6); 106 | display(head); 107 | cout< 3 | using namespace std; 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node* next; 9 | node(int val){ 10 | data=val; 11 | next=NULL; 12 | } 13 | }; 14 | void insertAttail(node* &head,int val){ 15 | node* n=new node(val); 16 | if(head==NULL){ 17 | head=n; 18 | return; 19 | } 20 | node* temp=head; 21 | 22 | while (temp->next!=NULL) 23 | { 24 | temp=temp->next; 25 | } 26 | temp->next=n; 27 | return; 28 | 29 | } 30 | void insertAthead(node* &head,int val){ 31 | node* n=new node(val); 32 | n->next=head; 33 | head=n; 34 | 35 | } 36 | void display(node* head){ 37 | node* temp=head; 38 | while (temp!=NULL) 39 | { 40 | cout<data<<" "; 41 | temp=temp->next; 42 | } 43 | 44 | } 45 | node* merge(node* &head1,node* &head2){ 46 | node* p1=head1; 47 | node* p2=head2; 48 | node* dummyNode=new node(-1); 49 | node* p3=dummyNode; 50 | while (p1!=NULL && p2!=NULL) 51 | { 52 | if(p1->datadata){ 53 | p3->next=p1; 54 | p1=p1->next; 55 | } 56 | else{ 57 | p3->next=p2; 58 | p2=p2->next; 59 | } 60 | p3=p3->next; 61 | } 62 | while (p1!=NULL) 63 | { 64 | p3->next=p1; 65 | p1=p1->next; 66 | p3=p3->next; 67 | } 68 | while (p2!=NULL) 69 | { 70 | p3->next=p2; 71 | p2=p2->next; 72 | p3=p3->next; 73 | } 74 | return dummyNode->next; 75 | } 76 | node* mergeRecursive(node* &head1,node* &head2){ 77 | if(head1==NULL){ 78 | return head2; 79 | } 80 | if(head2==NULL){ 81 | return head1; 82 | } 83 | node* result; 84 | if(head1->datadata){ 85 | result=head1; 86 | result->next=mergeRecursive(head1->next,head2); 87 | } 88 | else 89 | { 90 | result=head2; 91 | result->next=mergeRecursive(head2->next,head2); 92 | } 93 | return result; 94 | 95 | } 96 | int main(){ 97 | node* head1=NULL; 98 | node* head2=NULL; 99 | insertAttail(head1,1); 100 | insertAttail(head1,2); 101 | insertAttail(head1,3); 102 | insertAttail(head2,4); 103 | insertAttail(head2,5); 104 | insertAttail(head2,6); 105 | display(head1); 106 | cout< 28 | using namespace std; 29 | 30 | 31 | // } Driver Code Ends 32 | //User function template in C++ 33 | 34 | class Solution 35 | { 36 | bool isValid(int arr[],int n,int m,int range){ 37 | int temp=0,st=1; 38 | for(int i=0;irange){ 41 | st++; 42 | temp=arr[i]; 43 | } 44 | if(st>m) 45 | return false; 46 | } 47 | return true; 48 | } 49 | public: 50 | //Function to find minimum number of pages. 51 | int findPages(int arr[], int n, int m) 52 | { 53 | if(n>t; 79 | while(t--){ 80 | int n; 81 | cin>>n; 82 | int A[n]; 83 | for(int i=0;i>A[i]; 85 | } 86 | int m; 87 | cin>>m; 88 | Solution ob; 89 | cout << ob.findPages(A, n, m) << endl; 90 | } 91 | return 0; 92 | } 93 | // } Driver Code Ends 94 | -------------------------------------------------------------------------------- /Kadane_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fast \ 5 | ios_base::sync_with_stdio(false); \ 6 | cin.tie(NULL); 7 | #define time cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" \ 8 | << "\n"; 9 | #define F first 10 | #define S second 11 | #define pb push_back 12 | typedef long long int ll; 13 | 14 | /*-----------------------To find Maximum Sum Subarray-------------------------------------------------------------------------------------*/ 15 | 16 | void solve() 17 | { 18 | 19 | int array[] = {4, 3, -2, 6, -14, 7, -1, 4, 5, 7, -10, 2, 9, -10, -5, -9, 6, 1}; 20 | 21 | int arraySize = sizeof(array) / sizeof(array[0]); 22 | 23 | /*****************Brute Force***************************/ 24 | 25 | int total = array[0]; 26 | int current_sum = array[0]; 27 | 28 | for (int i = 0; i < arraySize; i++) 29 | { 30 | current_sum = array[i]; 31 | 32 | for (int j = i + 1; j < arraySize; j++) 33 | { 34 | 35 | current_sum += array[j]; 36 | total = max(current_sum, total); 37 | } 38 | } 39 | 40 | /********************Kadane's Approach***********************/ 41 | 42 | int currentSum = array[0]; 43 | int overallSum = array[0]; 44 | 45 | for (int i = 1; i < arraySize; i++) 46 | { 47 | if (currentSum >= 0) 48 | { 49 | currentSum += array[i]; 50 | } 51 | else 52 | { 53 | currentSum = array[i]; 54 | } 55 | 56 | if (overallSum < currentSum) 57 | { 58 | overallSum = currentSum; 59 | } 60 | } 61 | 62 | cout << "Solution using Kadane's Algo : " << overallSum << "\n"; 63 | cout << "Solution using Brute Force Approach : " << total << "\n"; 64 | 65 | /* 66 | Time complexity of Brute force = O(n^2) ; 67 | Time complexity of Kadane's Algorithm = O(n^2) ; 68 | */ 69 | } 70 | 71 | int32_t main() 72 | { 73 | 74 | fast; 75 | time; 76 | 77 | #ifndef ONLINE_JUDGE 78 | freopen("input.txt", "r", stdin); 79 | freopen("output.txt", "w", stdout); 80 | #endif 81 | 82 | int t = 1; 83 | // cin >> t; 84 | 85 | while (t--) 86 | { 87 | solve(); 88 | } 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Reverse_Circular_Doubly_Linklist.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation to reverse a 2 | // doubly circular linked list 3 | #include 4 | 5 | using namespace std; 6 | 7 | // structure of a node of linked list 8 | struct Node { 9 | int data; 10 | Node *next, *prev; 11 | }; 12 | 13 | // function to create and return a new node 14 | Node* getNode(int data) 15 | { 16 | Node* newNode = (Node*)malloc(sizeof(Node)); 17 | newNode->data = data; 18 | return newNode; 19 | } 20 | 21 | // Function to insert at the end 22 | void insertEnd(Node** head, Node* new_node) 23 | { 24 | // If the list is empty, create a single node 25 | // circular and doubly list 26 | if (*head == NULL) { 27 | new_node->next = new_node->prev = new_node; 28 | *head = new_node; 29 | return; 30 | } 31 | 32 | // If list is not empty 33 | 34 | /* Find last node */ 35 | Node* last = (*head)->prev; 36 | 37 | // Start is going to be next of new_node 38 | new_node->next = *head; 39 | 40 | // Make new node previous of start 41 | (*head)->prev = new_node; 42 | 43 | // Make last previous of new node 44 | new_node->prev = last; 45 | 46 | // Make new node next of old last 47 | last->next = new_node; 48 | } 49 | 50 | // Utility function to reverse a 51 | // doubly circular linked list 52 | Node* reverse(Node* head) 53 | { 54 | if (!head) 55 | return NULL; 56 | 57 | // Initialize a new head pointer 58 | Node* new_head = NULL; 59 | 60 | // get pointer to the the last node 61 | Node* last = head->prev; 62 | 63 | // set 'curr' to last node 64 | Node *curr = last, *prev; 65 | 66 | // traverse list in backward direction 67 | while (curr->prev != last) { 68 | prev = curr->prev; 69 | 70 | // insert 'curr' at the end of the list 71 | // starting with the 'new_head' pointer 72 | insertEnd(&new_head, curr); 73 | curr = prev; 74 | } 75 | insertEnd(&new_head, curr); 76 | 77 | // head pointer of the reversed list 78 | return new_head; 79 | } 80 | 81 | // function to display a doubly circular list in 82 | // forward and backward direction 83 | void display(Node* head) 84 | { 85 | if (!head) 86 | return; 87 | 88 | Node* temp = head; 89 | 90 | cout << "Forward direction: "; 91 | while (temp->next != head) { 92 | cout << temp->data << " "; 93 | temp = temp->next; 94 | } 95 | cout << temp->data; 96 | 97 | Node* last = head->prev; 98 | temp = last; 99 | 100 | cout << "\nBackward direction: "; 101 | while (temp->prev != last) { 102 | cout << temp->data << " "; 103 | temp = temp->prev; 104 | } 105 | cout << temp->data; 106 | } 107 | 108 | // Driver program to test above 109 | int main() 110 | { 111 | Node* head = NULL; 112 | 113 | insertEnd(&head, getNode(1)); 114 | insertEnd(&head, getNode(2)); 115 | insertEnd(&head, getNode(3)); 116 | insertEnd(&head, getNode(4)); 117 | insertEnd(&head, getNode(5)); 118 | 119 | cout << "Current list:\n"; 120 | display(head); 121 | 122 | head = reverse(head); 123 | 124 | cout << "\n\nReversed list:\n"; 125 | display(head); 126 | 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /Multiple_Paranthesis_Using_Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Stack 4 | { 5 | char ch; 6 | Stack *next; 7 | }; 8 | class Stack_Paranthesis 9 | { 10 | Stack *top = NULL; 11 | Stack *ptr; 12 | 13 | protected: 14 | void reset_stack(); 15 | bool pop(char ch); 16 | void push(char ch); 17 | bool isEmpty(); 18 | 19 | public: 20 | void processing_Results(); 21 | }; 22 | 23 | 24 | void Stack_Paranthesis:: processing_Results(){ 25 | string Expression; 26 | int popcount=0; 27 | int pushCount=0; 28 | bool flag; 29 | cout<<"Enter the Expression Using the Multiple Paranthesis: "; 30 | cin>>Expression; 31 | for (int i = 0; i < Expression.size(); i++) 32 | { 33 | if(Expression[i]=='('|| Expression[i]=='{'||Expression[i]=='['){ 34 | push(Expression[i]); 35 | pushCount++; 36 | } else if (Expression[i]==')'|| Expression[i]=='}'||Expression[i]==']'){ 37 | if(isEmpty()){ 38 | cout<<"Stack is Empty as the Expression is Unbalanced "<popcount){ 54 | cout<<"Stack is Unbalanced"<next; 65 | delete ptr; 66 | } 67 | } 68 | 69 | bool Stack_Paranthesis::isEmpty() 70 | { 71 | if (top == NULL) 72 | { 73 | return true; 74 | } 75 | else 76 | { 77 | return false; 78 | } 79 | } 80 | 81 | void Stack_Paranthesis::push(char ch) 82 | { 83 | Stack *new_node = new Stack; 84 | new_node->ch = ch; 85 | if (top == NULL) 86 | { 87 | top = new_node; 88 | } 89 | else 90 | { 91 | new_node->next = top; 92 | top = new_node; 93 | } 94 | } 95 | bool Stack_Paranthesis::pop(char R_ch) 96 | { 97 | Stack *new_node = top; 98 | if((top->ch=='('&&R_ch==')')||(top->ch=='{'&&R_ch=='}')||(top->ch=='['&&R_ch==']')){ 99 | top=top->next; 100 | delete new_node; 101 | return true; 102 | }else{ 103 | return false; 104 | } 105 | } 106 | int main() 107 | { 108 | int choice; 109 | Stack_Paranthesis operations; 110 | do 111 | { 112 | cout << "1. Result of the Given Expression:" << endl; 113 | cout << "2. Exit" << endl; 114 | cout << endl; 115 | cout << "Enter your Choice: "; 116 | cin >> choice; 117 | switch (choice) 118 | { 119 | case 1: 120 | { 121 | operations.processing_Results(); 122 | break; 123 | } 124 | case 2: 125 | { 126 | break; 127 | } 128 | 129 | default: 130 | break; 131 | } 132 | } while (choice != 2); 133 | 134 | return 0; 135 | } -------------------------------------------------------------------------------- /dequeue.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | insert_at_beg(): inserts an item at the front of Dequeue. 3 | insert_at_end(): inserts an item at the rear of Dequeue. 4 | delete_fr_beg(): Deletes an item from front of Dequeue. 5 | delete_fr_rear(): Deletes an item from rear of Dequeue. 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | #define SIZE 10 11 | class dequeue { 12 | int a[20],f,r; 13 | public: 14 | dequeue(); 15 | void insert_at_beg(int); 16 | void insert_at_end(int); 17 | void delete_fr_front(); 18 | void delete_fr_rear(); 19 | void show(); 20 | }; 21 | dequeue::dequeue() { 22 | f=-1; 23 | r=-1; 24 | } 25 | void dequeue::insert_at_end(int i) { 26 | if(r>=SIZE-1) { 27 | cout<<"\n insertion is not possible, overflow!!!!"; 28 | } else { 29 | if(f==-1) { 30 | f++; 31 | r++; 32 | } else { 33 | r=r+1; 34 | } 35 | a[r]=i; 36 | cout<<"\nInserted item is"<>c; 99 | switch(c) { 100 | case 1: 101 | cout<<"enter the element to be inserted"; 102 | cin>>i; 103 | d.insert_at_beg(i); 104 | break; 105 | case 2: 106 | cout<<"enter the element to be inserted"; 107 | cin>>i; 108 | d.insert_at_end(i); 109 | break; 110 | case 3: 111 | d.show(); 112 | break; 113 | case 4: 114 | d.delete_fr_front(); 115 | break; 116 | case 5: 117 | d.delete_fr_rear(); 118 | break; 119 | case 6: 120 | exit(1); 121 | break; 122 | default: 123 | cout<<"invalid choice"; 124 | break; 125 | } 126 | } while(c!=7); 127 | } 128 | -------------------------------------------------------------------------------- /The Knight’s tour problem_Backtracking.cpp: -------------------------------------------------------------------------------- 1 | //Input : 2 | // N = 8 3 | // Output: 4 | // 0 59 38 33 30 17 8 63 5 | // 37 34 31 60 9 62 29 16 6 | // 58 1 36 39 32 27 18 7 7 | // 35 48 41 26 61 10 15 28 8 | // 42 57 2 49 40 23 6 19 9 | // 47 50 45 54 25 20 11 14 10 | // 56 43 52 3 22 13 24 5 11 | // 51 46 55 44 53 4 21 12 12 | 13 | 14 | 15 | #include 16 | using namespace std; 17 | 18 | #define N 8 19 | 20 | int solveKTUtil(int x, int y, int movei, int sol[N][N], 21 | int xMove[], int yMove[]); 22 | 23 | /* A utility function to check if i,j are 24 | valid indexes for N*N chessboard */ 25 | int isSafe(int x, int y, int sol[N][N]) 26 | { 27 | return (x >= 0 && x < N && y >= 0 && y < N 28 | && sol[x][y] == -1); 29 | } 30 | 31 | /* A utility function to print 32 | solution matrix sol[N][N] */ 33 | void printSolution(int sol[N][N]) 34 | { 35 | for (int x = 0; x < N; x++) { 36 | for (int y = 0; y < N; y++) 37 | cout << " " << setw(2) << sol[x][y] << " "; 38 | cout << endl; 39 | } 40 | } 41 | 42 | /* This function solves the Knight Tour problem using 43 | Backtracking. This function mainly uses solveKTUtil() 44 | to solve the problem. It returns false if no complete 45 | tour is possible, otherwise return true and prints the 46 | tour. 47 | Please note that there may be more than one solutions, 48 | this function prints one of the feasible solutions. */ 49 | int solveKT() 50 | { 51 | int sol[N][N]; 52 | 53 | /* Initialization of solution matrix */ 54 | for (int x = 0; x < N; x++) 55 | for (int y = 0; y < N; y++) 56 | sol[x][y] = -1; 57 | 58 | /* xMove[] and yMove[] define next move of Knight. 59 | xMove[] is for next value of x coordinate 60 | yMove[] is for next value of y coordinate */ 61 | int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; 62 | int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }; 63 | 64 | // Since the Knight is initially at the first block 65 | sol[0][0] = 0; 66 | 67 | /* Start from 0,0 and explore all tours using 68 | solveKTUtil() */ 69 | if (solveKTUtil(0, 0, 1, sol, xMove, yMove) == 0) { 70 | cout << "Solution does not exist"; 71 | return 0; 72 | } 73 | else 74 | printSolution(sol); 75 | 76 | return 1; 77 | } 78 | 79 | /* A recursive utility function to solve Knight Tour 80 | problem */ 81 | int solveKTUtil(int x, int y, int movei, int sol[N][N], 82 | int xMove[8], int yMove[8]) 83 | { 84 | int k, next_x, next_y; 85 | if (movei == N * N) 86 | return 1; 87 | 88 | /* Try all next moves from 89 | the current coordinate x, y */ 90 | for (k = 0; k < 8; k++) { 91 | next_x = x + xMove[k]; 92 | next_y = y + yMove[k]; 93 | if (isSafe(next_x, next_y, sol)) { 94 | sol[next_x][next_y] = movei; 95 | if (solveKTUtil(next_x, next_y, movei + 1, sol, 96 | xMove, yMove) 97 | == 1) 98 | return 1; 99 | else 100 | 101 | // backtracking 102 | sol[next_x][next_y] = -1; 103 | } 104 | } 105 | return 0; 106 | } 107 | 108 | // Driver Code 109 | int main() 110 | { 111 | // Function Call 112 | solveKT(); 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /calculator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | void add(); 7 | void sub(); 8 | void multi(); 9 | void division(); 10 | void sqr(); 11 | void srt(); 12 | void exit(); 13 | void main() 14 | { 15 | clrscr(); 16 | int opr; 17 | // display different operation of the calculator 18 | do 19 | { 20 | cout << "Select any operation from the C++ Calculator" 21 | "\n1 = Addition" 22 | "\n2 = Subtraction" 23 | "\n3 = Multiplication" 24 | "\n4 = Division" 25 | "\n5 = Square" 26 | "\n6 = Square Root" 27 | "\n7 = Exit" 28 | "\n \n Make a choice: "; 29 | cin >> opr; 30 | 31 | switch (opr) 32 | { 33 | case 1: 34 | add(); // call add() function to find the Addition 35 | break; 36 | case 2: 37 | sub(); // call sub() function to find the subtraction 38 | break; 39 | case 3: 40 | multi(); // call multi() function to find the multiplication 41 | break; 42 | case 4: 43 | division(); // call division() function to find the division 44 | break; 45 | case 5: 46 | sqr(); // call sqr() function to find the square of a number 47 | break; 48 | case 6: 49 | srt(); // call srt() function to find the Square Root of the given number 50 | break; 51 | case 7: 52 | exit(0); // terminate the program 53 | break; 54 | default: 55 | cout <<"Something is wrong..!!"; 56 | break; 57 | } 58 | cout <<" \n------------------------------\n"; 59 | }while(opr != 7); 60 | getch(); 61 | } 62 | 63 | void add() 64 | { 65 | int n, sum = 0, i, number; 66 | cout <<"How many numbers you want to add: "; 67 | cin >> n; 68 | cout << "Please enter the number one by one: \n"; 69 | for (i = 1; i <= n; i++) 70 | { 71 | cin >> number; 72 | sum = sum + number; 73 | } 74 | cout << "\n Sum of the numbers = "<< sum; 75 | } 76 | void sub() 77 | { 78 | int num1, num2, z; 79 | cout <<" \n Enter the First number = "; 80 | cin >> num1; 81 | cout << "\n Enter the Second number = "; 82 | cin >> num2; 83 | z = num1 - num2; 84 | cout <<"\n Subtraction of the number = " << z; 85 | } 86 | void multi() 87 | { 88 | int num1, num2, mul; 89 | cout <<" \n Enter the First number = "; 90 | cin >> num1; 91 | cout << "\n Enter the Second number = "; 92 | cin >> num2; 93 | mul = num1 * num2; 94 | cout <<"\n Multiplication of two numbers = " << mul; 95 | } 96 | void division() 97 | { 98 | int num1, num2, div = 0; 99 | cout <<" \n Enter the First number = "; 100 | cin >> num1; 101 | cout << "\n Enter the Second number = "; 102 | cin >> num2; 103 | while ( num2 == 0) 104 | { 105 | cout << "\n Divisor canot be zero" 106 | "\n Please enter the divisor once again: "; 107 | cin >> num2; 108 | } 109 | div = num1 / num2; 110 | cout <<"\n Division of two numbers = " << div; 111 | } 112 | void sqr() 113 | { 114 | int num1; 115 | float sq; 116 | cout <<" \n Enter a number to find the Square: "; 117 | cin >> num1; 118 | sq = num1 * num1; 119 | cout <<" \n Square of " << num1<< " is : "<< sq; 120 | } 121 | void srt() 122 | { 123 | float q; 124 | int num1; 125 | cout << "\n Enter the number to find the Square Root:"; 126 | cin >> num1; 127 | q = sqrt(num1); 128 | cout <<" \n Square Root of " << num1<< " is : "<< q; 129 | } 130 | -------------------------------------------------------------------------------- /Inorder_Traversal.cpp: -------------------------------------------------------------------------------- 1 | /*Traversal in Binary Tree 2 | 3 | Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. 4 | One way of traversing is Depth First Search: 5 | 1 6 | / \ 7 | 2 3 8 | / \ 9 | 4 5 10 | (a) Inorder (Left, Root, Right) : 4 2 5 1 3 11 | (b) Preorder (Root, Left, Right) : 1 2 4 5 3 12 | (c) Postorder (Left, Right, Root) : 4 5 2 3 1 13 | 14 | Inorder Traversal : 15 | 16 | Algorithm Inorder(tree) : 17 | 1. Traverse the left subtree, i.e., call Inorder(left-subtree) 18 | 2. Visit the root. 19 | 3. Traverse the right subtree, i.e., call Inorder(right-subtree) 20 | 21 | Uses of Inorder : 22 | In case of binary search trees (BST), Inorder traversal gives nodes in increasing order. 23 | 24 | Here we discussed both approahes i.e iterative & recursive: 25 | */ 26 | 27 | //Iterative approach: 28 | 29 | #include 30 | using namespace std; 31 | 32 | 33 | // Data structure to store a binary tree node 34 | struct Node 35 | { 36 | int data; 37 | Node *left, *right; 38 | 39 | Node(int data) 40 | { 41 | this->data = data; 42 | this->left = this->right = nullptr; 43 | } 44 | }; 45 | 46 | // Recursive function to perform inorder traversal on the tree 47 | void inorderRecursion(Node* root) 48 | { 49 | // return if the current node is empty 50 | if (root == nullptr) { 51 | return; 52 | } 53 | 54 | // Traverse the left subtree 55 | inorderRecursion(root->left); 56 | 57 | // Display the data part of the root (or current node) 58 | cout << root->data << " "; 59 | 60 | // Traverse the right subtree 61 | inorderRecursion(root->right); 62 | } 63 | 64 | void inorderIterative(Node* root) 65 | { 66 | // create an empty stack 67 | stack stack; 68 | 69 | // start from the root node (set current node to the root node) 70 | Node* curr = root; 71 | 72 | // if the current node is null and the stack is also empty, we are done 73 | while (!stack.empty() || curr != nullptr) 74 | { 75 | // if the current node exists, push it into the stack (defer it) 76 | // and move to its left child 77 | if (curr != nullptr) 78 | { 79 | stack.push(curr); 80 | curr = curr->left; 81 | } 82 | else { 83 | // otherwise, if the current node is null, pop an element from the stack, 84 | // print it, and finally set the current node to its right child 85 | curr = stack.top(); 86 | stack.pop(); 87 | cout << curr->data << " "; 88 | 89 | curr = curr->right; 90 | } 91 | } 92 | } 93 | 94 | 95 | int main() 96 | { 97 | /* Construct the following tree 98 | 1 99 | / \ 100 | / \ 101 | 2 3 102 | / / \ 103 | / / \ 104 | 4 5 6 105 | / \ 106 | / \ 107 | 7 8 108 | */ 109 | 110 | Node* root = new Node(1); 111 | root->left = new Node(2); 112 | root->right = new Node(3); 113 | root->left->left = new Node(4); 114 | root->right->left = new Node(5); 115 | root->right->right = new Node(6); 116 | root->right->left->left = new Node(7); 117 | root->right->left->right = new Node(8); 118 | 119 | inorderRecursion(root); 120 | 121 | 122 | return 0; 123 | } -------------------------------------------------------------------------------- /Preorder_Traversal.cpp: -------------------------------------------------------------------------------- 1 | /*Traversal in Binary Tree 2 | 3 | Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. 4 | One way of traversing is Depth First Search: 5 | 1 6 | / \ 7 | 2 3 8 | / \ 9 | 4 5 10 | (a) Inorder (Left, Root, Right) : 4 2 5 1 3 11 | (b) Preorder (Root, Left, Right) : 1 2 4 5 3 12 | (c) Postorder (Left, Right, Root) : 4 5 2 3 1 13 | 14 | Preorder Traversal : 15 | 16 | Algorithm Preorder(tree) 17 | 1. Visit the root. 18 | 2. Traverse the left subtree, i.e., call Preorder(left-subtree) 19 | 3. Traverse the right subtree, i.e., call Preorder(right-subtree) 20 | 21 | Uses of Preorder 22 | Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree 23 | */ 24 | 25 | #include 26 | using namespace std; 27 | 28 | // Data structure to store a binary tree node 29 | struct Node 30 | { 31 | int data; 32 | Node *left, *right; 33 | 34 | Node(int data) 35 | { 36 | this->data = data; 37 | this->left = this->right = nullptr; 38 | } 39 | }; 40 | 41 | // Recursive function to perform preorder traversal on the tree 42 | void preorderRecursive(Node* root) 43 | { 44 | // if the current node is empty 45 | if (root == nullptr) { 46 | return; 47 | } 48 | 49 | // Display the data part of the root (or current node) 50 | cout << root->data << " "; 51 | 52 | // Traverse the left subtree 53 | preorderRecursive(root->left); 54 | 55 | // Traverse the right subtree 56 | preorderRecursive(root->right); 57 | } 58 | 59 | //Iterative implementation 60 | void preorderIterative(Node* root) 61 | { 62 | // return if the tree is empty 63 | if (root == nullptr) 64 | return; 65 | 66 | // create an empty stack and push the root node 67 | stack stack; 68 | stack.push(root); 69 | 70 | // loop till stack is empty 71 | while (!stack.empty()) 72 | { 73 | // pop a node from the stack and print it 74 | Node* curr = stack.top(); 75 | stack.pop(); 76 | 77 | cout << curr->data << " "; 78 | 79 | // push the right child of the popped node into the stack 80 | if (curr->right) { 81 | stack.push(curr->right); 82 | } 83 | 84 | // push the left child of the popped node into the stack 85 | if (curr->left) { 86 | stack.push(curr->left); 87 | } 88 | 89 | // the right child must be pushed first so that the left child 90 | // is processed first (LIFO order) 91 | } 92 | } 93 | 94 | 95 | 96 | int main() 97 | { 98 | /* Construct the following tree 99 | 1 100 | / \ 101 | / \ 102 | 2 3 103 | / / \ 104 | / / \ 105 | 4 5 6 106 | / \ 107 | / \ 108 | 7 8 109 | */ 110 | 111 | Node* root = new Node(1); 112 | root->left = new Node(2); 113 | root->right = new Node(3); 114 | root->left->left = new Node(4); 115 | root->right->left = new Node(5); 116 | root->right->right = new Node(6); 117 | root->right->left->left = new Node(7); 118 | root->right->left->right = new Node(8); 119 | 120 | preorderRecursive(root); 121 | preorderIterative(root); 122 | 123 | return 0; 124 | } 125 | 126 | -------------------------------------------------------------------------------- /StrassensMM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | vector> Add(vector> A,vector> B,int n){ 5 | vector> Ans(n,vector (n,0)); 6 | int i,j; 7 | for(i=0;i> Multiply(vector> A,vector> B,int n){ 14 | vector> Ans(n,vector (n,0)); 15 | int i,j; 16 | if(n<=2){ 17 | Ans[0][0] = A[0][0]*B[0][0] + A[0][1]*B[1][0]; 18 | Ans[0][1] = A[0][0]*B[0][1] + A[0][1]*B[1][1]; 19 | Ans[1][0] = A[1][0]*B[0][0] + A[1][1]*B[1][0]; 20 | Ans[1][1] = A[1][0]*B[0][1] + A[1][1]*B[1][1]; 21 | } 22 | else{ 23 | vector> A11(n/2,vector (n/2,0)); 24 | vector> A12(n/2,vector (n/2,0)); 25 | vector> A21(n/2,vector (n/2,0)); 26 | vector> A22(n/2,vector (n/2,0)); 27 | 28 | vector> B11(n/2,vector (n/2,0)); 29 | vector> B12(n/2,vector (n/2,0)); 30 | vector> B21(n/2,vector (n/2,0)); 31 | vector> B22(n/2,vector (n/2,0)); 32 | 33 | vector> C11(n/2,vector (n/2,0)); 34 | vector> C12(n/2,vector (n/2,0)); 35 | vector> C21(n/2,vector (n/2,0)); 36 | vector> C22(n/2,vector (n/2,0)); 37 | 38 | //Assigning values to smaller A matrix,Dividing 39 | for(i=0;i> MatrixA(n,vector (n,0)); 91 | vector> MatrixB(n,vector (n,0)); 92 | 93 | //Assigning Matrix 94 | for(i=0;i> MatrixAB(n,vector (n,0)); 117 | MatrixAB = Multiply(MatrixA,MatrixB,n); 118 | cout<<"Answer : \n"; 119 | for(i=0;i 2 | 3 | using namespace std; 4 | 5 | class set_disjoint //Classs Declaration 6 | { 7 | public: 8 | int dset[20]; 9 | 10 | set_disjoint(int v) 11 | { 12 | for(int i=0;i<=v;i++) //for loop 13 | { 14 | dset[i] = i; 15 | } 16 | } 17 | 18 | int search_root(int v) //find root of tree 19 | { 20 | while(v != dset[v]) 21 | { 22 | v = dset[v]; 23 | } 24 | return v; 25 | } 26 | 27 | void take_union(int e1, int e2) //merge vertex edge relation 28 | { 29 | int r1 = search_root(e1); 30 | int r2 = search_root(e2); 31 | 32 | if(e1 == r1 && e2 == r2) 33 | { 34 | dset[e1] = e2; 35 | } 36 | else if(r1 != r1 && e2 == r2) 37 | { 38 | dset[e2] = e1; 39 | } 40 | else if(e1 == r1 && e2!= r2) 41 | { 42 | dset[e1] = e2; 43 | } 44 | else if(e1 != r1 && e2 != r2) 45 | { 46 | dset[r1] = r2; 47 | } 48 | } 49 | }; 50 | 51 | class edge //Classs Declaration 52 | { 53 | public: 54 | int e1; 55 | int e2; 56 | int wt; 57 | }; 58 | 59 | class graph //Class Graph Created 60 | { 61 | public: 62 | int v; //Variable Declaration 63 | int e; 64 | edge ed[20]; 65 | 66 | graph(int vertices, int edges) 67 | { 68 | v = vertices; 69 | e = edges; 70 | } 71 | 72 | void graph_accept(); //Functions 73 | void graph_display(); 74 | void kruskal_mst(); 75 | void edgesort(); 76 | 77 | }; 78 | 79 | void graph::edgesort() //Sort Edge Function 80 | { 81 | edge temp; //arrange in ascending order 82 | for(int i=0;i ed[j+1].wt) 87 | { 88 | temp.e1 = ed[j].e1; 89 | temp.e2 = ed[j].e2; 90 | temp.wt = ed[j].wt; 91 | 92 | ed[j].e1 = ed[j+1].e1; 93 | ed[j].e2 = ed[j+1].e2; 94 | ed[j].wt = ed[j+1].wt; 95 | 96 | ed[j+1].e1 = temp.e1; 97 | ed[j+1].e2 = temp.e2; 98 | ed[j+1].wt = temp.wt; 99 | } 100 | } 101 | } 102 | } 103 | void graph::kruskal_mst() //kruskal Function 104 | { 105 | edge mst[20]; //edge for minimum spanning tree 106 | int mst_ctr = 0; //iterator 107 | int mst_cost = 0; 108 | set_disjoint dj(v); 109 | edgesort(); 110 | cout<<"\n Edges after sorting: "; 111 | graph_display(); 112 | cout<<"\n"; 113 | 114 | for(int i=0;i>ed[i].e1; 143 | cout<<"\n Enter e2 :"; 144 | cin>>ed[i].e2; 145 | cout<<"\n Enter weight :"; 146 | cin>>ed[i].wt; 147 | } 148 | } 149 | 150 | void graph::graph_display() //Display Function 151 | { 152 | for(int i=0;i\n"; 163 | cout<<"\n Enter the number of vertics : "; 164 | cin>>v; 165 | cout<<"\n Enter the number of edges : "; 166 | cin>>e; 167 | 168 | graph g(v, e); //Function Call 169 | g.graph_accept(); 170 | g.graph_display(); 171 | g.kruskal_mst(); 172 | } 173 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![hacktoberfest](hacktoberfest.png) 2 | # Hactoberfest2021 To All the Beginners 3 | # Guys Don't Forget to give star to this repo. 4 | ## Any one can add their programs here ,but program should not be repeated. 5 | ## Your file should contain extension , without extension your PR will be rejected. 6 | ### Example 1 : - 10001PrimeNumber.cpp 7 | ### Example 2 : - LinearSearch.cpp 8 | # you can add searching , sorting , data structures and many more programs. 9 | # Your Code Should be Understandable so if there is need to explain ,do it. 10 | # Don't Add Simple Programs like sum ,Subtraction,multiplication,division etc.. 11 | # Thank You 12 | # Happy Hacking 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 47 | 48 | 53 | 54 | 55 | 56 | 57 | 62 | 63 | 68 | 69 | 70 | 75 | 80 | 81 | 87 | 88 | ## *Dont forget to star this repo and follow me!* 89 | -------------------------------------------------------------------------------- /Quiz Game: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int Guess; 6 | int Total; 7 | 8 | // Question Class 9 | class Question { 10 | private: 11 | string Question_Text; 12 | string Answer_1; 13 | string Answer_2; 14 | string Answer_3; 15 | string Answer_4; 16 | int Correct_Answer; 17 | int Question_Score; 18 | 19 | public: 20 | // Setter Function 21 | void setValues(string, string, 22 | string, string, 23 | string, int, int); 24 | 25 | // Function to ask questions 26 | void askQuestion(); 27 | }; 28 | 29 | // Driver code 30 | int main() 31 | { 32 | cout << "\n\n\t\t\t\tTHE DAILY QUIZ" 33 | << endl; 34 | cout << "\nPress Enter to start " 35 | << "the quiz... " << endl; 36 | 37 | // Input 38 | cin.get(); 39 | 40 | string Name; 41 | int Age; 42 | 43 | // Input the details 44 | cout << "What is your name?" 45 | << endl; 46 | cin >> Name; 47 | cout << endl; 48 | 49 | cout << "How old are you?" 50 | << endl; 51 | cin >> Age; 52 | cout << endl; 53 | 54 | string Respond; 55 | cout << "Are you ready to take" 56 | << " the quiz " << Name 57 | << "? yes/no" << endl; 58 | cin >> Respond; 59 | 60 | if (Respond == "yes") { 61 | cout << endl; 62 | cout << "Good Luck!" << endl; 63 | } 64 | else { 65 | cout << "Okay Good Bye!" << endl; 66 | return 0; 67 | } 68 | 69 | // Objects of Question Class 70 | Question q1; 71 | Question q2; 72 | Question q3; 73 | Question q4; 74 | Question q5; 75 | Question q6; 76 | Question q7; 77 | Question q8; 78 | Question q9; 79 | Question q10; 80 | 81 | // 3 is the position of 82 | // correct answer 83 | q1.setValues("Question : ", "Answer 1", 84 | "Answer 2", "Answer 3", 85 | "Answer 4", 3, 10); 86 | q2.setValues("Question : ", "Answer 1", 87 | "Answer 2", "Answer 3", 88 | "Answer 4", 3, 10); 89 | q3.setValues("Question : ", "Answer 1", 90 | "Answer 2", "Answer 3", 91 | "Answer 4", 3, 10); 92 | q4.setValues("Question : ", "Answer 1", 93 | "Answer 2", "Answer 3", 94 | "Answer 4", 3, 10); 95 | q5.setValues("Question : ", "Answer 1", 96 | "Answer 2", "Answer 3", 97 | "Answer 4", 3, 10); 98 | q6.setValues("Question : ", "Answer 1", 99 | "Answer 2", "Answer 3", 100 | "Answer 4", 3, 10); 101 | q7.setValues("Question : ", "Answer 1", 102 | "Answer 2", "Answer 3", 103 | "Answer 4", 3, 10); 104 | q8.setValues("Question : ", "Answer 1", 105 | "Answer 2", "Answer 3", 106 | "Answer 4", 3, 10); 107 | q9.setValues("Question : ", "Answer 1", 108 | "Answer 2", "Answer 3", 109 | "Answer 4", 3, 10); 110 | q10.setValues("Question : ", "Answer 1", 111 | "Answer 2", "Answer 3", 112 | "Answer 4", 3, 10); 113 | 114 | q1.askQuestion(); 115 | q2.askQuestion(); 116 | q3.askQuestion(); 117 | q4.askQuestion(); 118 | q5.askQuestion(); 119 | q6.askQuestion(); 120 | q7.askQuestion(); 121 | q8.askQuestion(); 122 | q9.askQuestion(); 123 | q10.askQuestion(); 124 | 125 | // Display the total score 126 | cout << "Total Score = " << Total 127 | << "out of 100" << endl; 128 | 129 | // Display the results 130 | 131 | // If the player pass the quiz 132 | if (Total >= 70) { 133 | cout << "Congrats you passed the" 134 | << " quiz!" << endl; 135 | } 136 | 137 | // Otherwise 138 | else { 139 | cout << "Alas! You failed the quiz." 140 | << endl; 141 | cout << "Better luck next time." 142 | << endl; 143 | } 144 | return 0; 145 | } 146 | 147 | // Function to set the values of 148 | // the questions 149 | void Question::setValues( 150 | string q, string a1, 151 | string a2, string a3, 152 | string a4, int ca, int pa) 153 | { 154 | Question_Text = q; 155 | Answer_1 = a1; 156 | Answer_2 = a2; 157 | Answer_3 = a3; 158 | Answer_4 = a4; 159 | Correct_Answer = ca; 160 | Question_Score = pa; 161 | } 162 | 163 | // Function to ask questions 164 | void Question::askQuestion() 165 | { 166 | cout << endl; 167 | 168 | // Print the questions 169 | cout << Question_Text << endl; 170 | cout << "1. " << Answer_1 << endl; 171 | cout << "2. " << Answer_2 << endl; 172 | cout << "3. " << Answer_3 << endl; 173 | cout << "4. " << Answer_4 << endl; 174 | cout << endl; 175 | 176 | // Display the answer 177 | cout << "What is your answer?(in number)" 178 | << endl; 179 | cin >> Guess; 180 | 181 | // If the answer is correct 182 | if (Guess == Correct_Answer) { 183 | cout << endl; 184 | cout << "Correct !" << endl; 185 | 186 | // Update the correct score 187 | Total = Total + Question_Score; 188 | cout << "Score = " << Question_Score 189 | << " out of " 190 | << Question_Score 191 | << "!" << endl; 192 | cout << endl; 193 | } 194 | 195 | // Otherise 196 | else { 197 | cout << endl; 198 | cout << "Wrong !" << endl; 199 | cout << "Score = 0" 200 | << " out of " 201 | << Question_Score 202 | << "!" << endl; 203 | cout << "Correct answer = " 204 | << Correct_Answer 205 | << "." << endl; 206 | cout << endl; 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /Bloom_filter_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class BloomFilter 6 | { 7 | public: 8 | bool arr[100]; 9 | int filter_size; 10 | int filter; 11 | BloomFilter(int n) 12 | { 13 | filter_size = n; 14 | for(int i=0;i9;i--) 138 | { 139 | cout<<" "<0;i--) 142 | { 143 | cout<<" "<0) 152 | { 153 | int t = temp_val; 154 | t = t >> (ctr - 1); 155 | t = t & 1; 156 | cout<<" "<0) && ((filter & shift_val2) >0) && ((filter & shift_val3) >0) && ((filter & shift_val4) >0)) 161 | { 162 | cout<<"\nThe data " <9;i--) 174 | { 175 | cout<<" "<0;i--) 178 | { 179 | cout<<" "<0) 185 | { 186 | int t = filter; 187 | t = t >> (ctr - 1); 188 | t = t & 1; 189 | cout<<" "<>s; 206 | b.search_data1(s); 207 | //b.search_data1(s); 208 | cout<<"\n\n\n Do you want to enter other data(1/0) :"; 209 | cin>>ch; 210 | if(ch==0) 211 | break; 212 | } 213 | return 0; 214 | } 215 | -------------------------------------------------------------------------------- /Primes_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class disjointset 6 | { 7 | public: 8 | int djset[20]; 9 | 10 | disjointset(int v) 11 | { 12 | for(int i=0;i<=v;i++) 13 | { 14 | djset[i] = i; 15 | } 16 | } 17 | 18 | int find_root(int v) 19 | { 20 | while(v != djset[v]) 21 | { 22 | v = djset[v]; 23 | } 24 | return v; 25 | } 26 | 27 | void take_union(int v1, int v2) 28 | { 29 | int r1 = find_root(v1); 30 | int r2 = find_root(v2); 31 | 32 | if(v1 == r1 && v2 == r2) 33 | { 34 | djset[v1] = v2; 35 | } 36 | else if(v1 != r1 && v2 == r2) 37 | { 38 | djset[v2] = v1; 39 | } 40 | else if(v1 == r1 && v2!= r2) 41 | { 42 | djset[v1] = v2; 43 | } 44 | else if(v1 != r1 && v2 != r2) 45 | { 46 | djset[r1] = r2; 47 | } 48 | } 49 | }; 50 | 51 | class edge 52 | { 53 | public: 54 | int v1; 55 | int v2; 56 | int wt; 57 | }; 58 | 59 | class graph 60 | { 61 | public: 62 | int v; 63 | int data[20][20]; 64 | 65 | graph(int vt) 66 | { 67 | v = vt; 68 | } 69 | void prims_algorithm(); 70 | }; 71 | 72 | 73 | void graph::prims_algorithm() 74 | { 75 | int sv; 76 | int visited[v]; 77 | int i,j,k; 78 | edge ed[20]; 79 | edge mst[20]; 80 | edge discarded_edge[20]; 81 | 82 | int mst_ctr = 0; 83 | int edge_ctr =0; 84 | int mst_flag = 0; 85 | int discarded_ctr = 0; 86 | int discarded_flag = 0; 87 | 88 | disjointset d(v); 89 | 90 | for(i=0;i>sv; 96 | sv = sv - 1; 97 | visited[sv] = 1; 98 | 99 | while(1) 100 | { 101 | int visited_flag = 0; 102 | for(i=0;i "<>v; 205 | graph g(v); 206 | 207 | for(int i=0;i>g.data[i][j]; 216 | g.data[j][i] = g.data[i][j]; 217 | } 218 | } 219 | g.prims_algorithm(); 220 | return 0; 221 | } 222 | -------------------------------------------------------------------------------- /Pattern in C++/AlphabetPattern.cpp: -------------------------------------------------------------------------------- 1 | //* Don't Burn Your Opportunities just for Temporary Happiness 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | //*Pattern Questions 9 | 10 | //* Q1. 11 | /* ABCDEDCBA //Alphabet Pattern 1 12 | ABCD DCBA 13 | ABC CBA 14 | AB BA 15 | A A */ 16 | 17 | int t; 18 | for (int i = 1; i <= n; i++) 19 | { 20 | char a = 'A'; 21 | for (int j = n; j >= i; j--) 22 | { 23 | cout<= i; t--) 43 | { 44 | cout<= (n-1)+i) 58 | { 59 | cout<= n-i+1 && j<= (n+i)-1) 90 | { 91 | cout<=n-i+1 && j<= n+i) 119 | { 120 | if (j<=n) 121 | { 122 | cout<= n-i+1 && j<= n+i-1) 151 | { 152 | if (j<=n) 153 | { 154 | cout<= n+i-1) || (j<=n-i+1)) 205 | { 206 | cout<=n-i+1 && j<=n+i-1 && j%2!=0) 231 | { 232 | cout<=n-i+1 && j<=n+i-1 && j%2==0) 240 | { 241 | cout< 2 | using namespace std; 3 | const int N = 4; 4 | 5 | // final_path[] stores the final solution ie, the 6 | // path of the salesman. 7 | int final_path[N+1]; 8 | 9 | // visited[] keeps track of the already visited nodes 10 | // in a particular path 11 | bool visited[N]; 12 | 13 | // Stores the final minimum weight of shortest tour. 14 | int final_res = INT_MAX; 15 | 16 | // Function to copy temporary solution to 17 | // the final solution 18 | void copyToFinal(int curr_path[]) 19 | { 20 | for (int i=0; i lower bound of the root node 60 | // curr_weight-> stores the weight of the path so far 61 | // level-> current level while moving in the search 62 | // space tree 63 | // curr_path[] -> where the solution is being stored which 64 | // would later be copied to final_path[] 65 | void TSPRec(int adj[N][N], int curr_bound, int curr_weight, 66 | int level, int curr_path[]) 67 | { 68 | // base case is when we have reached level N which 69 | // means we have covered all the nodes once 70 | if (level==N) 71 | { 72 | // check if there is an edge from last vertex in 73 | // path back to the first vertex 74 | if (adj[curr_path[level-1]][curr_path[0]] != 0) 75 | { 76 | // curr_res has the total weight of the 77 | // solution we got 78 | int curr_res = curr_weight + 79 | adj[curr_path[level-1]][curr_path[0]]; 80 | 81 | // Update final result and final path if 82 | // current result is better. 83 | if (curr_res < final_res) 84 | { 85 | copyToFinal(curr_path); 86 | final_res = curr_res; 87 | } 88 | } 89 | return; 90 | } 91 | 92 | // for any other level iterate for all vertices to 93 | // build the search space tree recursively 94 | for (int i=0; i 2 | 3 | using namespace std; 4 | 5 | class hash_table //Class hash_table created 6 | { 7 | public: 8 | 9 | int table_hash[50]; //Array Creation 10 | int table_hash_size; //Create hash value size 11 | int ctr; 12 | hash_table(int n) 13 | { 14 | table_hash_size =8; 15 | ctr = 0; 16 | for(int i=0;i"; 170 | do //do while statement 171 | { 172 | cout<<"\n\n1.Insert Flavour_ID\n2.Search Flavour\n3.Display Flavour\n4.Exit\n"; 173 | cout<<"\nEnter your choice:-\n"; 174 | cin>>ch; 175 | cout<>value; 187 | h.store_value_linear_probin_with_replacemet(value); 188 | cout<<"\n Do you want to continue (1/0) : "; 189 | cin>>choice; 190 | if(choice == 0) 191 | break; 192 | } 193 | } 194 | } 195 | break; 196 | case 2: //Case 2 197 | { 198 | int ch2,val; 199 | while(1) 200 | { 201 | cout<<"\n Enter the Flavour_ID to be searched: "; 202 | cin>>val; 203 | h.search_value_in_hash_table_hash_linear_probing(val); 204 | cout<<"\n Do you want to search other value (1/0) : "; 205 | cin>>ch2; 206 | if(ch2 == 0) 207 | break; 208 | } 209 | break; 210 | } 211 | 212 | case 3: //Case 3 213 | { 214 | if(ch==3) 215 | h.display_hash_table_hash_linear_probing(); 216 | } 217 | break; 218 | case 4: //Case 4 219 | exit(0); 220 | } 221 | }while(ch!=4); 222 | 223 | return 0; 224 | } 225 | --------------------------------------------------------------------------------

StutiSharma27
Contributor

roughking07
Contributor

Pratik29121
Contributor

ShreyasV7
Contributor

SiddharthBhamare01
Contributor

arjunpundir1106
Contributor

learnerpk
Contributor

AshishSingh13s
Contributor

vishalrajofficial
Contributor

hunt-s7
Contributor

Debanjan
Contributor

tarunsingh7379
Contributor

NehalAgrawal186
Contributor

SiddharthBhamare01
Contributor

zomsik
Contributor

the-artemis
Contributor
43 |
44 | AbhirupB 45 |
Contributor 46 |
49 |
50 | Tushar00728 51 |
Contributor 52 |
58 |
59 | Juanes2424 60 |
Contributor 61 |
64 |
65 | Meghasinha234 66 |
Contributor 67 |
71 |
72 | 12345radha 73 |
Contributor 74 |
76 |
77 | tajenderkumar5 78 |
Contributor 79 |
82 |
83 | mohdanwaroddin 84 |
Contributor 85 |