├── tries.cpp ├── README.md ├── TEMP.CPP ├── powerof2.cpp ├── sortinginstr.cpp ├── biggest.cpp ├── pointers.cpp ├── reversestring.cpp ├── incexc.cpp ├── tiling.cpp ├── friendspairing.cpp ├── dowhile.cpp ├── gcd.cpp ├── divisible.cpp ├── DP_friendspair.cpp ├── recursionarraysort.cpp ├── recursionfibonacci.cpp ├── numberof1s.cpp ├── oddeven.cpp ├── unique1.cpp ├── pointersinfunc.cpp ├── recursivesum.cpp ├── substrings.cpp ├── recursionpower.cpp ├── pattern3.cpp ├── pattern8.cpp ├── recursionfactorial.cpp ├── towerofhanoi.cpp ├── moveallxtoend.cpp ├── pattern5.cpp ├── forloop.cpp ├── removeduplicates.cpp ├── sumfirstnnumb.cpp ├── reverse.cpp ├── chararray.cpp ├── subsets.cpp ├── maze.cpp ├── factorial.cpp ├── pattern6.cpp ├── subASCII.cpp ├── prime.cpp ├── permutestring.cpp ├── pattern1.cpp ├── set.cpp ├── gameboard.cpp ├── jumps.cpp ├── pattern10.cpp ├── whileloop.cpp ├── deque.cpp ├── maxtilli.cpp ├── pattern11.cpp ├── replacepi.cpp ├── longestsubstring.cpp ├── maxandmindifference.cpp ├── nCr.cpp ├── unorderedset.cpp ├── octaltodecimal.cpp ├── binarytodecimal.cpp ├── pattern4.cpp ├── pattern9.cpp ├── ifelse.cpp ├── wordsfromphone.cpp ├── primenew.cpp ├── binaryintro.cpp ├── checkpalindrome.cpp ├── fibenaccisequence.cpp ├── knapsack.cpp ├── multiset.cpp ├── printprimes.cpp ├── sumallsubarrays.cpp ├── DP_uglynumbers.cpp ├── pattern14.cpp ├── sieve.cpp ├── incdec.cpp ├── reverseusingstack.cpp ├── encapsulation.cpp ├── maxminarray.cpp ├── pascaltriangle.cpp ├── decimaltobinary.cpp ├── decimaltooctal.cpp ├── primefunction.cpp ├── maxfrequency.cpp ├── pattern12.cpp ├── pattern2.cpp ├── max.cpp ├── armstrong.cpp ├── hashingstl.cpp ├── wavesort.cpp ├── getman.cpp ├── recordbreaker.cpp ├── DP_countnumbinarystring.cpp ├── primefactorisation.cpp ├── linearsearch.cpp ├── switch.cpp ├── hexadecimaltodecimal.cpp ├── DP_numberofsubseq.cpp ├── longestarithmeticarray.cpp ├── pattern13.cpp ├── unique3.cpp ├── findpeakelement.cpp ├── maxconsecutiveones.cpp ├── DP_minjumpstoreachend.cpp ├── stockspan.cpp ├── largestrectangle.cpp ├── catalan.cpp ├── transposesquarematrix.cpp ├── trappingrainwater.cpp ├── DP_optimalgamestrategy.cpp ├── allcasessame.cpp ├── calculator.cpp ├── vector.cpp ├── pythagoreantriplet.cpp ├── countallnodes.cpp ├── sumallnodes.cpp ├── decimaltohexadecimal.cpp ├── maxsumsubarray.cpp ├── DP_0nknapsack.cpp ├── DP_kadanealgo.cpp ├── firstrepeatingelement.cpp ├── unique2.cpp ├── DNFsort.cpp ├── search.cpp ├── searchingtwodarray.cpp ├── largestword.cpp ├── subarraywithgivensum.cpp ├── insertionsort.cpp ├── reversestack.cpp ├── redundantparentheses.cpp ├── twoDarray.cpp ├── selectionsort.cpp ├── smallest+missingnum.cpp ├── DP_maxlengthbitonicsubseq.cpp ├── countsort.cpp ├── DP_KorderedLCS.cpp ├── countfreq.cpp ├── pair.cpp ├── permutation.cpp ├── pattern7.cpp ├── DFS.cpp ├── DP_LCSwith3strings.cpp ├── bubblesort.cpp ├── quicksort.cpp ├── topologicalsort.cpp ├── heightinbinarytree.cpp ├── binarysearch.cpp ├── heapstl.cpp ├── indiancoinchange.cpp ├── permutation2.cpp ├── BFS.cpp ├── 3sumproblem.cpp ├── searchinBST.cpp ├── bellmanfordalgo.cpp ├── matrixmulti.cpp ├── minsubarraysize.cpp ├── connectedcompo.cpp ├── floydwarshallalgo.cpp ├── spriralordermatrixrev.cpp ├── queueusingstack2.cpp ├── arrstack.cpp ├── hashslidingwind.cpp ├── smallestsubsequencesumk.cpp ├── subarraysum0.cpp ├── bipartitegraph.cpp ├── activityselection.cpp ├── stackusingqueue.cpp ├── optimalmerge.cpp ├── postfix.cpp ├── prefix.cpp ├── searchsortrotatedarr.cpp ├── maxpathsum.cpp ├── connectedcompoquestion.cpp ├── DP_Fibonacci.cpp ├── maxmindistance.cpp ├── painter'spartition.cpp ├── queueusingstack.cpp ├── cycledetectundirectgraph.cpp ├── levelordertraversal.cpp ├── leftview.cpp ├── checkBST.cpp ├── formnumdivby3.cpp ├── DP_01knapsack.cpp ├── dijkstraalgo.cpp ├── factorialknapsack.cpp ├── cycledetectdirectgraph.cpp ├── sumreplacement.cpp ├── balancedparenthesis.cpp ├── heapssort.cpp ├── sumatkthnode.cpp ├── DP_minnumbsquare.cpp ├── binarytraversal.cpp ├── string.cpp ├── rightview.cpp ├── palindromicconcatenation.cpp ├── flattenBT.cpp ├── slidingwindowmax.cpp ├── primsalgoMST.cpp ├── topkmostfreq.cpp ├── mergesort.cpp ├── infix2postfix.cpp ├── allocateminpages.cpp ├── indenticalBST.cpp ├── treebypostandinorder.cpp ├── treebypreandinorder.cpp ├── evenoddll.cpp ├── perfectnumbers.cpp ├── DP_matrixchainmultiplication.cpp ├── DP_coinchangeprob.cpp ├── DP_longestincreasingsubseq.cpp ├── stackusingqueue2.cpp ├── cycledetectundirgaphDSU.cpp ├── kruskalalgoMST.cpp ├── largestBSTinBT.cpp ├── mergeksortedarray.cpp ├── arrqueue.cpp ├── possibleBSTs.cpp ├── shortestdistance.cpp ├── reverseknodes.cpp ├── countinversionmerge.cpp ├── graphrepresent.cpp ├── llqueue.cpp ├── ratinmaze.cpp ├── infix2prefix.cpp ├── verticalorderprint.cpp ├── DP_longestcommonsubseq.cpp ├── expedi.cpp └── surroundedregion.cpp /tries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | return 0; 5 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # apnacollege_courseyt 2 | These are the codes written by me following the Apna College Course when I was in second year of my college. All the codes are written in C++ for my reference. 3 | -------------------------------------------------------------------------------- /TEMP.CPP: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // This program is to understand use of pointers in the function. 4 | 5 | 6 | int main(){ 7 | int a=2, b; 8 | b=a++ + ++a + a + a++; 9 | cout< 2 | using namespace std; 3 | // Program to find wheather a number is a function of 2. 4 | int ispowerof2(int n){ 5 | return (n && !(n & n-1)); 6 | } 7 | int main(){ 8 | cout< 2 | using namespace std; 3 | // This program is to understand sorting in string. 4 | int main(){ 5 | string s1="baiwhqpbxmspq"; 6 | sort(s1.begin(),s1.end()); // #include 7 | cout< 2 | using namespace std; 3 | // This program is to make biggest number from a number string. 4 | int main(){ 5 | string s="1084986475"; 6 | sort(s.begin(),s.end(),greater()); //greater 7 | cout< 2 | using namespace std; 3 | // This program is to understand pointers. 4 | int main(){ 5 | int a=10; 6 | int *aptr=&a; 7 | 8 | cout<<*aptr< 2 | using namespace std; 3 | void reverse(string s){ 4 | if(s.length()==0){ 5 | return; 6 | } 7 | string ros=s.substr(1); 8 | reverse(ros); 9 | cout< 2 | using namespace std; 3 | int divisible(int n, int a, int b){ 4 | int c1=n/a; 5 | int c2=n/b; 6 | int c3=n/(a*b); 7 | return c1+c2-c3; 8 | } 9 | int main(){ 10 | int n, a, b; 11 | cin>>n>>a>>b; 12 | cout< 2 | using namespace std; 3 | int tilingWays(int n) 4 | { 5 | if (n == 0) 6 | return 0; 7 | if (n == 1) 8 | return 1; 9 | return tilingWays(n - 1) + tilingWays(n - 2); 10 | } 11 | int main() 12 | { 13 | cout << tilingWays(4) << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /friendspairing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int friendsPairing(int n) 4 | { 5 | if (n == 0 || n == 1 || n==2) 6 | return n; 7 | return friendsPairing(n - 1) + friendsPairing(n - 2)*(n-1); 8 | } 9 | int main() 10 | { 11 | cout << friendsPairing(4) << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /dowhile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | // This program is for understanding "do while loop". 6 | int num; 7 | cout<<"ENTER NUMBER: \n"; 8 | cin>>num; 9 | 10 | do{ 11 | cout<>num; 13 | }while (num>0); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /gcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Eulid algorithm 4 | int gcd(int a, int b){ 5 | while(b!=0){ 6 | int rem=a%b; 7 | a=b; 8 | b=rem; 9 | } 10 | return a; 11 | } 12 | 13 | int main(){ 14 | int a, b; 15 | cin>>a>>b; 16 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program is for printing numbers upto 100 which 6 | are not divisible by 3. */ 7 | 8 | for(int i=0;i<=100;i++){ 9 | if(i%3==0){ 10 | continue; 11 | } 12 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin >> n; 7 | vector dp(n + 1, 0); 8 | dp[0] = 1; 9 | dp[1] = 1; 10 | for (int i = 2; i <= n; i++) 11 | { 12 | dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]; 13 | } 14 | cout << dp[n]; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /recursionarraysort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool sorted(int arr[],int n){ 4 | if(n==1){ 5 | return true; 6 | } 7 | bool restArray=sorted(arr+1,n-1); 8 | return (arr[0] 2 | using namespace std; 3 | // Program for fibonacci calculation using recursion. 4 | 5 | int fib(int n){ 6 | if(n==0 || n==1){ 7 | return n; 8 | } 9 | return fib(n-1)+fib(n-2); 10 | } 11 | 12 | int main(){ 13 | int n; 14 | cin>>n; 15 | cout< 2 | using namespace std; 3 | // Program to find number of one's in a binary form of a number. 4 | int numberofones(int n){ 5 | int count=0; 6 | while(n){ 7 | n=n & (n-1); 8 | count++; 9 | } 10 | return count; 11 | } 12 | int main(){ 13 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | // This program is about finding odd and even. 6 | int num; 7 | cout<<"ENTER NUMBER: \n"; 8 | cin>>num; 9 | 10 | if(num%2==0){ 11 | cout<<"Number is EVEN \n"; 12 | }else{ 13 | cout<<"Number is ODD \n"; 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /unique1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Program to find the unique number. 4 | int unique(int arr[],int n){ 5 | int xorsum=0; 6 | for(int i=0;i 2 | using namespace std; 3 | // This program is to understand use of pointers in the function. 4 | void swap(int *a,int *b){ 5 | int temp=*a; 6 | *a=*b; 7 | *b=temp; 8 | } 9 | 10 | int main(){ 11 | int a=2; 12 | int b=4; 13 | 14 | swap(&a,&b); 15 | cout< 2 | using namespace std; 3 | // Program to understand recursion in c++ 4 | 5 | int sum(int n){ 6 | if(n==0){ // or 1 7 | return 0; // or 1 8 | } 9 | int prevsum= sum(n-1); 10 | return n+prevsum; 11 | } 12 | 13 | int main(){ 14 | int n; 15 | cin>>n; 16 | cout< 2 | using namespace std; 3 | void subseq(string s,string ans){ 4 | if(s.length()==0){ 5 | cout< 2 | using namespace std; 3 | // Program to find power of a number using recursion. 4 | 5 | int power(int n, int p){ 6 | if(p==0){ 7 | return 1; 8 | } 9 | int prevpow=power(n,p-1); 10 | return n*prevpow; 11 | } 12 | 13 | int main(){ 14 | int n, p; 15 | cin>>n>>p; 16 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(inverted half pyramid). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | for(int i=n;i>=1;i--){ 11 | for(int j=1;j<=i;j++){ 12 | cout<<"*"; 13 | } 14 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(inverted pattern). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | for(int i=1;i<=n;i++){ 11 | for(int j=1;j<=n+1-i;j++){ 12 | cout< 2 | using namespace std; 3 | // Program for factorial calculation using recursion. 4 | 5 | int factorial(int n){ 6 | if(n==0){ 7 | return 1; 8 | } 9 | int prevfact= factorial(n-1); 10 | return n*prevfact; 11 | } 12 | 13 | int main(){ 14 | int n; 15 | cin>>n; 16 | cout< 2 | using namespace std; 3 | void towerOfHanoi(int n, char src, char dest,char helper){ 4 | if(n==0){ 5 | return; 6 | } 7 | towerOfHanoi(n-1,src,helper,dest); 8 | cout<<"Move from "< 2 | using namespace std; 3 | string moveAllX(string s){ 4 | if(s.length()==0){ 5 | return ""; 6 | } 7 | char ch=s[0]; 8 | string ans=moveAllX(s.substr(1)); 9 | if(ch=='x'){ 10 | return ans+ch; 11 | } 12 | return (ch+ans); 13 | } 14 | int main(){ 15 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(half pyramid using numbers). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | for(int i=1;i<=n;i++){ 11 | for(int j=1;j<=i;j++){ 12 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | // This program is for understanding "for loop". 6 | int num; 7 | cout<<"ENTER NUMBER: \n"; 8 | cin>>num; 9 | 10 | int sum=0; 11 | for(int counter=1;counter<=num;counter++){ 12 | sum=sum+counter; 13 | } 14 | cout<<"SUM of first "< 2 | using namespace std; 3 | string removeDup(string s){ 4 | if(s.length()==0){ 5 | return ""; 6 | } 7 | char ch=s[0]; 8 | string ans=removeDup(s.substr(1)); 9 | if(ch==ans[0]){ 10 | return ans; 11 | } 12 | return (ch+ans); 13 | } 14 | int main(){ 15 | cout< 2 | using namespace std; 3 | /* This program is for calculating sum of first n natural numbers. */ 4 | int sum(int n){ 5 | int ans=0; 6 | for(int i=1;i<=n;i++){ 7 | ans+=i; 8 | } 9 | return ans; 10 | 11 | } 12 | int main(){ 13 | int n; 14 | cout<<"Enter a number: "<>n; 16 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program finds reverse of a number. */ 6 | int n; 7 | cout<<"Enter any number:"<>n; 9 | int reverse=0; 10 | 11 | while(n>0){ 12 | int lastdigit=n%10; 13 | reverse=reverse*10+lastdigit; 14 | n=n/10; 15 | } 16 | cout< 2 | using namespace std; 3 | /* This program is for understanding character array. */ 4 | 5 | int main(){ 6 | // char arr[100]="apple"; 7 | // int i=0; 8 | // while(arr[i]!='\0'){ 9 | // cout<>arr; 15 | // cout< 2 | using namespace std; 3 | // Program to find number of subsets. 4 | void subsets(int arr[],int n){ 5 | for(int i=0;i<(1< 2 | using namespace std; 3 | int countPathMaze(int n, int i, int j) 4 | { 5 | if (i == n - 1 && j == n - 1) 6 | { 7 | return 1; 8 | } 9 | if (i >= n || j >= n) 10 | { 11 | return 0; 12 | } 13 | return countPathMaze(n, i + 1, j) + countPathMaze(n, i, j + 1); 14 | } 15 | int main() 16 | { 17 | cout << countPathMaze(3, 0, 0) << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void fact(int num){ 6 | int product=1; 7 | for(int i=2;i<=num;i++){ 8 | product*=i; 9 | } 10 | cout<>n; 18 | fact(n); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /pattern6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(floyd's triangle). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | int count=1; 10 | 11 | for(int i=1;i<=n;i++){ 12 | for(int j=1;j<=i;j++){ 13 | cout< 2 | using namespace std; 3 | void subseq(string s,string ans){ 4 | if(s.length()==0){ 5 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program is for finding prime number. */ 6 | int num; 7 | cout<<"Enter A Number:"<>num; 9 | int i; 10 | for(i=2;i 2 | using namespace std; 3 | void peramutation(string s,string ans){ 4 | if(s.length()==0){ 5 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(rectangle). */ 6 | int row,col; 7 | cout<<"Enter number of rows :"<>row; 9 | cout<<"Enter number of columns :"<>col; 11 | 12 | for(int i=1;i<=row;i++){ 13 | for(int j=1;j<=col;j++){ 14 | cout<<"*"; 15 | } 16 | cout< 2 | #include 3 | using namespace std; 4 | int32_t main() 5 | { 6 | set s; 7 | s.insert(1); 8 | s.insert(2); 9 | s.insert(2); 10 | s.insert(3); 11 | for (auto i : s) 12 | { 13 | cout << i << " "; 14 | } 15 | cout << endl; 16 | cout << s.size() << "\n"; 17 | s.erase(2); 18 | for (auto i : s) 19 | { 20 | cout << i << " "; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /gameboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int countPath(int s, int e) 4 | { 5 | if (s == e) 6 | { 7 | return 1; 8 | } 9 | if (s > e) 10 | { 11 | return 0; 12 | } 13 | int count = 0; 14 | for (int i = 1; i <= 6; i++) 15 | { 16 | count += countPath(s + i, e); 17 | } 18 | return count; 19 | } 20 | int main() 21 | { 22 | cout << countPath(0, 3) << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /jumps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | // This program is for understanding jumps in c++. 6 | 7 | int pocketmoney=3000; 8 | for(int date=1;date<=30;date++){ 9 | if(date%2==0){ 10 | continue; 11 | } 12 | if(pocketmoney==0){ 13 | break; 14 | } 15 | cout<<"Go out today!! \n"; 16 | pocketmoney=pocketmoney-300; 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /pattern10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(rhombus pattern ). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | for(int i=1;i<=n;i++){ 11 | for(int j=1;j<=(n-i);j++){ 12 | cout<<" "; 13 | } 14 | for(int j=1;j<=n;j++){ 15 | cout<<"*"; 16 | } 17 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | // This program is for understanding "while loop". 6 | int num; 7 | cout<<"ENTER NUMBER: \n"; 8 | cin>>num; 9 | 10 | while(num>0){ 11 | cout<<"This Number is Positive!!! \n"; 12 | cout<<"Please ENTER a NUMBER AGAIN : \n"; 13 | cin>>num; 14 | cout<<"\n"; 15 | } 16 | cout<<"This Number is Negative!!! \n"; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /deque.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | deque dq; 6 | dq.push_back(1); 7 | dq.push_back(2); 8 | dq.push_front(3); 9 | dq.push_front(4); 10 | for(auto i : dq){ 11 | cout< 2 | using namespace std; 3 | /* This program is for finding maximum element till ith index of ARRAY*/ 4 | 5 | int main(){ 6 | int n; 7 | int mx=INT_MIN; 8 | cout<<"Enter the size of array: "<>n; 10 | int arr[n]; 11 | for(int i=0;i>arr[i]; 13 | } 14 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(number pattern). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | 11 | for(int i=1;i<=n;i++){ 12 | for(int j=1;j<=n-i;j++){ 13 | cout<<" "; 14 | } 15 | for(int j=1;j<=i;j++){ 16 | cout< 2 | using namespace std; 3 | void replacePi(string s) 4 | { 5 | if (s.length() == 0) 6 | { 7 | return; 8 | } 9 | if (s[0] == 'p' && s[1] == 'i') 10 | { 11 | cout << "3.14"; 12 | replacePi(s.substr(2)); 13 | } 14 | else 15 | { 16 | cout << s[0]; 17 | replacePi(s.substr(1)); 18 | } 19 | } 20 | int main() 21 | { 22 | replacePi("pippppiiiipi"); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /longestsubstring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | string s; 6 | cin >> s; 7 | vector dict(256, -1); 8 | int maxLen = 0, start = -1; 9 | for (int i = 0; i < s.size(); i++) 10 | { 11 | if (dict[s[i]] > start) 12 | { 13 | start = dict[s[i]]; 14 | } 15 | dict[s[i]] = i; 16 | maxLen = max(maxLen, i - start); 17 | } 18 | cout << maxLen; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /maxandmindifference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (auto &i : a) 9 | { 10 | cin >> i; 11 | } 12 | sort(a.begin(), a.end()); 13 | long long mn = 0, mx = 0; 14 | for (int i = 0; i < n / 2; i++) 15 | { 16 | mx += (a[i + n / 2] - a[i]); 17 | mn += (a[2 * i + 1] - a[2 * i]); 18 | } 19 | cout << mn << " " << mx; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /nCr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int fact(int num){ 6 | int product=1; 7 | for(int i=2;i<=num;i++){ 8 | product*=i; 9 | } 10 | return product; 11 | } 12 | int main(){ 13 | /* This program prints combination of a number using function. */ 14 | int n,r; 15 | cout<<"Enter two numbers: "<>n>>r; 17 | 18 | cout<<"combination is: "< 2 | #include 3 | using namespace std; 4 | int32_t main() 5 | { 6 | unordered_set s; 7 | s.insert(1); 8 | s.insert(2); 9 | s.insert(3); 10 | s.insert(3); 11 | for (auto i : s) 12 | { 13 | cout << i << " "; 14 | } 15 | cout << endl; 16 | cout << s.size() << "\n"; 17 | s.erase(s.find(3)); 18 | for (auto i : s) 19 | { 20 | cout << i << " "; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /octaltodecimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for converting octal number to decimal number. */ 4 | int octaltodecimal(int n){ 5 | int ans=0; 6 | int x=1; 7 | while(n>0){ 8 | int y=n%10; 9 | ans+=x*y; 10 | x*=8; 11 | n/=10; 12 | } 13 | return ans; 14 | 15 | } 16 | int main(){ 17 | int n; 18 | cout<<"Enter a OCTAL number: "<>n; 20 | cout< 2 | using namespace std; 3 | /* This program is for converting binary number to decimal number. */ 4 | int binarytodecimal(int n){ 5 | int ans=0; 6 | int x=1; 7 | while(n>0){ 8 | int y=n%10; 9 | ans+=x*y; 10 | x*=2; 11 | n/=10; 12 | } 13 | return ans; 14 | 15 | } 16 | int main(){ 17 | int n; 18 | cout<<"Enter a binary number: "<>n; 20 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(half pyramid after 180 degree rotation). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | for(int i=1;i<=n;i++){ 11 | for(int j=1;j<=n;j++){ 12 | if(j<=n-i){ 13 | cout<<" "; 14 | }else{ 15 | cout<<"*"; 16 | } 17 | } 18 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(0-1 Pattern). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | for(int i=1;i<=n;i++){ 11 | for(int j=1;j<=i;j++){ 12 | if((j+i)%2==0){ 13 | cout<<1<<" "; 14 | }else{ 15 | cout<<0<<" "; 16 | } 17 | 18 | } 19 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | // This program is for understanding if/else concept. 6 | int savings; 7 | cin>>savings; 8 | 9 | if(savings>5000){ 10 | if(savings>10000){ 11 | cout<<"Road Trip With Neha \n"; 12 | }else{ 13 | cout<<"Shopping With Neha \n"; 14 | } 15 | }else if(savings>2000){ 16 | cout<<"Rashmi \n"; 17 | }else{ 18 | cout<<"Friends \n"; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /wordsfromphone.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | string keypadArr[]={"","./","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 4 | void keypad(string s,string ans){ 5 | if(s.length()==0){ 6 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | /* This program finds wheather a number is prime or not. */ 7 | int n; 8 | cout<<"Enter any number:"<>n; 10 | bool flag=0; 11 | 12 | for(int i=2;i 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | int main() 16 | { 17 | struct Node *root = new Node(1); 18 | root->left = new Node(2); 19 | root->right = new Node(3); 20 | 21 | root->left->left = new Node(4); 22 | root->left->right = new Node(5); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /checkpalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for checking palindrome. */ 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | char arr[n+1]; 9 | cin>>arr; 10 | bool check=true; 11 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void fib(int num){ 6 | int t1=0; 7 | int t2=1; 8 | int nextterm; 9 | for(int i=1;i<=num;i++){ 10 | cout<>n; 22 | fib(n); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int knapsack(int value[], int wt[], int n, int W) 4 | { 5 | if (n == 0 || W == 0) 6 | return 0; 7 | if(wt[n-1]>W){ 8 | return knapsack(value, wt, n - 1, W); 9 | } 10 | return max(knapsack(value, wt, n - 1, W - wt[n - 1]) + value[n - 1], knapsack(value, wt, n - 1, W)); 11 | } 12 | int main() 13 | { 14 | int wt[]={10,20,30}; 15 | int value[]={100,50,150}; 16 | int W=50; 17 | cout << knapsack(value,wt,3,W) << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /multiset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int32_t main() 5 | { 6 | multiset s; 7 | s.insert(1); 8 | s.insert(2); 9 | s.insert(3); 10 | s.insert(3); 11 | s.insert(3); 12 | s.insert(3); 13 | s.insert(3); 14 | for (auto i : s) 15 | { 16 | cout << i << " "; 17 | } 18 | cout << endl; 19 | cout << s.size() << "\n"; 20 | s.erase(s.find(3)); 21 | for (auto i : s) 22 | { 23 | cout << i << " "; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /printprimes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program is for printing prime numbers between two given numbers. */ 6 | int num1,num2; 7 | cout<<"Enter Two Numbers:"<>num1>>num2; 9 | 10 | for(int i=num1;i<=num2;i++){ 11 | int j; 12 | for(j=2;j 2 | using namespace std; 3 | /* This program is for finding sum of all elements of all subarrays.*/ 4 | 5 | int main(){ 6 | int n; 7 | int sum; 8 | cout<<"Enter the size of array: "<>n; 10 | int arr[n]; 11 | for(int i=0;i>arr[i]; 13 | } 14 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int n = 10; 6 | int c2(0), c3(0), c5(0); 7 | vector dp(n + 1); 8 | dp[0] = 1; 9 | for (int i = 1; i <= n; i++) 10 | { 11 | dp[i] = min({2 * dp[c2], 3 * dp[c3], 5 * dp[c5]}); 12 | if (2 * dp[c2] == dp[i]) 13 | c2++; 14 | if (3 * dp[c3] == dp[i]) 15 | c3++; 16 | if (5 * dp[c5] == dp[i]) 17 | c5++; 18 | } 19 | cout << dp[n - 1] << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /pattern14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(zig-zag pattern). */ 6 | int n; 7 | cout<<"Enter any number:"<>n; 9 | 10 | 11 | for(int i=1;i<=3;i++){ 12 | for(int j=1;j<=n;j++){ 13 | if((i+j)%4==0 || (i==2 && j%4==0)){ 14 | cout<<"* "; 15 | } 16 | else{ 17 | cout<<" "; 18 | } 19 | } 20 | cout< 2 | using namespace std; 3 | // Program for sieve of eratosthenes. 4 | void primeSieve(int n){ 5 | int prime[100]={0}; 6 | for(int i=2;i<=n;i++){ 7 | if(prime[i]==0){ 8 | for(int j=i*i;j<=n;j+=i){ 9 | prime[j]=1; 10 | } 11 | } 12 | } 13 | for(int i=2;i<=n;i++){ 14 | if(prime[i]==0){ 15 | cout<>n; 22 | primeSieve(n); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /incdec.cpp: -------------------------------------------------------------------------------- 1 | // #include 2 | // using namespace std; 3 | // void dec(int n){ 4 | // if(n==0){ 5 | // return; 6 | // } 7 | // cout<>n; 13 | // dec(n); 14 | // return 0; 15 | // } 16 | #include 17 | using namespace std; 18 | void inc(int n){ 19 | if(n==0){ 20 | return; 21 | } 22 | inc(n-1); 23 | cout<>n; 28 | inc(n); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /reverseusingstack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void reverseSentence(string s){ 5 | stack st; 6 | for(int i=0;i 2 | using namespace std; 3 | class A 4 | { 5 | public: 6 | int a; 7 | void funcA() 8 | { 9 | cout << "Func A\n"; 10 | } 11 | 12 | private: 13 | int b; 14 | void funcB() 15 | { 16 | cout << "Func B\n"; 17 | } 18 | 19 | protected: 20 | int c; 21 | void funcC() 22 | { 23 | cout << "Func C\n"; 24 | } 25 | }; 26 | int main() 27 | { 28 | A obj; 29 | obj.funcA(); 30 | // obj.funcB(); error for private 31 | // obj.funcC(); error for protected 32 | return 0; 33 | } -------------------------------------------------------------------------------- /maxminarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for finding maximum and minimum element given in ARRAY */ 4 | 5 | int main(){ 6 | int n; 7 | cout<<"Enter the size of array: "<>n; 9 | int arr[n]; 10 | for(int i=0;i>arr[i]; 12 | } 13 | int maxno=INT_MIN; 14 | int minno=INT_MAX; 15 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int fact(int num){ 6 | int product=1; 7 | for(int i=2;i<=num;i++){ 8 | product*=i; 9 | } 10 | return product; 11 | } 12 | int main(){ 13 | /* This program prints pascal's triangle using function. */ 14 | int n; 15 | cout<<"Enter a number: "<>n; 17 | for(int i=0;i 2 | using namespace std; 3 | /* This program is for converting decimal number to binary number. */ 4 | int decimaltobinary(int n){ 5 | int ans=0; 6 | int x=1; 7 | while(x<=n){ 8 | x*=2; 9 | } 10 | x/=2; 11 | while(x>0){ 12 | int lastdigit=n/x; 13 | n-=lastdigit*x; 14 | x/=2; 15 | ans=ans*10 + lastdigit; 16 | } 17 | return ans; 18 | } 19 | int main(){ 20 | int n; 21 | cout<<"Enter a decimal number: "<>n; 23 | cout< 2 | using namespace std; 3 | /* This program is for converting decimal number to octal number. */ 4 | int decimaltooctal(int n){ 5 | int ans=0; 6 | int x=1; 7 | while(x<=n){ 8 | x*=8; 9 | } 10 | x/=8; 11 | while(x>0){ 12 | int lastdigit=n/x; 13 | n-=lastdigit*x; 14 | x/=8; 15 | ans=ans*10 + lastdigit; 16 | } 17 | return ans; 18 | } 19 | int main(){ 20 | int n; 21 | cout<<"Enter a decimal number: "<>n; 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool isprime(int num){ 6 | for(int i=2;i<=sqrt(num);i++){ 7 | if(num%i==0){ 8 | return false; 9 | } 10 | } 11 | return true; 12 | } 13 | int main(){ 14 | /* This program finds wheather a number is prime or not using function. */ 15 | int a,b; 16 | cout<<"Enter two numbers: "<>a>>b; 18 | 19 | for(int i=a;i<=b;i++){ 20 | if(isprime(i)){ 21 | cout< 2 | using namespace std; 3 | // This program is to print the letter having max frequency in string. 4 | int main(){ 5 | string s="ahureuhlaadkaxcxxxgggggggggg"; 6 | int freq[26]; 7 | for(int i=0;i<26;i++){ 8 | freq[i]=0; 9 | } 10 | for(int i=0;imaxF){ 17 | maxF=freq[i]; 18 | ans=i+'a'; 19 | } 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(palindromic pattern). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | 11 | for(int i=1;i<=n;i++){ 12 | int j; 13 | for(j=1;j<=n-i;j++){ 14 | cout<<" "; 15 | } 16 | int k=i; 17 | for(;j<=n;j++){ 18 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(hollow rectangle). */ 6 | int row,col; 7 | cout<<"Enter number of rows :"<>row; 9 | cout<<"Enter number of columns :"<>col; 11 | 12 | for(int i=1;i<=row;i++){ 13 | for(int j=1;j<=col;j++){ 14 | if(i==1 || i==row || j==1 || j==col){ 15 | cout<<"*"; 16 | } 17 | else{ 18 | cout<<" "; 19 | } 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | // This program is about finding max of three numbers. 6 | int num1,num2,num3; 7 | cout<<"ENTER THREE NUMBERS : \n"; 8 | cin>>num1>>num2>>num3; 9 | 10 | if(num1>num2){ 11 | if(num1>num3){ 12 | cout<<"MAXIMUM Number is : "<num3){ 17 | cout<<"MAXIMUM Number is : "< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | /* This program finds wheather number is armstrong or not. */ 7 | int n; 8 | cout<<"Enter any number:"<>n; 10 | float sum=0; 11 | int originaln=n; 12 | 13 | while(n>0){ 14 | int lastdigit=n%10; 15 | sum+= pow(lastdigit,3); 16 | n=n/10; 17 | } 18 | if(sum==originaln){ 19 | cout<<"The number is armstrong number."< 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // #define int long long 13 | #define vi vector 14 | #define pii pair 15 | #define vii vector 16 | #define rep(i, a, b) for (int i = a; i < b; i++) 17 | #define ff first 18 | #define ss second 19 | #define setBits(x) builtin_popcount(x) 20 | 21 | signed main() 22 | { 23 | map m; 24 | 25 | m[8] = 2; 26 | cout << m[8] << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /wavesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int arr[],int i,int j){ 4 | int temp=arr[i]; 5 | arr[i]=arr[j]; 6 | arr[j]=temp; 7 | } 8 | void waveSort(int arr[], int n) 9 | { 10 | for(int i=1;iarr[i-1]){ 12 | swap(arr,i,i-1); 13 | } 14 | if(arr[i]>arr[i+1] && i<=n-2){ 15 | swap(arr,i,i+1); 16 | } 17 | } 18 | } 19 | int main() 20 | { 21 | int arr[] = {1,3,4,7,5,6,2}; 22 | waveSort(arr, 7); 23 | for (int i = 0; i < 7; i++) 24 | { 25 | cout << arr[i] << " "; 26 | } 27 | cout << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /getman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Program to understand bit manipulation. 4 | int getBit(int n,int pos){ 5 | return (n & (1< 2 | using namespace std; 3 | /* This program is for finding the record breaking day. */ 4 | 5 | int main(){ 6 | int n; 7 | cout<<"Enter the size of array: "<>n; 9 | int arr[n+1]; 10 | arr[n]=-1; 11 | for(int i=0;i>arr[i]; 13 | } 14 | if(n==1){ 15 | cout<<"1"<mx && arr[i]>arr[i+1]){ 22 | ans++; 23 | } 24 | mx=max(mx,arr[i]); 25 | } 26 | cout< 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e3 + 2, MOD = 1e9 + 7; 14 | 15 | int main() 16 | { 17 | int n; 18 | cin >> n; 19 | vector fib(n + 2, 0); 20 | fib[0] = 1; 21 | fib[1] = 1; 22 | for (int i = 2; i <= n + 1; i++) 23 | { 24 | fib[i] = fib[i - 1] + fib[i - 2]; 25 | } 26 | cout << fib[n + 1]; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /primefactorisation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Program to do prime factorisation sieve of eratosthenes. 4 | void primefactor(int n){ 5 | int spf[100]={0}; 6 | for(int i=2;i<=n;i++){ 7 | spf[i]=i; 8 | } 9 | for(int i=2;i<=n;i++){ 10 | if(spf[i]==i){ 11 | for(int j=i*i;j<=n;j+=i){ 12 | if(spf[j]==j){ 13 | spf[j]=i; 14 | } 15 | } 16 | } 17 | } 18 | while(n!=1){ 19 | cout<>n; 26 | primefactor(n); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /linearsearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for finding index of an element given in ARRAY */ 4 | 5 | int linearsearch(int arr[], int n, int key){ 6 | for(int i=0;i>n; 18 | int arr[n]; 19 | for(int i=0;i>arr[i]; 21 | } 22 | int key; 23 | cout<<"Enter the key number: "; 24 | cin>>key; 25 | cout<<"The index of key number is: "< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program is for usage of switch case. */ 6 | char press; 7 | cout<<"Enter a letter(a,b,c,d) : \n"; 8 | cin>>press; 9 | 10 | switch (press) 11 | { 12 | case 'a': 13 | cout<<"Yo"< 2 | using namespace std; 3 | /* This program is for converting hexademical number to decimal number. */ 4 | int hexadecimaltodecimal(string n){ 5 | int ans=0; 6 | int x=1; 7 | int s=n.size(); 8 | for(int i=s-1;i>=0;i--){ 9 | if(n[i]>= '0' && n[i]<='9'){ 10 | ans+=x*(n[i]-'0'); 11 | }else if(n[i]>= 'A' && n[i]<='F'){ 12 | ans+=x*(n[i]-'A'+10); 13 | } 14 | x*=16; 15 | } 16 | return ans; 17 | 18 | } 19 | int main(){ 20 | string n; 21 | cout<<"Enter a HEXADECIMAL number: "<>n; 23 | cout< 2 | using namespace std; 3 | 4 | #define rep(i, a, b) for (int i = a; i < b; i++) 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | string s; 10 | cin >> s; 11 | int e = 1, a = 0, ab = 0, abc = 0; 12 | rep(i, 0, n) 13 | { 14 | if (s[i] == 'a') 15 | a += e; 16 | else if (s[i] == 'b') 17 | ab += a; 18 | else if (s[i] == 'c') 19 | abc += ab; 20 | else if (s[i] == '?') 21 | { 22 | abc += 3 * abc + ab; 23 | ab = 3 * ab + a; 24 | a = 3 * a + e; 25 | } 26 | } 27 | cout << abc << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /longestarithmeticarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for finding the longest aithmetic array. */ 4 | 5 | int main(){ 6 | int n; 7 | cout<<"Enter the size of array: "<>n; 9 | int arr[n]; 10 | for(int i=0;i>arr[i]; 12 | } 13 | int ans=2; 14 | int pd=arr[1]-arr[0]; 15 | int j=2; 16 | int curr=2; 17 | while(j 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(star pattern). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | 11 | for(int i=1;i<=n;i++){ 12 | for(int j=1;j<=n-i;j++){ 13 | cout<<" "; 14 | } 15 | for(int j=1;j<=(2*i)-1;j++){ 16 | cout<<"*"<<" "; 17 | } 18 | cout<=1;i--){ 21 | for(int j=1;j<=n-i;j++){ 22 | cout<<" "; 23 | } 24 | for(int j=1;j<=(2*i)-1;j++){ 25 | cout<<"*"<<" "; 26 | } 27 | cout< 2 | using namespace std; 3 | // Program to find the unique number. 4 | bool getBit(int n,int pos){ 5 | return (n&(1< 2 | using namespace std; 3 | int findPeakElement(int arr[], int low, int high, int n) 4 | { 5 | int mid = low + (high - low) / 2; 6 | if ((mid == 0 || arr[mid - 1] <= arr[mid]) && (mid == n - 1 || arr[mid + 1] <= arr[mid])) 7 | { 8 | return mid; 9 | } 10 | else if (mid > 0 && arr[mid - 1] > arr[mid]) 11 | { 12 | return findPeakElement(arr, low, mid - 1, n); 13 | } 14 | else 15 | { 16 | return findPeakElement(arr, mid + 1, high, n); 17 | } 18 | } 19 | int main() 20 | { 21 | int arr[] = {0, 6, 8, 5, 7, 9}; 22 | int n = 6; 23 | cout << "Peak element index:" << findPeakElement(arr, 0, n - 1, n) << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /maxconsecutiveones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin >> n; 7 | int k; 8 | cin >> k; 9 | vector a(n); 10 | for (auto &i : a) 11 | { 12 | cin >> i; 13 | } 14 | int zerocnt = 0, i = 0, ans = 0; 15 | for (int j = 0; j < n; j++) 16 | { 17 | if (a[j] == 0) 18 | { 19 | zerocnt++; 20 | } 21 | while (zerocnt > k) 22 | { 23 | if (a[i] == 0) 24 | { 25 | zerocnt--; 26 | } 27 | i++; 28 | } 29 | //zerocnt<=k 30 | ans = max(ans, j - i + 1); 31 | } 32 | cout << ans; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /DP_minjumpstoreachend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int inf = 1e9; 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | vector arr(n); 9 | for (auto &i : arr) 10 | { 11 | cin >> i; 12 | } 13 | vector jumps(n, inf); 14 | if (arr[0] == 0) 15 | { 16 | cout << inf << " "; 17 | return 0; 18 | } 19 | jumps[0] = 0; 20 | for (int i = 1; i < n; i++) 21 | { 22 | for (int j = 0; j < i; j++) 23 | { 24 | if (i <= (j + arr[j])) 25 | { 26 | jumps[i] = min(jumps[i], jumps[j] + 1); 27 | } 28 | } 29 | } 30 | cout << jumps[n - 1] << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /stockspan.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector stockspan(vector prices) 4 | { 5 | vector ans; 6 | stack> s; 7 | for (auto price : prices) 8 | { 9 | int days = 1; 10 | while (!s.empty() && s.top().first <= price) 11 | { 12 | days += s.top().second; 13 | s.pop(); 14 | } 15 | s.push({price, days}); 16 | ans.push_back(days); 17 | } 18 | return ans; 19 | } 20 | int main() 21 | { 22 | vector a = {100, 80, 60, 70, 60, 75, 85}; 23 | vector res = stockspan(a); 24 | for (auto i : res) 25 | { 26 | cout << i << " "; 27 | } 28 | cout << "\n"; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /largestrectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int get_max_area(vector a){ 4 | int n=a.size(),ans=0,i=0; 5 | stack st; 6 | a.push_back(0); 7 | while(ia[i]){ 9 | int t=st.top(); 10 | int h=a[t]; 11 | st.pop(); 12 | if(st.empty()){ 13 | ans=max(ans, h*i); 14 | } 15 | else{ 16 | int len=i-st.top()-1; 17 | ans=max(ans,h*len); 18 | } 19 | } 20 | st.push(i); 21 | i++; 22 | } 23 | return ans; 24 | } 25 | int main(){ 26 | vector a={2,1,5,6,2,3}; 27 | cout< 2 | using namespace std; 3 | int catalan(int n) 4 | { 5 | if (n <= 1) 6 | { 7 | return 1; 8 | } 9 | int res = 0; 10 | for (int i = 0; i <= n-1; i++) 11 | { 12 | res += catalan(i) * catalan(n - i - 1); 13 | } 14 | return res; 15 | } 16 | int main() 17 | { 18 | for (int i = 0; i < 10; i++) 19 | { 20 | cout << catalan(i) << " "; 21 | } 22 | cout << endl; 23 | return 0; 24 | } 25 | /* Applications of catalan numbers 26 | 1) possible BSTs 27 | 2)paranthesis / bracket combinations 28 | 3)possible forests 29 | 4)ways of triangulation 30 | 5)possible paths in matrix 31 | 6)dividing a circle using N cords 32 | 7)dyck words of give length 33 | and much more */ 34 | -------------------------------------------------------------------------------- /transposesquarematrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for understanding transpose of a SQUARE matrix. */ 4 | 5 | int main(){ 6 | int n,m; // OR int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}}; 7 | cin>>n>>m; 8 | int arr[n][m]; 9 | for(int i=0;i>arr[i][j]; 12 | } 13 | } 14 | for(int i=0;i 2 | using namespace std; 3 | int rain_water(vector a) 4 | { 5 | stack st; 6 | int n = a.size(), ans = 0; 7 | for (int i = 0; i < n; i++) 8 | { 9 | while (!st.empty() && a[st.top()] < a[i]) 10 | { 11 | int cur = st.top(); 12 | st.pop(); 13 | if (st.empty()) 14 | { 15 | break; 16 | } 17 | int diff = i - st.top() - 1; 18 | ans += (min(a[st.top()], a[i]) - a[cur]) * diff; 19 | } 20 | st.push(i); 21 | } 22 | return ans; 23 | } 24 | int main() 25 | { 26 | vector a = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; 27 | cout << rain_water(a); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /DP_optimalgamestrategy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector a; 4 | int dp[100][100]; 5 | int solve(int i, int j) 6 | { 7 | if (i == j) 8 | { 9 | return a[i]; 10 | } 11 | if (i > j) 12 | { 13 | return 0; 14 | } 15 | if (dp[i][j] != -1) 16 | { 17 | return dp[i][j]; 18 | } 19 | int l = a[i] + min(solve(i + 2, j), solve(i + 1, j - 1)); 20 | int r = a[j] + min(solve(i, j - 2), solve(i + 1, j - 1)); 21 | return dp[i][j] = max(l, r); 22 | } 23 | int main() 24 | { 25 | memset(dp, -1, sizeof(dp)); 26 | int n; 27 | cin >> n; 28 | a = vector(n); 29 | for (auto &i : a) 30 | { 31 | cin >> i; 32 | } 33 | cout << solve(0, n - 1); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /allcasessame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // This program is to make all letters of same case in string. 4 | int main(){ 5 | // string str="akfdkjcn"; 6 | // // convert all to uppercase 7 | // for(int i=0;i='a' && str[i]<='z') 9 | // str[i]-=32; 10 | // } 11 | // cout<='A' && str[i]<='z') 15 | // str[i]+=32; 16 | // } 17 | // cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program is a basic calculator. */ 6 | int num1,num2; 7 | cout<<"Enter Two Numbers : \n"; 8 | cin>>num1>>num2; 9 | char op; 10 | cout<<"Enter an operator(+,-,/,*) : \n"; 11 | cin>>op; 12 | 13 | switch (op) 14 | { 15 | case '+': 16 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | vector v; 7 | v.push_back(1); 8 | v.push_back(2); 9 | v.push_back(3); 10 | for(int i=0;i::iterator it; 14 | for(it=v.begin();it!=v.end();it++){ 15 | cout<<*it< v2 (3,50); // 50 50 50 22 | swap(v,v2); 23 | for(auto element:v){ 24 | cout< 2 | #include 3 | using namespace std; 4 | /* This program is for finding a pythagorean triplet. */ 5 | bool check(int x,int y,int z){ 6 | int a=max(x,max(y,z)); 7 | int b,c; 8 | if(a==x){ 9 | b=y; 10 | c=z; 11 | }else if(a==y){ 12 | b=x; 13 | c=z; 14 | }else{ 15 | b=x; 16 | c=y; 17 | } 18 | if(a*a==b*b+c*c){ 19 | return true; 20 | }else{ 21 | return false; 22 | } 23 | } 24 | int main(){ 25 | int x,y,z; 26 | cout<<"Enter three numbers: "<>x>>y>>z; 28 | if(check(x,y,z)){ 29 | cout<<"PYTHAGOREAN TRIPLATE!!!"< 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | int countNodes(Node* root){ 16 | if(root==NULL){ 17 | return 0; 18 | } 19 | return countNodes(root->left)+countNodes(root->right)+1; 20 | } 21 | int main() 22 | { 23 | struct Node *root = new Node(1); 24 | root->left = new Node(2); 25 | root->right = new Node(3); 26 | root->left->left = new Node(4); 27 | root->left->right = new Node(5); 28 | root->right->left = new Node(6); 29 | root->right->right = new Node(7); 30 | 31 | cout< 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | int sumNodes(Node* root){ 16 | if(root==NULL){ 17 | return 0; 18 | } 19 | return sumNodes(root->left)+sumNodes(root->right)+root->data; 20 | } 21 | int main() 22 | { 23 | struct Node *root = new Node(1); 24 | root->left = new Node(2); 25 | root->right = new Node(3); 26 | root->left->left = new Node(4); 27 | root->left->right = new Node(5); 28 | root->right->left = new Node(6); 29 | root->right->right = new Node(7); 30 | 31 | cout< 2 | using namespace std; 3 | /* This program is for converting decimal number to hexadecimal number. */ 4 | string decimaltohexadecimal(int n){ 5 | string ans=""; 6 | int x=1; 7 | while(x<=n){ 8 | x*=16; 9 | } 10 | x/=16; 11 | while(x>0){ 12 | int lastdigit=n/x; 13 | n-=lastdigit*x; 14 | x/=16; 15 | if(lastdigit<=9){ 16 | ans=ans+ to_string(lastdigit); 17 | }else 18 | { 19 | char c='A'+ lastdigit-10; 20 | ans.push_back(c); 21 | } 22 | 23 | } 24 | return ans; 25 | } 26 | int main(){ 27 | int n; 28 | cout<<"Enter a decimal number: "<>n; 30 | cout< 3 | using namespace std; 4 | void maxSubarraySum(int arr[], int n, int k, int x) 5 | { 6 | int sum = 0, ans = 0; 7 | for (int i = 0; i < k; i++) 8 | { 9 | sum += arr[i]; 10 | } 11 | if (sum < x) 12 | { 13 | ans = sum; 14 | } 15 | for (int i = k; i < n; i++) 16 | { 17 | sum = sum - arr[i - k]; 18 | sum = sum + arr[i]; 19 | 20 | if (sum < x) 21 | { 22 | ans = max(ans, sum); 23 | } 24 | } 25 | cout << ans << " is the max subarray sum ( 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e3 + 2, MOD = 1e9 + 7; 14 | 15 | int main() 16 | { 17 | int n = 5, w = 11; 18 | vector wt = {3, 2, 4, 5, 1}; 19 | vector val = {4, 3, 5, 6, 1}; 20 | vector dp(w + 1, 0); 21 | for (int j = 0; j <= w; j++) 22 | { 23 | for (int i = 0; i < n; i++) 24 | { 25 | if (j - wt[i] >= 0) 26 | dp[j] = max(dp[j], val[i] + dp[j - wt[i]]); 27 | } 28 | } 29 | cout << dp[w]; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /DP_kadanealgo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e3 + 2, MOD = 1e9 + 7; 14 | 15 | int main() 16 | { 17 | int n; 18 | cin >> n; 19 | vi a(n); 20 | rep(i, 0, n) 21 | { 22 | cin >> a[i]; 23 | } 24 | int curr = 0, maxTillNow = 0; 25 | rep(i, 0, n) 26 | { 27 | curr += a[i]; 28 | maxTillNow = max(curr, maxTillNow); 29 | if (curr < 0) 30 | { 31 | curr = 0; 32 | } 33 | } 34 | cout << maxTillNow << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /firstrepeatingelement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for finding the index of first repeating element in the array. */ 4 | 5 | int main(){ 6 | int n; 7 | cout<<"Enter the size of array: "<>n; 9 | int arr[n]; 10 | for(int i=0;i>arr[i]; 12 | } 13 | const int N=1e6+2; 14 | int idx[N]; 15 | for(int i=0;i 2 | using namespace std; 3 | // Program to find the 2 unique numbers. 4 | int setBit(int n,int pos){ 5 | return ((n&(1<>1; 19 | } 20 | int newxor=0; 21 | for(int i=0;i 2 | using namespace std; 3 | void swap(int arr[],int i ,int j){ 4 | int temp=arr[i]; 5 | arr[i]=arr[j]; 6 | arr[j]=temp; 7 | } 8 | void dnfSort(int arr[], int n) 9 | { 10 | int low=0; 11 | int mid=0; 12 | int high=n-1; 13 | while(mid<=high){ 14 | if(arr[mid]==0){ 15 | swap(arr,low,mid); 16 | low++;mid++; 17 | } 18 | else if(arr[mid]==1){ 19 | mid++; 20 | } 21 | else{ 22 | swap(arr,mid,high); 23 | high--; 24 | } 25 | } 26 | } 27 | int main() 28 | { 29 | int arr[] = {1, 0,2,1,0,1,2,1,2}; 30 | dnfSort(arr, 9); 31 | for (int i = 0; i < 9; i++) 32 | { 33 | cout << arr[i] << " "; 34 | } 35 | cout << endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int firstOcc(int arr[], int n, int i, int key) 4 | { 5 | if (i == n) 6 | { 7 | return -1; 8 | } 9 | if (arr[i] == key) 10 | { 11 | return i; 12 | } 13 | return firstOcc(arr, n, i + 1, key); 14 | } 15 | int lastOcc(int arr[], int n, int i, int key) 16 | { 17 | if (i == n) 18 | { 19 | return -1; 20 | } 21 | int restArray = lastOcc(arr, n, i + 1, key); 22 | if (restArray != -1) 23 | { 24 | return restArray; 25 | } 26 | if (arr[i] == key) 27 | { 28 | return i; 29 | } 30 | return -1; 31 | } 32 | int main() 33 | { 34 | int arr[] = {4, 2, 1, 2, 5, 2, 7}; 35 | cout << firstOcc(arr, 7, 0, 2) << endl; 36 | cout << lastOcc(arr, 7, 0, 2) << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /searchingtwodarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for understanding searching in 2D array. */ 4 | //for sorted rows and columns. 5 | int main(){ 6 | int n,m; 7 | cin>>n>>m; 8 | int arr[n][m]; 9 | for(int i=0;i>arr[i][j]; 12 | } 13 | } 14 | int target; 15 | cin>>target; 16 | bool found=false; 17 | int r=0,c=m-1; 18 | while(r=0){ 19 | if(arr[r][c]==target){ 20 | found=true; 21 | } 22 | if(arr[r][c]>target){ 23 | c--; 24 | }else{ 25 | r++; 26 | } 27 | } 28 | if(found==true){ 29 | cout<<"ELEMENT IS FOUND!"< 2 | using namespace std; 3 | /* This program is for finding largest world in the given sentence. */ 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | cin.ignore(); 9 | char arr[n+1]; 10 | cin.getline(arr,n); 11 | cin.ignore(); 12 | int i=0; 13 | int currlen=0, maxlen=0; 14 | int st=0,maxst=0; 15 | while(1){ 16 | if(arr[i]==' ' || arr[i]=='\0'){ 17 | if(currlen>maxlen){ 18 | maxlen=currlen; 19 | maxst=st; 20 | } 21 | currlen=0; 22 | st=i+1; 23 | }else 24 | currlen++; 25 | if(arr[i]=='\0') 26 | break; 27 | i++; 28 | 29 | } 30 | cout< 2 | using namespace std; 3 | /* This program is for finding number of subarrays with sum of all elements equal to given sum. */ 4 | 5 | int main(){ 6 | int n,s; 7 | cin>>n>>s; 8 | int a[n]; 9 | for(int i=0;i>a[i]; 11 | } 12 | int i=0,j=0,st=-1,en=-1,sum=0; 13 | while(js){ 24 | sum-=a[i]; 25 | i++; 26 | } 27 | if(sum==s){ 28 | st=i+1; 29 | en=j+1; 30 | break; 31 | } 32 | j++; 33 | } 34 | cout< 2 | using namespace std; 3 | /* This program is for arranging elements of array in accending order using insertion sort. */ 4 | 5 | void insertionsort(int arr[], int n){ 6 | for(int i=1;icurrent && j>=0){ 10 | arr[j+1]=arr[j]; 11 | j--; 12 | } 13 | arr[j+1]=current; 14 | } 15 | for(int i=0;i>n; 24 | int arr[n]; 25 | cout<<"Enter Numbers for array: "<>arr[i]; 28 | } 29 | cout<<"Numbers in accending order: "; 30 | insertionsort(arr,n); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /reversestack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void insertAtBottom(stack &st, int ele) 5 | { 6 | if (st.empty()) 7 | { 8 | st.push(ele); 9 | return; 10 | } 11 | int topele = st.top(); 12 | st.pop(); 13 | insertAtBottom(st, ele); 14 | st.push(topele); 15 | } 16 | void reverse(stack &st) 17 | { 18 | if (st.empty()) 19 | { 20 | return; 21 | } 22 | int ele = st.top(); 23 | st.pop(); 24 | reverse(st); 25 | insertAtBottom(st, ele); 26 | } 27 | int main() 28 | { 29 | stack st; 30 | st.push(1); 31 | st.push(2); 32 | st.push(3); 33 | st.push(4); 34 | reverse(st); 35 | while (!st.empty()) 36 | { 37 | cout << st.top() << endl; 38 | st.pop(); 39 | } 40 | cout << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /redundantparentheses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | string s; 6 | cin >> s; 7 | stack st; 8 | bool ans = false; 9 | for (int i = 0; i < s.size(); i++) 10 | { 11 | if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') 12 | { 13 | st.push(s[i]); 14 | } 15 | else if (s[i] == '(') 16 | { 17 | st.push(s[i]); 18 | } 19 | else if (s[i] == ')') 20 | { 21 | if (st.top() == '(') 22 | { 23 | ans = true; 24 | } 25 | while (st.top() == '+' || st.top() == '-' || st.top() == '*' || st.top() == '/') 26 | { 27 | st.pop(); 28 | } 29 | st.pop(); 30 | } 31 | } 32 | cout << ans; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /twoDarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for understanding 2D ARRAY by searching a matrix element. */ 4 | 5 | int main(){ 6 | int n,m; 7 | cin>>n>>m; 8 | int arr[n][m]; 9 | for(int i=0;i>arr[i][j]; 12 | } 13 | } 14 | cout<>x; 17 | cout< 2 | using namespace std; 3 | /* This program is for arranging elements of array in accending order using selection sort. */ 4 | 5 | void selectionsort(int arr[], int n){ 6 | for(int i=0;i>n; 24 | int arr[n]; 25 | cout<<"Enter Numbers for array: "<>arr[i]; 28 | } 29 | cout<<"Numbers in accending order: "; 30 | selectionsort(arr,n); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /smallest+missingnum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for finding the smallest positive missing number */ 4 | 5 | int main() 6 | { 7 | int n; 8 | cout << "Enter the size of array: " << endl; 9 | cin >> n; 10 | int a[n]; 11 | for (int i = 0; i < n; i++) 12 | { 13 | cin >> a[i]; 14 | } 15 | const int N = 1e6 + 2; 16 | bool check[N]; 17 | for (int i = 0; i < N; i++) 18 | { 19 | check[i] = 0; 20 | } 21 | for (int i = 0; i < n; i++) 22 | { 23 | if (a[i] >= 0) 24 | { 25 | check[a[i]] = 1; 26 | } 27 | } 28 | int ans = -1; 29 | for (int i = 1; i < N; i++) 30 | { 31 | if (check[i] == false) 32 | { 33 | ans = i; 34 | break; 35 | } 36 | } 37 | cout << ans << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /DP_maxlengthbitonicsubseq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n = 8; 6 | vector a = {1, 11, 2, 10, 4, 5, 2, 1}; 7 | vector forward(n, 1), backward(n, 1); 8 | for (int i = 0; i < n; i++) 9 | { 10 | for (int j = 0; j < i; j++) 11 | { 12 | if (a[i] > a[j]) 13 | forward[i] = max(forward[i], 1 + forward[j]); 14 | } 15 | } 16 | for (int i = n - 1; i >= 0; i--) 17 | { 18 | for (int j = n - 1; j > i; j--) 19 | { 20 | if (a[i] > a[j]) 21 | { 22 | backward[i] = max(backward[i], 1 + backward[j]); 23 | } 24 | } 25 | } 26 | int ans = 0; 27 | for (int i = 0; i < n; i++) 28 | { 29 | ans = max(ans, forward[i] + backward[i] - 1); 30 | } 31 | cout << ans; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /countsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void countSort(int arr[], int n) 5 | { 6 | int k = arr[0]; 7 | for (int i = 0; i < n; i++) 8 | { 9 | k = max(k, arr[i]); 10 | } 11 | int count[10] = {0}; 12 | for (int i = 0; i < n; i++) 13 | { 14 | count[arr[i]]++; 15 | } 16 | for (int i = 1; i <= k; i++) 17 | { 18 | count[i] += count[i - 1]; 19 | } 20 | int output[n]; 21 | for (int i = n - 1; i >= 0; i--) 22 | { 23 | output[--count[arr[i]]] = arr[i]; 24 | } 25 | for (int i = 0; i < n; i++) 26 | { 27 | arr[i] = output[i]; 28 | } 29 | } 30 | int main() 31 | { 32 | int arr[] = {1, 3, 2, 3, 4, 1, 6, 4, 3}; 33 | countSort(arr, 9); 34 | for (int i = 0; i < 9; i++) 35 | { 36 | cout << arr[i] << " "; 37 | } 38 | cout << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /DP_KorderedLCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n, m, k; 4 | vector a, b; 5 | int dp[2005][2005][7]; 6 | int solve(int i, int j, int k) 7 | { 8 | if (i == n or j == m) 9 | { 10 | return 0; 11 | } 12 | if (dp[i][j][k] != -1) 13 | { 14 | return dp[i][j][k]; 15 | } 16 | int c1(0), c2(0), c3(0); 17 | if (a[i] == b[j]) 18 | c1 = 1 + solve(i + 1, j + 1, k); 19 | if (k > 0) 20 | c2 = 1 + solve(i + 1, j + 1, k - 1); 21 | c3 = max(solve(i + 1, j, k), solve(i, j + 1, k)); 22 | return dp[i][j][k] = max({c1, c2, c3}); 23 | } 24 | int main() 25 | { 26 | memset(dp, -1, sizeof(dp)); 27 | cin >> n >> m >> k; 28 | a = vector(n); 29 | b = vector(m); 30 | for (auto &i : a) 31 | cin >> i; 32 | for (auto &i : b) 33 | cin >> i; 34 | cout << solve(0, 0, k); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /countfreq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // #define int long long 13 | #define vi vector 14 | #define pii pair 15 | #define vii vector 16 | #define rep(i, a, b) for (int i = a; i < b; i++) 17 | #define ff first 18 | #define ss second 19 | #define setBits(x) builtin_popcount(x) 20 | 21 | signed main() 22 | { 23 | int n; 24 | cin >> n; 25 | vi a(n); 26 | rep(i, 0, n) 27 | cin >> a[i]; 28 | unordered_map freq; 29 | rep(i, 0, n) 30 | freq[a[i]]++; 31 | unordered_map::iterator it; 32 | for (it = freq.begin(); it != freq.end(); it++) 33 | { 34 | cout << it->ff << " " << it->ss << endl; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /pair.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | bool myCompare(pair p1, pair p2) 6 | { 7 | return p1.first < p2.first; 8 | } 9 | int main() 10 | { 11 | // pair p; 12 | // p.first=3; 13 | // p.second='f'; 14 | int arr[] = {10, 16, 7, 14, 5, 3, 12, 9}; 15 | vector> v; 16 | for (int i = 0; i < (sizeof(arr) / sizeof(arr[0])); i++) 17 | { 18 | // pair p; 19 | // p.first=arr[i]; 20 | // p.second=i; 21 | v.push_back(make_pair(arr[i], i)); 22 | } 23 | sort(v.begin(), v.end(), myCompare); 24 | for (int i = 0; v.size(); i++) 25 | { 26 | arr[v[i].second] = i; 27 | } 28 | for (int i = 0; v.size(); i++) 29 | { 30 | cout << arr[i] << " "; 31 | } 32 | cout << endl; 33 | return 0; 34 | } 35 | 36 | //error -------------------------------------------------------------------------------- /permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector> ans; 4 | void permute(vector &a, int idx) 5 | { 6 | if (idx == a.size()) 7 | { 8 | ans.push_back(a); 9 | return; 10 | } 11 | for (int i = idx; i < a.size(); i++) 12 | { 13 | swap(a[i], a[idx]); 14 | permute(a, idx + 1); 15 | swap(a[i], a[idx]); 16 | } 17 | return; 18 | } 19 | int main() 20 | { 21 | int n; 22 | cin >> n; 23 | vector a(n); 24 | for (auto &i : a) 25 | cin >> i; 26 | permute(a, 0); 27 | for (auto v : ans) 28 | { 29 | for (auto i : v) 30 | cout << i << " "; 31 | cout << "\n"; 32 | } 33 | return 0; 34 | } 35 | 36 | // sort(nums,begin(),nums.end()); //shortcut 37 | // do{ 38 | // ans.push_back(nums); 39 | // }while(next_permutation(nums.begin(),nums.end())); -------------------------------------------------------------------------------- /pattern7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | /* This program prints a pattern(butterfly pattern). */ 6 | int n; 7 | cout<<"Enter a number :"<>n; 9 | 10 | for(int i=1;i<=n;i++){ 11 | for(int j=1;j<=i;j++){ 12 | cout<<"*"; 13 | } 14 | int space=2*n-2*i; 15 | for(int j=1;j<=space;j++){ 16 | cout<<" "; 17 | } 18 | for(int j=1;j<=i;j++){ 19 | cout<<"*"; 20 | } 21 | cout<=1;i--){ 24 | for(int j=1;j<=i;j++){ 25 | cout<<"*"; 26 | } 27 | int space=2*n-2*i; 28 | for(int j=1;j<=space;j++){ 29 | cout<<" "; 30 | } 31 | for(int j=1;j<=i;j++){ 32 | cout<<"*"; 33 | } 34 | cout< 2 | using namespace std; 3 | const int N = 1e5 + 2; 4 | bool vis[N]; //visited array 5 | vector adj[N]; //adjecency list 6 | 7 | void dfs(int node) 8 | { 9 | //preorder 10 | vis[node] = 1; 11 | cout << node << " "; 12 | 13 | //inorder 14 | vector::iterator it; 15 | for (it = adj[node].begin(); it != adj[node].end(); it++) 16 | { 17 | if (vis[*it]) 18 | ; 19 | else 20 | { 21 | dfs(*it); 22 | } 23 | } 24 | //postorder 25 | } 26 | int main() 27 | { 28 | int n, m; 29 | cin >> n >> m; 30 | for (int i = 0; i <= n; i++) 31 | { 32 | vis[i] = false; 33 | } 34 | int x, y; 35 | for (int i = 0; i < m; i++) 36 | { 37 | cin >> x >> y; 38 | adj[x].push_back(y); 39 | adj[y].push_back(x); 40 | } 41 | dfs(1); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /DP_LCSwith3strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int dp[205][205][205]; 4 | int LCS(string &s1, string &s2, string &s3, int i, int j, int k) 5 | { 6 | if (i == 0 or j == 0 or k == 0) 7 | { 8 | return 0; 9 | } 10 | if (dp[i][j][k] != -1) 11 | return dp[i][j][k]; 12 | if (s1[i - 1] == s2[j - 1] and s2[j - 1] == s3[k - 1]) 13 | { 14 | return dp[i][j][k] = 1 + LCS(s1, s2, s3, i - 1, j - 1, k - 1); 15 | } 16 | int l = LCS(s1, s2, s3, i - 1, j, k); 17 | int r = LCS(s1, s2, s3, i, j - 1, k); 18 | int p = LCS(s1, s2, s3, i, j, k - 1); 19 | return dp[i][j][k] = max({l, r, p}); 20 | } 21 | int main() 22 | { 23 | memset(dp, -1, sizeof(dp)); 24 | string s1; 25 | cin >> s1; 26 | string s2; 27 | cin >> s2; 28 | string s3; 29 | cin >> s3; 30 | cout << LCS(s1, s2, s3, s1.size(), s2.size(), s3.size()); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /bubblesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for arranging elements of array in accending order using bubble sort. */ 4 | 5 | void bubblesort(int arr[], int n){ 6 | int counter=0; 7 | while(counterarr[i+1]){ 10 | int temp=arr[i]; 11 | arr[i]=arr[i+1]; 12 | arr[i+1]=temp; 13 | } 14 | } 15 | counter++; 16 | } 17 | for(int i=0;i>n; 26 | int arr[n]; 27 | cout<<"Enter Numbers for array: "<>arr[i]; 30 | } 31 | cout<<"Numbers in accending order: "; 32 | bubblesort(arr,n); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int arr[],int i,int j){ 4 | int temp=arr[i]; 5 | arr[i]=arr[j]; 6 | arr[j]=temp; 7 | } 8 | int partition(int arr[], int l, int r) 9 | { 10 | int pivot=arr[r]; 11 | int i=l-1; 12 | for (int j = l; j < r; j++) 13 | { 14 | if(arr[j] 2 | using namespace std; 3 | int main() 4 | { 5 | int n, m; 6 | cin >> n >> m; 7 | int cnt = 0; 8 | vector> adj(n); 9 | vector indeg(n, 0); 10 | for (int i = 0; i < m; i++) 11 | { 12 | int u, v; 13 | cin >> u >> v; 14 | adj[u].push_back(v); 15 | indeg[v]++; 16 | } 17 | queue pq; 18 | for (int i = 0; i < n; i++) 19 | { 20 | if (indeg[i] == 0) 21 | { 22 | pq.push(i); 23 | } 24 | } 25 | while (!pq.empty()) 26 | { 27 | cnt++; 28 | int x = pq.front(); 29 | pq.pop(); 30 | cout << x << " "; 31 | for (auto it : adj[x]) 32 | { 33 | indeg[it]--; 34 | if (indeg[it] == 0) 35 | { 36 | pq.push(it); 37 | } 38 | } 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /heightinbinarytree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | int calcHeight(Node* root){ //height: it is the depth of the tree's deepest node. 16 | if(root==NULL){ 17 | return 0; 18 | } 19 | int lHeight=calcHeight(root->left); 20 | int rHeight=calcHeight(root->right); 21 | return max(lHeight,rHeight)+1; 22 | } 23 | int main() 24 | { 25 | struct Node *root = new Node(1); 26 | root->left = new Node(2); 27 | root->right = new Node(3); 28 | 29 | root->left->left = new Node(4); 30 | root->left->right = new Node(5); 31 | 32 | root->right->left = new Node(6); 33 | root->right->right = new Node(7); 34 | 35 | cout< 2 | using namespace std; 3 | /* This program is for finding index of an element given in ARRAY provided 4 | elements are in accending order.*/ 5 | 6 | int binarysearch(int arr[], int n, int key){ 7 | int s=0; 8 | int e=n-1; 9 | while(s<=e){ 10 | int mid=(s+e)/2; 11 | if(arr[mid]==key){ 12 | return mid; 13 | }else if(arr[mid]>key){ 14 | e=mid-1; 15 | }else{ 16 | s=mid+1; 17 | } 18 | } 19 | return -1; 20 | 21 | } 22 | int main(){ 23 | int n; 24 | cout<<"Enter the size of array: "<>n; 26 | int arr[n]; 27 | cout<<"Enter elements in accending order: "<>arr[i]; 30 | } 31 | int key; 32 | cout<<"Enter the key number: "; 33 | cin>>key; 34 | cout<<"The index of key number is: "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | // #define int long long 8 | #define vi vector 9 | #define pii pair 10 | #define vii vector 11 | #define rep(i, a, b) for (int i = a; i < b; i++) 12 | #define ff first 13 | #define ss second 14 | #define setBits(x) builtin_popcount(x) 15 | 16 | signed main() 17 | { 18 | priority_queue> pq; 19 | pq.push(2); // O(log(n)) 20 | pq.push(3); 21 | pq.push(1); 22 | cout << pq.size() << endl; // O(1) 23 | cout << pq.top() << endl; // O(1) 24 | pq.pop(); // O(log(n)) 25 | cout << pq.top() << endl; 26 | 27 | priority_queue, greater> pqm; 28 | pqm.push(2); 29 | pqm.push(1); 30 | pqm.push(3); 31 | cout << pqm.top() << endl; 32 | pqm.pop(); 33 | cout << pqm.top() << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /indiancoinchange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // #define int long long 14 | #define vi vector 15 | #define pii pair 16 | #define vii vector 17 | #define rep(i, a, b) for (int i = a; i < b; i++) 18 | #define ff first 19 | #define ss second 20 | #define setBits(x) builtin_popcount(x) 21 | 22 | signed main() 23 | { 24 | int n; 25 | cin >> n; 26 | vi a(n); 27 | rep(i, 0, n) 28 | { 29 | cin >> a[i]; 30 | } 31 | int x; 32 | cin >> x; 33 | sort(a.begin(), a.end(), greater()); 34 | int ans = 0; 35 | for (int i = 0; i < n; i++) 36 | { 37 | ans += x / a[i]; 38 | x -= x / a[i] * a[i]; 39 | } 40 | cout << ans << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /permutation2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void helper(vector a, vector> &ans, int idx) 4 | { 5 | if (idx == a.size()) 6 | { 7 | ans.push_back(a); 8 | return; 9 | } 10 | for (int i = idx; i < a.size(); i++) 11 | { 12 | if (i != idx and a[i] == a[idx]) 13 | continue; 14 | swap(a[i], a[idx]); 15 | helper(a, ans, idx + 1); 16 | } 17 | } 18 | vector> permute(vector nums) 19 | { 20 | sort(nums.begin(), nums.end()); 21 | vector> ans; 22 | helper(nums, ans, 0); 23 | return ans; 24 | } 25 | int main() 26 | { 27 | int n; 28 | cin >> n; 29 | vector a(n); 30 | for (auto &i : a) 31 | cin >> i; 32 | vector> res = permute(a); 33 | for (auto v : res) 34 | { 35 | for (auto i : v) 36 | cout << i << " "; 37 | cout << "\n"; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /BFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e5 + 2; 4 | bool vis[N]; //visited array 5 | vector adj[N]; //adjecency list 6 | 7 | int main() 8 | { 9 | for (int i = 0; i < N; i++) 10 | { 11 | vis[i] = 0; 12 | } 13 | int n, m; 14 | cin >> n >> m; 15 | int x, y; 16 | for (int i = 0; i < m; i++) 17 | { 18 | cin >> x >> y; 19 | adj[x].push_back(y); 20 | adj[y].push_back(x); 21 | } 22 | queue q; 23 | q.push(1); 24 | vis[1] = true; 25 | while (!q.empty()) 26 | { 27 | int node = q.front(); 28 | q.pop(); 29 | cout << node << endl; 30 | vector::iterator it; 31 | for (it = adj[node].begin(); it != adj[node].end(); it++) 32 | { 33 | if (!vis[*it]) 34 | { 35 | vis[*it] = 1; 36 | q.push(*it); 37 | } 38 | } 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /3sumproblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin >> n; 7 | int target; 8 | cin >> target; 9 | vector a(n); 10 | for (auto &i : a) 11 | { 12 | cin >> i; 13 | } 14 | bool found = false; 15 | sort(a.begin(), a.end()); 16 | for (int i = 0; i < n; i++) 17 | { 18 | int lo = i + 1, hi = n - 1; 19 | while (lo < hi) 20 | { 21 | int current = a[i] + a[lo] + a[hi]; 22 | if (current == target) 23 | { 24 | found = true; 25 | } 26 | if (current < target) 27 | { 28 | lo++; 29 | } 30 | else 31 | { 32 | hi--; 33 | } 34 | } 35 | } 36 | if (found) 37 | { 38 | cout << "True"; 39 | } 40 | else 41 | { 42 | cout << "False"; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /searchinBST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | Node *left, *right; 7 | Node(int val) 8 | { 9 | data = val; 10 | left = NULL; 11 | right = NULL; 12 | } 13 | }; 14 | Node* searchInBST(Node* root,int key){ 15 | if(root==NULL){ 16 | return NULL; 17 | } 18 | if(root->data==key){ 19 | return root; 20 | } 21 | //data>key 22 | if(root->data > key){ 23 | return searchInBST(root->left,key); 24 | } 25 | //dataright,key); 27 | } 28 | int main() 29 | { 30 | Node *root = new Node(4); 31 | root->left = new Node(2); 32 | root->right = new Node(5); 33 | root->left->left = new Node(1); 34 | root->left->right = new Node(3); 35 | root->right->right=new Node(6); 36 | 37 | if(searchInBST(root,10)==NULL){ 38 | cout<<"Key doesn't exists"; 39 | }else{ 40 | cout<<"Key exists"; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /bellmanfordalgo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int INF = 1e9; 4 | int main() 5 | { 6 | int n, m; 7 | cin >> n >> m; 8 | vector> edges; 9 | for (int i = 0; i < m; i++) 10 | { 11 | int u, v, w; 12 | cin >> u >> v >> w; 13 | edges.push_back({u, v, w}); 14 | } 15 | int src; 16 | cin >> src; 17 | vector dist(n, INF); 18 | dist[src] = 0; 19 | for (int iter = 0; iter < n - 1; iter++) 20 | { 21 | bool change = false; // if to determine negative cycle 22 | for (auto e : edges) 23 | { 24 | int u = e[0]; 25 | int v = e[1]; 26 | int w = e[2]; 27 | if (dist[v] > w + dist[u]) 28 | { 29 | change = true; 30 | } 31 | dist[v] = min(dist[v], w + dist[u]); 32 | } 33 | } 34 | for (auto i : dist) 35 | { 36 | cout << i << " "; 37 | } 38 | cout << "\n"; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /matrixmulti.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for understanding matrix multipication. */ 4 | 5 | int main(){ 6 | int n1,n2,n3; 7 | cin>>n1>>n2>>n3; 8 | 9 | int arr1[n1][n2]; 10 | int arr2[n2][n3]; 11 | for(int i=0;i>arr1[i][j]; 14 | } 15 | } 16 | for(int i=0;i>arr2[i][j]; 19 | } 20 | } 21 | int ans[n1][n3]; 22 | for(int i=0;i 3 | using namespace std; 4 | int smallestSubarrayWithSum(int arr[], int n, int x) 5 | { 6 | int sum = 0, minLength = n + 1, start = 0, end = 0; 7 | while (end < n) 8 | { 9 | while (sum <= x && end < n) 10 | { 11 | sum += arr[end++]; 12 | } 13 | while (sum > x && start < n) 14 | { 15 | if (end - start < minLength) 16 | { 17 | minLength = end - start; 18 | } 19 | sum -= arr[start++]; 20 | } 21 | } 22 | return minLength; 23 | } 24 | int main() 25 | { 26 | int arr[] = {1, 4, 45, 6, 10, 19}; 27 | int x = 51; 28 | int n = 6; 29 | int minLength = smallestSubarrayWithSum(arr, n, x); 30 | if (minLength == n + 1) 31 | { 32 | cout << "No such subarray exists" << endl; 33 | } 34 | else 35 | { 36 | cout << "Smallest length subarray is: " << minLength << endl; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /connectedcompo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector vis; 4 | int n, m; 5 | vector> adj; 6 | vector components; 7 | int get_comp(int idx) 8 | { 9 | if (vis[idx]) 10 | return 0; 11 | vis[idx] = true; 12 | int ans = 1; 13 | for (auto i : adj[idx]) 14 | { 15 | if (!vis[i]) 16 | { 17 | ans += get_comp(i); 18 | vis[i] = true; 19 | } 20 | } 21 | return ans; 22 | } 23 | int main() 24 | { 25 | cin >> n >> m; 26 | adj = vector>(n); 27 | vis = vector(n); 28 | for (int i = 0; i < m; i++) 29 | { 30 | int u, v; 31 | cin >> u >> v; 32 | adj[u].push_back(v); 33 | adj[v].push_back(u); 34 | } 35 | for (int i = 0; i < n; i++) 36 | { 37 | if (!vis[i]) 38 | { 39 | components.push_back(get_comp(i)); 40 | } 41 | } 42 | for (auto i : components) 43 | { 44 | cout << i << " "; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /floydwarshallalgo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int INF = 1e9; 4 | int main() 5 | { 6 | vector> graph = {{0, 5, INF, 10}, {INF, 0, 3, INF}, {INF, INF, 0, 1}, {INF, INF, INF, 0}}; 7 | int n = graph.size(); 8 | vector> dist = graph; 9 | for (int k = 0; k < n; k++) 10 | { 11 | for (int i = 0; i < n; i++) 12 | { 13 | for (int j = 0; j < n; j++) 14 | { 15 | if (dist[i][k] + dist[k][j] < dist[i][j]) 16 | { 17 | dist[i][j] = dist[i][k] + dist[k][j]; 18 | } 19 | } 20 | } 21 | } 22 | for (int i = 0; i < n; i++) 23 | { 24 | for (int j = 0; j < n; j++) 25 | { 26 | if (dist[i][j] == INF) 27 | { 28 | cout << "INF "; 29 | } 30 | else 31 | cout << dist[i][j] << " "; 32 | } 33 | cout << "\n"; 34 | } 35 | //1,3 36 | cout << dist[1][3]; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /spriralordermatrixrev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* This program is for understanding 2D ARRAY by spiral order matrix traversal. */ 4 | 5 | int main(){ 6 | int n,m; 7 | cin>>n>>m; 8 | int arr[n][m]; 9 | for(int i=0;i>arr[i][j]; 12 | } 13 | } 14 | cout<=col_start;col--){ 26 | cout<=row_start;row--){ 30 | cout< 2 | using namespace std; 3 | class que 4 | { 5 | stack s1; 6 | 7 | public: 8 | void push(int x) 9 | { 10 | s1.push(x); 11 | } 12 | int pop() 13 | { 14 | if (s1.empty()) 15 | { 16 | cout << "Queue is empty\n"; 17 | return -1; 18 | } 19 | int x = s1.top(); 20 | s1.pop(); 21 | if (s1.empty()) 22 | { 23 | return x; 24 | } 25 | int item = pop(); 26 | s1.push(x); 27 | return item; 28 | } 29 | bool empty() 30 | { 31 | if (s1.empty()) 32 | return true; 33 | return false; 34 | } 35 | }; 36 | int main() 37 | { 38 | que q; 39 | q.push(1); 40 | q.push(2); 41 | q.push(3); 42 | q.push(4); 43 | cout << q.pop() << endl; 44 | q.push(5); 45 | cout << q.pop() << endl; 46 | cout << q.pop() << endl; 47 | cout << q.pop() << endl; 48 | cout << q.pop() << endl; 49 | cout << q.pop() << endl; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /arrstack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define n 100 4 | class stack{ 5 | int* arr; 6 | int top; 7 | public: 8 | stack(){ 9 | arr=new int[n]; 10 | top=-1; 11 | } 12 | void push(int x){ 13 | if(top==n-1){ 14 | cout<<"Stack Overflow"< 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // #define int long long 14 | #define vi vector 15 | #define pii pair 16 | #define vii vector 17 | #define rep(i, a, b) for (int i = a; i < b; i++) 18 | #define ff first 19 | #define ss second 20 | #define setBits(x) builtin_popcount(x) 21 | 22 | signed main() 23 | { 24 | int n, k; 25 | cin >> n >> k; 26 | vi a(n); 27 | rep(i, 0, n) 28 | { 29 | cin >> a[i]; 30 | } 31 | int s = 0, ans = INT_MAX; 32 | rep(i, 0, k) 33 | { 34 | s += a[i]; 35 | } 36 | cout << s << " "; 37 | ans = min(ans, s); 38 | // sliding window technique 39 | rep(i, 1, n - k + 1) 40 | { 41 | s -= a[i - 1]; 42 | s += a[i + k - 1]; 43 | ans = min(ans, s); 44 | cout << s << " "; 45 | } 46 | cout << endl; 47 | cout << ans << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /smallestsubsequencesumk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // #define int long long 13 | #define vi vector 14 | #define pii pair 15 | #define vii vector 16 | #define rep(i, a, b) for (int i = a; i < b; i++) 17 | #define ff first 18 | #define ss second 19 | #define setBits(x) builtin_popcount(x) 20 | 21 | signed main() 22 | { 23 | int n, k; 24 | cin >> n >> k; 25 | vi a(n); 26 | rep(i, 0, n) 27 | cin >> 28 | a[i]; 29 | priority_queue pq; 30 | 31 | rep(i, 0, n) 32 | { 33 | pq.push(a[i]); 34 | } 35 | 36 | int sum = 0; 37 | int cnt = 0; 38 | 39 | while (!pq.empty()) 40 | { 41 | sum += pq.top(); 42 | pq.pop(); 43 | 44 | cnt++; 45 | 46 | if (sum >= k) 47 | break; 48 | } 49 | if (sum < k) 50 | cout << "-1" << endl; 51 | else 52 | cout << cnt << endl; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /subarraysum0.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // #define int long long 13 | #define vi vector 14 | #define pii pair 15 | #define vii vector 16 | #define rep(i, a, b) for (int i = a; i < b; i++) 17 | #define ff first 18 | #define ss second 19 | #define setBits(x) builtin_popcount(x) 20 | 21 | signed main() 22 | { 23 | int n; 24 | cin >> n; 25 | vi a(n); 26 | rep(i, 0, n) 27 | { 28 | cin >> a[i]; 29 | } 30 | map cnt; 31 | int prefSum = 0; 32 | rep(i, 0, n) 33 | { 34 | prefSum += a[i]; 35 | cnt[prefSum]++; 36 | } 37 | int ans = 0; 38 | map::iterator it; 39 | 40 | for (it = cnt.begin(); it != cnt.end(); it++) 41 | { 42 | int c = it->ss; 43 | ans += (c * (c - 1)) / 2; 44 | if (it->ff == 0) 45 | { 46 | ans += it->ss; 47 | } 48 | } 49 | cout << ans << endl; 50 | return 0; 51 | } -------------------------------------------------------------------------------- /bipartitegraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector> adj; 4 | vector vis; 5 | vector col; 6 | bool bipart; 7 | void color(int u, int curr) 8 | { 9 | if (col[u] != -1 and col[u] != curr) 10 | { 11 | bipart = false; 12 | return; 13 | } 14 | col[u] = curr; 15 | if (vis[u]) 16 | return; 17 | vis[u] = true; 18 | for (auto i : adj[u]) 19 | { 20 | color(i, curr xor 1); 21 | } 22 | } 23 | int main() 24 | { 25 | bipart = true; 26 | int n, m; 27 | cin >> n >> m; 28 | adj = vector>(n); 29 | vis = vector(n, false); 30 | col = vector(n, -1); 31 | for (int i = 0; i < m; i++) 32 | { 33 | int u, v; 34 | cin >> u >> v; 35 | adj[u].push_back(v); 36 | adj[v].push_back(u); 37 | } 38 | for (int i = 0; i < n; i++) 39 | { 40 | if (!vis[i]) 41 | { 42 | color(i, 0); 43 | } 44 | } 45 | if (bipart) 46 | cout << "Graph is bipartite" << endl; 47 | else 48 | cout << "Graph is not bipartite" << endl; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /activityselection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // #define int long long 14 | #define vi vector 15 | #define pii pair 16 | #define vii vector 17 | #define rep(i, a, b) for (int i = a; i < b; i++) 18 | #define ff first 19 | #define ss second 20 | #define setBits(x) builtin_popcount(x) 21 | 22 | signed main() 23 | { 24 | int n; 25 | cin >> n; 26 | vector> v; 27 | for (int i = 0; i < n; i++) 28 | { 29 | int start, end; 30 | cin >> start >> end; 31 | v.push_back({start, end}); 32 | } 33 | sort(v.begin(), v.end(), [&](vector &a, vector &b) 34 | { return a[1] < b[1]; }); 35 | int take = 1; 36 | int end = v[0][1]; 37 | for (int i = 1; i < n; i++) 38 | { 39 | if (v[i][0] >= end) 40 | { 41 | take++; 42 | end = v[i][1]; 43 | } 44 | } 45 | cout << take << "\n"; 46 | return 0; 47 | } -------------------------------------------------------------------------------- /stackusingqueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Stack 4 | { 5 | int N; 6 | queue q1; 7 | queue q2; 8 | 9 | public: 10 | Stack() 11 | { 12 | N = 0; 13 | } 14 | void push(int val) // making push costly 15 | { 16 | q2.push(val); 17 | N++; 18 | while (!q1.empty()) 19 | { 20 | q2.push(q1.front()); 21 | q1.pop(); 22 | } 23 | queue temp = q1; 24 | q1 = q2; 25 | q2 = temp; 26 | } 27 | void pop() 28 | { 29 | q1.pop(); 30 | N--; 31 | } 32 | int top() 33 | { 34 | return q1.front(); 35 | } 36 | int size() 37 | { 38 | return N; 39 | } 40 | }; 41 | int main() 42 | { 43 | Stack st; 44 | st.push(1); 45 | st.push(2); 46 | st.push(3); 47 | st.push(4); 48 | cout << st.top() << endl; 49 | st.pop(); 50 | cout << st.top() << endl; 51 | st.pop(); 52 | cout << st.top() << endl; 53 | st.pop(); 54 | cout << st.top() << endl; 55 | st.pop(); 56 | cout << st.size() << endl; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /optimalmerge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // #define int long long 14 | #define vi vector 15 | #define pii pair 16 | #define vii vector 17 | #define rep(i, a, b) for (int i = a; i < b; i++) 18 | #define ff first 19 | #define ss second 20 | #define setBits(x) builtin_popcount(x) 21 | 22 | signed main() 23 | { 24 | int n; 25 | cin >> n; 26 | vector a(n); 27 | for (int i = 0; i < n; i++) 28 | { 29 | cin >> a[i]; 30 | } 31 | priority_queue, greater> minheap; 32 | for (int i = 0; i < n; i++) 33 | { 34 | minheap.push(a[i]); 35 | } 36 | int ans = 0; 37 | while (minheap.size() > 1) 38 | { 39 | int e1 = minheap.top(); 40 | minheap.pop(); 41 | int e2 = minheap.top(); 42 | minheap.pop(); 43 | ans += e1 + e2; 44 | minheap.push(e1 + e2); 45 | } 46 | cout << ans << endl; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /postfix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int postfixEvaluation(string s) 6 | { 7 | stack st; 8 | for (int i = 0; i < s.length(); i++) 9 | { 10 | if (s[i] >= '0' && s[i] <= '9') 11 | { 12 | st.push(s[i] - '0'); 13 | } 14 | else 15 | { 16 | int op2 = st.top(); 17 | st.pop(); 18 | int op1 = st.top(); 19 | st.pop(); 20 | switch (s[i]) 21 | { 22 | case '+': 23 | st.push(op1 + op2); 24 | break; 25 | case '-': 26 | st.push(op1 - op2); 27 | break; 28 | case '*': 29 | st.push(op1 * op2); 30 | break; 31 | case '/': 32 | st.push(op1 / op2); 33 | break; 34 | case '^': 35 | st.push(pow(op1, op2)); 36 | break; 37 | } 38 | } 39 | } 40 | return st.top(); 41 | } 42 | int main() 43 | { 44 | cout << postfixEvaluation("46+2/5*7+") << endl; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /prefix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int prefixEvaluation(string s) 6 | { 7 | stack st; 8 | for (int i = s.length() - 1; i >= 0; i--) 9 | { 10 | if (s[i] >= '0' && s[i] <= '9') 11 | { 12 | st.push(s[i] - '0'); 13 | } 14 | else 15 | { 16 | int op1 = st.top(); 17 | st.pop(); 18 | int op2 = st.top(); 19 | st.pop(); 20 | switch (s[i]) 21 | { 22 | case '+': 23 | st.push(op1 + op2); 24 | break; 25 | case '-': 26 | st.push(op1 - op2); 27 | break; 28 | case '*': 29 | st.push(op1 * op2); 30 | break; 31 | case '/': 32 | st.push(op1 / op2); 33 | break; 34 | case '^': 35 | st.push(pow(op1, op2)); 36 | break; 37 | } 38 | } 39 | } 40 | return st.top(); 41 | } 42 | int main() 43 | { 44 | cout << prefixEvaluation("-+7*45+20") << endl; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /searchsortrotatedarr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int searchInRotatedArray(int arr[], int key, int left, int right) 4 | { 5 | if (left > right) 6 | { 7 | return -1; 8 | } 9 | int mid = (left + right) / 2; 10 | if (arr[mid] == key) 11 | { 12 | return mid; 13 | } 14 | if (arr[left] <= arr[mid]) 15 | { 16 | if (key >= arr[left] && key <= arr[mid]) 17 | { 18 | return searchInRotatedArray(arr, key, left, mid - 1); 19 | } 20 | return searchInRotatedArray(arr, key, mid + 1, right); 21 | } 22 | if (key >= arr[mid] && key <= arr[right]) 23 | { 24 | return searchInRotatedArray(arr, key, mid + 1, right); 25 | } 26 | return searchInRotatedArray(arr, key, left, mid - 1); 27 | } 28 | int main() 29 | { 30 | int arr[] = {6, 7, 8, 9, 10, 1, 2, 5}; 31 | int n = 8; 32 | int key = 8; 33 | int idx = searchInRotatedArray(arr, key, 0, n - 1); 34 | if (idx == -1) 35 | { 36 | cout << "Key doesn't exist" << endl; 37 | } 38 | else 39 | { 40 | cout << "Key is present at idx: " << idx << endl; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /maxpathsum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | int maxPathSumUtil(Node *root, int &ans) 16 | { 17 | if (root == NULL) 18 | { 19 | return 0; 20 | } 21 | int left = maxPathSumUtil(root->left, ans); 22 | int right = maxPathSumUtil(root->right, ans); 23 | int nodeMax = max(max(root->data, root->data + left + right), max(root->data + left, root->data + right)); 24 | ans = max(ans, nodeMax); 25 | int singlePathSum = max(root->data, max(root->data + left, root->data + right)); 26 | return singlePathSum; 27 | } 28 | int maxPathSum(Node *root) 29 | { 30 | int ans = INT_MIN; 31 | maxPathSumUtil(root, ans); 32 | return ans; 33 | } 34 | int main() 35 | { 36 | struct Node *root = new Node(1); 37 | root->left = new Node(2); 38 | root->right = new Node(3); 39 | root->left->left = new Node(4); 40 | root->right->right = new Node(5); 41 | //test 42 | cout << maxPathSum(root); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /connectedcompoquestion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector vis; 4 | int n, m; 5 | vector> adj; 6 | vector components; 7 | int get_comp(int idx) 8 | { 9 | if (vis[idx]) 10 | return 0; 11 | vis[idx] = true; 12 | int ans = 1; 13 | for (auto i : adj[idx]) 14 | { 15 | if (!vis[i]) 16 | { 17 | ans += get_comp(i); 18 | vis[i] = true; 19 | } 20 | } 21 | return ans; 22 | } 23 | int main() 24 | { 25 | cin >> n >> m; 26 | adj = vector>(n); 27 | vis = vector(n); 28 | for (int i = 0; i < m; i++) 29 | { 30 | int u, v; 31 | cin >> u >> v; 32 | adj[u].push_back(v); 33 | adj[v].push_back(u); 34 | } 35 | for (int i = 0; i < n; i++) 36 | { 37 | if (!vis[i]) 38 | { 39 | components.push_back(get_comp(i)); 40 | } 41 | } 42 | // for (auto i : components) 43 | // { 44 | // cout << i << " "; 45 | // } 46 | long long ans = 0; 47 | for (auto i : components) 48 | { 49 | ans += i * (n - i); 50 | } 51 | cout << ans / 2; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /DP_Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e5 + 2, MOD = 1e9 + 7; 14 | int dp[N]; 15 | int fib(int n) 16 | { 17 | if (n == 0) 18 | { 19 | return 0; 20 | } 21 | if (n == 1) 22 | { 23 | return 0; 24 | } 25 | if (n == 2) 26 | { 27 | return 1; 28 | } 29 | if (dp[n] != -1) 30 | return dp[n]; 31 | dp[n] = fib(n - 1) + fib(n - 2); 32 | return dp[n]; 33 | } 34 | int main() 35 | { 36 | int n; 37 | cin >> n; 38 | rep(i, 0, N) 39 | { 40 | dp[i] = -1; 41 | } 42 | cout << fib(n) << endl; 43 | return 0; 44 | 45 | // int n; 46 | // cin >> n; 47 | // vi dp(n + 1); 48 | // dp[0] = 0; 49 | // dp[1] = 1; 50 | // dp[2] = 1; 51 | // for (int i = 3; i <= n; i++) 52 | // { 53 | // dp[i] = dp[i - 1] + dp[i - 2]; 54 | // } 55 | // cout << dp[n] << endl; 56 | // return 0; 57 | } -------------------------------------------------------------------------------- /maxmindistance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | bool isFeasible(int mid, int arr[], int n, int k) 5 | { 6 | int pos = arr[0], elements = 1; 7 | for (int i = 1; i < n; i++) 8 | { 9 | if (arr[i] - pos >= mid) 10 | { 11 | pos = arr[i]; 12 | elements++; 13 | if (elements == k) 14 | { 15 | return true; 16 | } 17 | } 18 | } 19 | return false; 20 | } 21 | int largestMinDistance(int arr[], int n, int k) 22 | { 23 | sort(arr, arr + n); 24 | int result = -1; 25 | int left = 1; 26 | int right = arr[n - 1]; 27 | while (left < right) 28 | { 29 | int mid = (left + right) / 2; 30 | if (isFeasible(mid, arr, n, k)) 31 | { 32 | result = max(result, mid); 33 | left = mid + 1; 34 | } 35 | else 36 | { 37 | right = mid; 38 | } 39 | } 40 | return result; 41 | } 42 | int main() 43 | { 44 | int arr[] = {1, 2, 8, 4, 9}; 45 | int n = 5; 46 | int k = 3; 47 | cout << "largest min. distance is: " << largestMinDistance(arr, n, k) << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /painter'spartition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int findFeasible(int boards[], int n, int limit) 4 | { 5 | int painters = 1, sum = 0; 6 | for (int i = 0; i < n; i++) 7 | { 8 | sum += boards[i]; 9 | if (sum > limit) 10 | { 11 | sum = boards[i]; 12 | painters++; 13 | } 14 | } 15 | return painters; 16 | } 17 | int paintersPartition(int boards[], int n, int m) 18 | { 19 | int totalLength = 0, k = 0; 20 | for (int i = 0; i < n; i++) 21 | { 22 | k = max(k, boards[i]); 23 | totalLength += boards[i]; 24 | } 25 | int low = k, high = totalLength; 26 | while (low < high) 27 | { 28 | int mid = (low + high) / 2; 29 | int painters = findFeasible(boards, n, mid); 30 | if (painters <= m) 31 | { 32 | high = mid; 33 | } 34 | else 35 | { 36 | low = mid + 1; 37 | } 38 | } 39 | return low; 40 | } 41 | int main() 42 | { 43 | int arr[] = {10, 20, 30, 40}; 44 | int n = 4; 45 | int m = 2; 46 | cout << "The min. time to paint boards: " << paintersPartition(arr, n, m) << endl; 47 | return 0; 48 | } 49 | //ERROR -------------------------------------------------------------------------------- /queueusingstack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class que 4 | { 5 | stack s1; 6 | stack s2; 7 | 8 | public: 9 | void push(int x) 10 | { 11 | s1.push(x); 12 | } 13 | int pop() // pop or deque is made costly 14 | { 15 | if (s1.empty() && s2.empty()) 16 | { 17 | cout << "Queue is empty\n"; 18 | return -1; 19 | } 20 | if (s2.empty()) 21 | { 22 | while (!s1.empty()) 23 | { 24 | s2.push(s1.top()); 25 | s1.pop(); 26 | } 27 | } 28 | int topval = s2.top(); 29 | s2.pop(); 30 | return topval; 31 | } 32 | bool empty() 33 | { 34 | if (s1.empty() && s2.empty()) 35 | return true; 36 | return false; 37 | } 38 | }; 39 | int main() 40 | { 41 | que q; 42 | q.push(1); 43 | q.push(2); 44 | q.push(3); 45 | q.push(4); 46 | cout << q.pop() << endl; 47 | q.push(5); 48 | cout << q.pop() << endl; 49 | cout << q.pop() << endl; 50 | cout << q.pop() << endl; 51 | cout << q.pop() << endl; 52 | cout << q.pop() << endl; 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /cycledetectundirectgraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool iscycle(int src, vector> &adj, vector &visited, int parent) 4 | { 5 | visited[src] = true; 6 | for (auto i : adj[src]) 7 | { 8 | if (i != parent) 9 | { 10 | if (visited[i]) 11 | return true; 12 | if (!visited[i] and iscycle(i, adj, visited, src)) 13 | { 14 | return true; 15 | } 16 | } 17 | } 18 | return false; 19 | } 20 | int main() 21 | { 22 | int n, m; 23 | cin >> n >> m; 24 | vector> adj(n); 25 | for (int i = 0; i < m; i++) 26 | { 27 | int u, v; 28 | cin >> u >> v; 29 | adj[u].push_back(v); 30 | adj[v].push_back(u); 31 | } 32 | bool cycle = false; 33 | vector visited(n, false); 34 | for (int i = 0; i < n; i++) 35 | { 36 | if (!visited[i] and iscycle(i, adj, visited, -1)) 37 | { 38 | cycle = true; 39 | } 40 | } 41 | if (cycle) 42 | { 43 | cout << "Cycle is present!" << endl; 44 | } 45 | else 46 | { 47 | cout << "Cycle is not present!" << endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /levelordertraversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | void printLevelOrder(Node *root) 16 | { 17 | if (root == NULL) 18 | { 19 | return; 20 | } 21 | queue q; 22 | q.push(root); 23 | q.push(NULL); 24 | while (!q.empty()) 25 | { 26 | Node *node = q.front(); 27 | q.pop(); 28 | if (node != NULL) 29 | { 30 | cout << node->data << " "; 31 | if (node->left) 32 | q.push(node->left); 33 | if (node->right) 34 | q.push(node->right); 35 | } 36 | else if (!q.empty()) 37 | q.push(NULL); 38 | } 39 | } 40 | int main() 41 | { 42 | struct Node *root = new Node(1); 43 | root->left = new Node(2); 44 | root->right = new Node(3); 45 | root->left->left = new Node(4); 46 | root->left->right = new Node(5); 47 | root->right->left = new Node(6); 48 | root->right->right = new Node(7); 49 | 50 | printLevelOrder(root); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /leftview.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | Node *left,*right; 7 | Node(int val) 8 | { 9 | data = val; 10 | left = NULL; 11 | right = NULL; 12 | } 13 | }; 14 | void leftView(Node *root) 15 | { 16 | if (root == NULL) 17 | { 18 | return; 19 | } 20 | queue q; 21 | q.push(root); 22 | while (!q.empty()) 23 | { 24 | int n = q.size(); 25 | for (int i = 0; i < n; i++) 26 | { 27 | Node *temp = q.front(); 28 | q.pop(); 29 | if (i == 1) 30 | { 31 | cout << temp->data << " "; 32 | } 33 | if (temp->left != NULL) 34 | { 35 | q.push(temp->left); 36 | } 37 | if (temp->right != NULL) 38 | { 39 | q.push(temp->right); 40 | } 41 | } 42 | } 43 | } 44 | int main() 45 | { 46 | Node *root = new Node(5); 47 | root->left = new Node(3); 48 | root->right = new Node(6); 49 | root->left->left = new Node(2); 50 | root->left->right = new Node(4); 51 | //left view 52 | leftView(root); 53 | return 0; 54 | } 55 | //ERROR -------------------------------------------------------------------------------- /checkBST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | Node *left, *right; 7 | Node(int val) 8 | { 9 | data = val; 10 | left = NULL; 11 | right = NULL; 12 | } 13 | }; 14 | //check for BST 15 | bool isBST(Node* root,Node* min=NULL,Node* max=NULL){ 16 | if(root==NULL){ 17 | return true; 18 | } 19 | if(min!=NULL && root->data <= min->data){ 20 | return false; 21 | } 22 | if(max!=NULL && root->data >= max->data){ 23 | return false; 24 | } 25 | bool leftValid=isBST(root->left,min,root); 26 | bool rightValid=isBST(root->right,root,max); 27 | return leftValid and rightValid; 28 | } 29 | int main() 30 | { 31 | Node *root1 = new Node(1); 32 | root1->left = new Node(2); 33 | root1->right = new Node(3); 34 | 35 | if(isBST(root1,NULL,NULL)){ 36 | cout<<"Valid BST"<left = new Node(2); 43 | root2->right = new Node(8); 44 | 45 | if(isBST(root2,NULL,NULL)){ 46 | cout<<"Valid BST"< 2 | #include 3 | using namespace std; 4 | void computeNumberFromSubarray(vector arr, int k) 5 | { 6 | pair ans; 7 | int sum = 0; 8 | for (int i = 0; i < k; i++) 9 | { 10 | sum += arr[i]; 11 | } 12 | bool found = false; 13 | if (sum % 3 == 0) 14 | { 15 | ans = make_pair(0, k - 1); 16 | found = true; 17 | } 18 | for (int j = k; j < arr.size(); j++) 19 | { 20 | if (found) 21 | { 22 | break; 23 | } 24 | sum = sum + arr[j] - arr[j - k]; 25 | if (sum % 3 == 0) 26 | { 27 | ans = make_pair(j - k + 1, j); 28 | found = true; 29 | } 30 | } 31 | if (!found) 32 | { 33 | ans = make_pair(-1, 0); 34 | } 35 | if (ans.first == -1) 36 | { 37 | cout << "No such subarray exists" << endl; 38 | } 39 | else 40 | { 41 | for (int i = ans.first; i <= ans.second; i++) 42 | { 43 | cout << arr[i] << " "; 44 | } 45 | cout << endl; 46 | } 47 | } 48 | int main() 49 | { 50 | vector arr = {84, 23, 45, 12, 56, 82}; 51 | int k = 3; 52 | computeNumberFromSubarray(arr, k); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /DP_01knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e3 + 2, MOD = 1e9 + 7; 14 | int val[N], wt[N]; 15 | int dp[N][N]; 16 | int knapsack(int n, int w) 17 | { 18 | if (w < 0) 19 | { 20 | return 0; 21 | } 22 | if (n <= 0) 23 | { 24 | return 0; 25 | } 26 | if (dp[n][w] != -1) 27 | return dp[n][w]; 28 | if (wt[n - 1] > w) 29 | { 30 | dp[n][w] = knapsack(n - 1, w); 31 | } 32 | else 33 | dp[n][w] = max(knapsack(n - 1, w), knapsack(n - 1, w - wt[n - 1]) + val[n - 1]); 34 | return dp[n][w]; 35 | } 36 | int main() 37 | { 38 | rep(i, 0, N) 39 | { 40 | rep(j, 0, N) 41 | { 42 | dp[i][j] = -1; 43 | } 44 | } 45 | int n; 46 | cin >> n; 47 | rep(i, 0, n) 48 | { 49 | cin >> wt[i]; 50 | } 51 | rep(i, 0, n) 52 | { 53 | cin >> val[i]; 54 | } 55 | int w; 56 | cin >> w; 57 | cout << knapsack(n, w) << endl; 58 | return 0; 59 | } -------------------------------------------------------------------------------- /dijkstraalgo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int inf = 1e7; 4 | int main() 5 | { 6 | int n, m; 7 | cin >> n >> m; 8 | vector dist(n + 1, inf); 9 | vector>> graph(n + 1); 10 | for (int i = 0; i < m; i++) 11 | { 12 | int u, v, w; 13 | cin >> u >> v >> w; 14 | graph[u].push_back({v, w}); 15 | graph[v].push_back({u, w}); 16 | } 17 | int source; 18 | cin >> source; 19 | dist[source] = 0; 20 | set> s; 21 | //{wt,vertex} 22 | s.insert({0, source}); 23 | while (!s.empty()) 24 | { 25 | auto x = *(s.begin()); 26 | s.erase(x); 27 | for (auto it : graph[x.second]) 28 | { 29 | if (dist[it.first] > dist[x.second] + it.second) 30 | { 31 | s.erase({dist[it.first], it.first}); 32 | dist[it.first] = dist[x.second] + it.second; 33 | s.insert({dist[it.first], it.first}); 34 | } 35 | } 36 | } 37 | for (int i = 1; i <= n; i++) 38 | { 39 | if (dist[i] < inf) 40 | { 41 | cout << dist[i] << " "; 42 | } 43 | else 44 | { 45 | cout << -1 << " "; 46 | } 47 | } 48 | return 0; 49 | } -------------------------------------------------------------------------------- /factorialknapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // #define int long long 14 | #define vi vector 15 | #define pii pair 16 | #define vii vector 17 | #define rep(i, a, b) for (int i = a; i < b; i++) 18 | #define ff first 19 | #define ss second 20 | #define setBits(x) builtin_popcount(x) 21 | 22 | bool compare(pii p1, pii p2) 23 | { 24 | double v1 = (double)p1.ff / p1.ss; 25 | double v2 = (double)p2.ff / p2.ss; 26 | return v1 > v2; 27 | } 28 | signed main() 29 | { 30 | int n; 31 | cin >> n; 32 | vii a(n); 33 | rep(i, 0, n) 34 | { 35 | cin >> a[i].ff >> a[i].ss; 36 | } 37 | int w; 38 | cin >> w; 39 | sort(a.begin(), a.end(), compare); 40 | int ans = 0; 41 | rep(i, 0, n) 42 | { 43 | if (w >= a[i].ss) 44 | { 45 | ans += a[i].ff; 46 | w -= a[i].ss; 47 | continue; 48 | } 49 | double vw = (double)a[i].ff / a[i].ss; 50 | ans += vw * w; 51 | w = 0; 52 | break; 53 | } 54 | cout << ans << endl; 55 | return 0; 56 | } -------------------------------------------------------------------------------- /cycledetectdirectgraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool iscycle(int src, vector> &adj, vector &visited, vector &stack) 4 | { 5 | stack[src] = true; 6 | if (!visited[src]) 7 | { 8 | visited[src] = true; 9 | for (auto i : adj[src]) 10 | { 11 | if (!visited[i] and iscycle(i, adj, visited, stack)) 12 | { 13 | return true; 14 | } 15 | if(stack[i]) 16 | return true; 17 | } 18 | } 19 | stack[src]=false; 20 | return false; 21 | } 22 | int main() 23 | { 24 | int n, m; 25 | cin >> n >> m; 26 | vector> adj(n); 27 | for (int i = 0; i < m; i++) 28 | { 29 | int u, v; 30 | cin >> u >> v; 31 | adj[u].push_back(v); 32 | } 33 | bool cycle = false; 34 | vector stack(n, 0); 35 | vector visited(n, false); 36 | for (int i = 0; i < n; i++) 37 | { 38 | if (!visited[i] and iscycle(i, adj, visited, stack)) 39 | { 40 | cycle = true; 41 | } 42 | } 43 | if (cycle) 44 | { 45 | cout << "Cycle is present!" << endl; 46 | } 47 | else 48 | { 49 | cout << "Cycle is not present!" << endl; 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /sumreplacement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | void sumReplace(Node *root) 16 | { 17 | if (root == NULL) 18 | { 19 | return; 20 | } 21 | sumReplace(root->left); 22 | sumReplace(root->right); 23 | 24 | if (root->left != NULL) 25 | { 26 | root->data += root->left->data; 27 | } 28 | if (root->right != NULL) 29 | { 30 | root->data += root->right->data; 31 | } 32 | } 33 | void preorder(Node *root) 34 | { 35 | if (root == NULL) 36 | { 37 | return; 38 | } 39 | cout << root->data << " "; 40 | preorder(root->left); 41 | preorder(root->right); 42 | } 43 | int main() 44 | { 45 | struct Node *root = new Node(1); 46 | root->left = new Node(2); 47 | root->right = new Node(3); 48 | root->left->left = new Node(4); 49 | root->left->right = new Node(5); 50 | root->right->left = new Node(6); 51 | root->right->right = new Node(7); 52 | 53 | preorder(root); 54 | cout << "\n"; 55 | sumReplace(root); 56 | preorder(root); 57 | cout << "\n"; 58 | return 0; 59 | } -------------------------------------------------------------------------------- /balancedparenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | bool isValid(string s){ 5 | int n=s.size(); 6 | stack st; 7 | bool ans=true; 8 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | // #define int long long 7 | #define vi vector 8 | #define pii pair 9 | #define vii vector 10 | #define rep(i, a, b) for (int i = a; i < b; i++) 11 | #define ff first 12 | #define ss second 13 | #define setBits(x) builtin_popcount(x) 14 | 15 | void heapify(vi &a, int n, int i) 16 | { 17 | int maxIndx = i; 18 | int l = 2 * i + 1; 19 | int r = 2 * i + 2; 20 | if (l < n && a[l] > a[maxIndx]) 21 | maxIndx = l; 22 | if (r < n && a[r] > a[maxIndx]) 23 | maxIndx = r; 24 | if (maxIndx != i) 25 | { 26 | swap(a[i], a[maxIndx]); 27 | heapify(a, n, maxIndx); 28 | } 29 | } 30 | 31 | void heapsort(vi &a) 32 | { 33 | int n = a.size(); 34 | for (int i = n / 2 - 1; i >= 0; i--) 35 | { 36 | heapify(a, n, i); 37 | } 38 | for (int i = n - 1; i > 0; i--) 39 | { 40 | swap(a[0], a[i]); 41 | heapify(a, i, 0); 42 | } 43 | } 44 | signed main() 45 | { 46 | int n; 47 | cin >> n; 48 | 49 | vi a(n); 50 | 51 | rep(i, 0, n) 52 | cin >> 53 | a[i]; 54 | 55 | heapsort(a); 56 | rep(i, 0, n) 57 | { 58 | cout << a[i] << " "; 59 | } 60 | return 0; 61 | } -------------------------------------------------------------------------------- /sumatkthnode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | int sumAtK(Node* root, int k){ 16 | if(root==NULL){ 17 | return -1; 18 | } 19 | queue q; 20 | q.push(root); 21 | q.push(NULL); 22 | int level=0; 23 | int sum=0; 24 | while(!q.empty()){ 25 | Node* node=q.front(); 26 | q.pop(); 27 | if(node!=NULL){ 28 | if(level==k){ 29 | sum+=node->data; 30 | } 31 | if(node->left) 32 | q.push(node->left); 33 | if(node->right) 34 | q.push(node->right); 35 | } 36 | else if(!q.empty()){ 37 | q.push(NULL); 38 | level++; 39 | } 40 | } 41 | return sum; 42 | } 43 | int main() 44 | { 45 | struct Node *root = new Node(1); 46 | root->left = new Node(2); 47 | root->right = new Node(3); 48 | root->left->left = new Node(4); 49 | root->left->right = new Node(5); 50 | root->right->left = new Node(6); 51 | root->right->right = new Node(7); 52 | 53 | cout< 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e5 + 2, MOD = 1e9 + 7; 14 | int dp[N]; 15 | int minSquare(int n) 16 | { 17 | if (n == 1 || n == 2 || n == 3 || n == 0) 18 | { 19 | return n; 20 | } 21 | if (dp[n] != MOD) 22 | return dp[n]; 23 | for (int i = 1; i * i <= n; i++) 24 | { 25 | dp[n] = min(dp[n], 1 + minSquare(n - i * i)); 26 | } 27 | return dp[n]; 28 | } 29 | int main() 30 | { 31 | rep(i, 0, N) 32 | { 33 | dp[i] = MOD; 34 | } 35 | int n; 36 | cin >> n; 37 | cout << minSquare(n) << endl; 38 | return 0; 39 | 40 | // int n; 41 | // cin >> n; 42 | // vi dp(n + 1, MOD); 43 | // dp[0] = 0; 44 | // dp[1] = 1; 45 | // dp[2] = 2; 46 | // dp[3] = 3; 47 | // for (int i = 1; i * i <= n; i++) 48 | // { 49 | // for (int j = 0; i * i + j <= n; j++) 50 | // { 51 | // dp[i * i + j] = min(dp[i * i + j], 1 + dp[j]); 52 | // } 53 | // } 54 | // cout << dp[n] << endl; 55 | // return 0; 56 | } -------------------------------------------------------------------------------- /binarytraversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | void preorder(struct Node *root) 16 | { 17 | if (root == NULL) 18 | { 19 | return; 20 | } 21 | cout << root->data << " "; 22 | preorder(root->left); 23 | preorder(root->right); 24 | } 25 | void inorder(struct Node *root) 26 | { 27 | if (root == NULL) 28 | { 29 | return; 30 | } 31 | inorder(root->left); 32 | cout << root->data << " "; 33 | inorder(root->right); 34 | } 35 | void postorder(struct Node *root) 36 | { 37 | if (root == NULL) 38 | { 39 | return; 40 | } 41 | postorder(root->left); 42 | postorder(root->right); 43 | cout << root->data << " "; 44 | } 45 | int main() 46 | { 47 | struct Node *root = new Node(1); 48 | root->left = new Node(2); 49 | root->right = new Node(3); 50 | 51 | root->left->left = new Node(4); 52 | root->left->right = new Node(5); 53 | 54 | root->right->left = new Node(6); 55 | root->right->right = new Node(7); 56 | 57 | // preorder(root); 58 | // inorder(root); 59 | postorder(root); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; // #include 3 | // This program is to understand strings and its functions. 4 | 5 | int main(){ 6 | // string s1="fam", s2="ily"; 7 | 8 | // // s1.append(s2); 9 | // // s1+=s2; 10 | // // cout< 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | void rightView(Node *root) 16 | { 17 | if (root == NULL) 18 | { 19 | return; 20 | } 21 | queue q; 22 | q.push(root); 23 | while (!q.empty()) 24 | { 25 | int n = q.size(); 26 | for (int i = 0; i < n; i++) 27 | { 28 | Node *curr = q.front(); 29 | q.pop(); 30 | if (i == n - 1) 31 | { 32 | cout << curr->data << " "; 33 | } 34 | if (curr->left != NULL) 35 | { 36 | q.push(curr->left); 37 | } 38 | if (curr->right != NULL) 39 | { 40 | q.push(curr->right); 41 | } 42 | } 43 | } 44 | } 45 | int main() 46 | { 47 | struct Node *root = new Node(1); 48 | root->left = new Node(2); 49 | root->right = new Node(3); 50 | root->left->left = new Node(4); 51 | root->left->right = new Node(5); 52 | root->right->left = new Node(6); 53 | root->right->right = new Node(7); 54 | //right view 55 | rightView(root); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /palindromicconcatenation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | bool isPalindrome(int n) 6 | { 7 | int temp = n, number = 0; 8 | //reverse n -> 123....number -> 321 9 | while (temp > 0) 10 | { 11 | number = number * 10 + temp % 10; 12 | temp = temp / 10; 13 | } 14 | if (number == n) 15 | { 16 | return true; 17 | } 18 | return false; 19 | } 20 | int findPalindromicSubarray(vector arr, int k) 21 | { 22 | int num = 0; 23 | for (int i = 0; i < k; i++) 24 | { 25 | num = num * 10 + arr[i]; 26 | } 27 | if (isPalindrome(num)) 28 | { 29 | return 0; 30 | } 31 | for (int j = k; j < arr.size(); j++) 32 | { 33 | num = (num % (int)pow(10, k - 1)) * 10 + arr[j]; 34 | if (isPalindrome(num)) 35 | { 36 | return j - k + 1; 37 | } 38 | } 39 | return -1; 40 | } 41 | int main() 42 | { 43 | vector arr = {2, 3, 5, 1, 1, 5}; 44 | int k = 4; 45 | int ans = findPalindromicSubarray(arr, k); 46 | if (ans == -1) 47 | { 48 | cout << "Palindromic subarray doesn't exists" << endl; 49 | } 50 | else 51 | { 52 | for (int i = ans; i < ans + k; i++) 53 | { 54 | cout << arr[i] << " "; 55 | } 56 | cout << endl; 57 | } 58 | return 0; 59 | } -------------------------------------------------------------------------------- /flattenBT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | void flatten(Node *root) 16 | { 17 | if (root == NULL || (root->left == NULL && root->right == NULL)) 18 | { 19 | return; 20 | } 21 | if (root->left != NULL) 22 | { 23 | flatten(root->left); 24 | Node *temp = root->right; 25 | root->right = root->left; 26 | root->left = NULL; 27 | 28 | Node *t = root->right; 29 | while (t->right != NULL) 30 | { 31 | t = t->right; 32 | } 33 | t->right = temp; 34 | } 35 | flatten(root->right); 36 | } 37 | void inorderPrint(Node *root) 38 | { 39 | if (root == NULL) 40 | { 41 | return; 42 | } 43 | inorderPrint(root->left); 44 | cout << root->data << " "; 45 | inorderPrint(root->right); 46 | } 47 | int main() 48 | { 49 | struct Node *root = new Node(4); 50 | root->left = new Node(9); 51 | root->right = new Node(5); 52 | root->left->left = new Node(1); 53 | root->left->right = new Node(3); 54 | root->right->right = new Node(6); 55 | 56 | flatten(root); 57 | inorderPrint(root); 58 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n, k; 6 | cin >> n >> k; 7 | vector a(n); 8 | for (auto &i : a) 9 | { 10 | cin >> i; 11 | } 12 | 13 | // multiset> s; 14 | // vector ans; 15 | // for (int i = 0; i < k; i++) 16 | // { 17 | // s.insert(a[i]); 18 | // } 19 | // ans.push_back(*s.begin()); 20 | // for (int i = k; i < n; i++) 21 | // { 22 | // s.erase(s.lower_bound(a[i - x])); 23 | // s.insert(a[i]); 24 | // ans.push_back(*s.begin()); 25 | // } 26 | // for (auto i : ans) 27 | // { 28 | // cout << i << " "; 29 | // } 30 | 31 | deque q; 32 | vector ans; 33 | for (int i = 0; i < k; i++) 34 | { 35 | while (!q.empty() && a[q.back()] < a[i]) 36 | { 37 | q.pop_back(); 38 | } 39 | q.push_back(i); 40 | } 41 | ans.push_back(a[q.front()]); 42 | for (int i = k; i < n; i++) 43 | { 44 | if (q.front() == i - k) 45 | { 46 | q.pop_front(); 47 | } 48 | while (!q.empty() && a[q.back()] < a[i]) 49 | { 50 | q.pop_back(); 51 | } 52 | q.push_back(i); 53 | ans.push_back(a[q.front()]); 54 | } 55 | for (auto i : ans) 56 | cout << i << " "; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /primsalgoMST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n, m; 4 | const int N = 1e5 + 3; 5 | vector> g[N]; 6 | int cost = 0; 7 | vector dist(N), parent(N); 8 | vector vis(N); 9 | const int INF = 1e9; 10 | void primsMST(int source) 11 | { 12 | for (int i = 0; i < n; i++) 13 | { 14 | dist[i] = INF; 15 | } 16 | set> s; 17 | dist[source] = 0; 18 | s.insert({0, source}); 19 | //{wt,vertex} 20 | while (!s.empty()) 21 | { 22 | auto x = *(s.begin()); 23 | s.erase(x); 24 | vis[x[1]] = true; 25 | int u = x[1]; 26 | int v = parent[x[1]]; 27 | int w = x[0]; 28 | cout << u << " " << v << " " << w << "\n"; 29 | cost += w; 30 | for (auto it : g[x[1]]) 31 | { 32 | if (vis[it[0]]) 33 | continue; 34 | if (dist[it[0]] > it[1]) 35 | { 36 | s.erase({dist[it[0]], it[0]}); 37 | dist[it[0]] = it[1]; 38 | s.insert({dist[it[0]], it[0]}); 39 | parent[it[0]] = x[1]; 40 | } 41 | } 42 | } 43 | } 44 | int main() 45 | { 46 | cin >> n >> m; 47 | for (int i = 0; i < m; i++) 48 | { 49 | int u, v, w; 50 | cin >> u >> v >> w; 51 | g[u].push_back({v, w}); 52 | g[v].push_back({u, w}); 53 | } 54 | primsMST(0); 55 | cout << cost; 56 | return 0; 57 | } -------------------------------------------------------------------------------- /topkmostfreq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // #define int long long 14 | #define vi vector 15 | #define pii pair 16 | #define vii vector 17 | #define rep(i, a, b) for (int i = a; i < b; i++) 18 | #define ff first 19 | #define ss second 20 | #define setBits(x) builtin_popcount(x) 21 | 22 | signed main() 23 | { 24 | int n, k; 25 | cin >> n >> k; 26 | vi a(n); 27 | rep(i, 0, n) 28 | { 29 | cin >> a[i]; 30 | } 31 | map freq; 32 | rep(i, 0, n) 33 | { 34 | int presentSize = freq.size(); 35 | if (freq[a[i]] == 0 && presentSize == k) 36 | { 37 | break; 38 | } 39 | freq[a[i]]++; 40 | } 41 | vii ans; 42 | map::iterator it; 43 | for (it = freq.begin(); it != freq.end(); it++) 44 | { 45 | if (it->ss != 0) 46 | { 47 | pii p; 48 | p.ff = it->ss; 49 | p.ss = it->ff; 50 | ans.push_back(p); 51 | } 52 | } 53 | sort(ans.begin(), ans.end(), greater()); 54 | vii ::iterator it1; 55 | for (it1 = ans.begin(); it1 != ans.end(); it1++) 56 | { 57 | cout << it1->ss << " " << it1->ff << endl; 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /mergesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void merge(int arr[], int l, int mid, int r) 4 | { 5 | int n1 = mid - l + 1; 6 | int n2 = r - mid; 7 | int a[n1]; 8 | int b[n2]; //temp arrays 9 | 10 | for (int i = 0; i < n1; i++) 11 | { 12 | a[i] = arr[l + i]; 13 | } 14 | for (int i = 0; i < n2; i++) 15 | { 16 | b[i] = arr[mid + 1 + i]; 17 | } 18 | int i = 0; 19 | int j = 0; 20 | int k = l; 21 | while (i < n1 && j < n2) 22 | { 23 | if (a[i] < b[j]) 24 | { 25 | arr[k] = a[i]; 26 | k++; 27 | i++; 28 | } 29 | else 30 | { 31 | arr[k] = b[j]; 32 | k++; 33 | j++; 34 | } 35 | } 36 | while (i < n1) 37 | { 38 | arr[k] = a[i]; 39 | k++; 40 | i++; 41 | } 42 | while (j < n2) 43 | { 44 | arr[k] = b[j]; 45 | k++; 46 | j++; 47 | } 48 | } 49 | void mergeSort(int arr[], int l, int r) 50 | { 51 | if (l < r) 52 | { 53 | int mid = (l + r) / 2; 54 | mergeSort(arr, l, mid); 55 | mergeSort(arr, mid + 1, r); 56 | merge(arr, l, mid, r); 57 | } 58 | } 59 | int main() 60 | { 61 | int arr[] = {5, 4, 3, 2, 1}; 62 | mergeSort(arr, 0, 4); 63 | for (int i = 0; i < 5; i++) 64 | { 65 | cout << arr[i] << " "; 66 | } 67 | cout << endl; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /infix2postfix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int prec(char c) 4 | { 5 | if (c == '^') 6 | { 7 | return 3; 8 | } 9 | else if (c == '*' || '/') 10 | { 11 | return 2; 12 | } 13 | else if (c == '+' || c == '-') 14 | { 15 | return 1; 16 | } 17 | else 18 | { 19 | return -1; 20 | } 21 | } 22 | void infixToPostfix(string s) 23 | { 24 | stack st; 25 | string res; 26 | for (int i = 0; i < s.length(); i++) 27 | { 28 | if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) 29 | { 30 | res += s[i]; 31 | } 32 | else if (s[i] == '(') 33 | { 34 | st.push(s[i]); 35 | } 36 | else if (s[i] == ')') 37 | { 38 | while (st.top() != '(') 39 | { 40 | res += st.top(); 41 | st.pop(); 42 | } 43 | st.pop(); 44 | } 45 | else 46 | { 47 | while (!st.empty() && prec(st.top()) >= prec(s[i])) 48 | { 49 | res += st.top(); 50 | st.pop(); 51 | } 52 | st.push(s[i]); 53 | } 54 | } 55 | while (!st.empty()) 56 | { 57 | res += st.top(); 58 | st.pop(); 59 | } 60 | cout<< res< 2 | #include 3 | using namespace std; 4 | bool isPossible(int arr[], int n, int m, int min) 5 | { 6 | int studentsRequired = 1, sum = 0; 7 | for (int i = 0; i < n; i++) 8 | { 9 | if (arr[i] > min) 10 | { 11 | return false; 12 | } 13 | if (sum + arr[i] > min) 14 | { 15 | studentsRequired++; 16 | sum = arr[i]; 17 | if (studentsRequired > m) 18 | { 19 | return false; 20 | } 21 | } 22 | else 23 | { 24 | sum += arr[i]; 25 | } 26 | } 27 | return true; 28 | } 29 | int allocateMinimumPages(int arr[], int n, int m) 30 | { 31 | int sum = 0; 32 | if (n < m) 33 | { 34 | return -1; 35 | } 36 | for (int i = 0; i < n; i++) 37 | { 38 | sum = sum + arr[i]; 39 | } 40 | int start = 0, end = sum, ans = INT_MAX; 41 | while (start <= end) 42 | { 43 | int mid = (start + end) / 2; 44 | if (isPossible(arr, n, m, mid)) 45 | { 46 | ans = min(ans, mid); 47 | end = mid - 1; 48 | } 49 | else 50 | { 51 | start = mid + 1; 52 | } 53 | } 54 | return ans; 55 | } 56 | int main() 57 | { 58 | int arr[] = {12, 34, 67, 90}; 59 | int n = 4; 60 | int m = 2; 61 | cout << "The min. number of pages: " << allocateMinimumPages(arr, n, m) << endl; 62 | return 0; 63 | } -------------------------------------------------------------------------------- /indenticalBST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | Node *left, *right; 7 | Node(int val) 8 | { 9 | data = val; 10 | left = NULL; 11 | right = NULL; 12 | } 13 | }; 14 | bool isIdentical(Node *root1, Node *root2) 15 | { 16 | if (root1 == NULL && root2 == NULL) 17 | { 18 | return true; 19 | } 20 | else if (root1 == NULL || root2 == NULL) 21 | { 22 | return false; 23 | } 24 | else 25 | { 26 | bool cond1 = root1->data == root2->data; 27 | bool cond2 = isIdentical(root1->left, root2->left); 28 | bool cond3 = isIdentical(root1->right, root2->right); 29 | if (cond1 && cond2 && cond3) 30 | { 31 | return true; 32 | } 33 | return false; 34 | } 35 | } 36 | int main() 37 | { 38 | Node *root = new Node(2); 39 | root->left = new Node(1); 40 | root->right = new Node(3); 41 | 42 | Node *root2 = new Node(6); 43 | root2->left = new Node(3); 44 | root2->right = new Node(10); 45 | 46 | if (isIdentical(root, root)) 47 | { 48 | cout << "BSTs are identical" << endl; 49 | } 50 | else 51 | { 52 | cout << "BSTs not identical" << endl; 53 | } 54 | 55 | if (isIdentical(root, root2)) 56 | { 57 | cout << "BSTs are identical" << endl; 58 | } 59 | else 60 | { 61 | cout << "BSTs not identical" << endl; 62 | } 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /treebypostandinorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | int search(int inorder[], int start, int end, int val) 16 | { 17 | for (int i = start; i <= end; i++) 18 | { 19 | if (inorder[i] == val) 20 | { 21 | return i; 22 | } 23 | } 24 | return -1; 25 | } 26 | Node *buildTree(int postorder[], int inorder[], int start, int end) 27 | { 28 | static int idx = 4; 29 | if (start > end) 30 | { 31 | return NULL; 32 | } 33 | int val = postorder[idx]; 34 | idx--; 35 | Node *curr = new Node(val); 36 | if (start == end) 37 | { 38 | return curr; 39 | } 40 | int pos = search(inorder, start, end, val); 41 | curr->right = buildTree(postorder, inorder, pos + 1, end); 42 | curr->left = buildTree(postorder, inorder, start, pos - 1); 43 | return curr; 44 | } 45 | void inorderPrint(Node *root) 46 | { 47 | if (root == NULL) 48 | { 49 | return; 50 | } 51 | inorderPrint(root->left); 52 | cout << root->data << " "; 53 | inorderPrint(root->right); 54 | } 55 | int main() 56 | { 57 | int postorder[] = {4,2,5,3,1}; 58 | int inorder[] = {4,2,1,5,3}; 59 | Node *root = buildTree(postorder, inorder, 0, 4); 60 | inorderPrint(root); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /treebypreandinorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | int search(int inorder[], int start, int end, int curr) 16 | { 17 | for (int i = start; i <= end; i++) 18 | { 19 | if (inorder[i] == curr) 20 | { 21 | return i; 22 | } 23 | } 24 | return -1; 25 | } 26 | Node *buildTree(int preorder[], int inorder[], int start, int end) 27 | { 28 | static int idx = 0; 29 | if (start > end) 30 | { 31 | return NULL; 32 | } 33 | int curr = preorder[idx]; 34 | idx++; 35 | Node *node = new Node(curr); 36 | if (start == end) 37 | { 38 | return node; 39 | } 40 | int pos = search(inorder, start, end, curr); 41 | node->left = buildTree(preorder, inorder, start, pos - 1); 42 | node->right = buildTree(preorder, inorder, pos + 1, end); 43 | return node; 44 | } 45 | void inorderPrint(Node *root) 46 | { 47 | if (root == NULL) 48 | { 49 | return; 50 | } 51 | inorderPrint(root->left); 52 | cout << root->data << " "; 53 | inorderPrint(root->right); 54 | } 55 | int main() 56 | { 57 | int preorder[] = {1, 2, 4, 3, 5}; 58 | int inorder[] = {4, 2, 1, 5, 3}; 59 | Node *root = buildTree(preorder, inorder, 0, 4); 60 | inorderPrint(root); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /evenoddll.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 insertAtTail(node *&head, int val) 15 | { 16 | node *n = new node(val); 17 | if (head == NULL) 18 | { 19 | head = n; 20 | return; 21 | } 22 | node *temp = head; 23 | while (temp->next != NULL) 24 | { 25 | temp = temp->next; 26 | } 27 | temp->next = n; 28 | } 29 | void display(node *head) 30 | { 31 | node *temp = head; 32 | while (temp != NULL) 33 | { 34 | cout << temp->data << "->"; 35 | temp = temp->next; 36 | } 37 | cout << "NULL" << endl; 38 | } 39 | void evenAfterOdd(node *&head) 40 | { 41 | node *odd = head; 42 | node *even = head->next; 43 | node *evenStart = even; 44 | while (odd->next != NULL && even->next != NULL) 45 | { 46 | odd->next = even->next; 47 | odd = odd->next; 48 | even->next = odd->next; 49 | even = even->next; 50 | } 51 | odd->next = evenStart; 52 | if (odd->next != NULL) 53 | { 54 | even->next = NULL; 55 | } 56 | } 57 | int main() 58 | { 59 | node *head = NULL; 60 | int arr[] = {1, 2, 3, 4, 5, 6}; 61 | for (int i = 0; i < 6; i++) 62 | { 63 | insertAtTail(head, arr[i]); 64 | } 65 | display(head); 66 | evenAfterOdd(head); 67 | display(head); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /perfectnumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | bool isNumberPerfect(int n) 5 | { 6 | int sum = 1; 7 | for (int i = 2; i < sqrt(n); i++) 8 | { 9 | if (n % i == 0) 10 | { 11 | if (i == n / i) 12 | { 13 | sum += i; 14 | } 15 | else 16 | { 17 | sum += i + n / i; 18 | } 19 | } 20 | } 21 | if (sum == n && n != 1) 22 | { 23 | return true; 24 | } 25 | return false; 26 | } 27 | int maxSum(int arr[], int n, int k) 28 | { 29 | if (n < k) 30 | { 31 | cout << "Invalid values" << endl; 32 | return -1; 33 | } 34 | int res = 0; 35 | for (int i = 0; i < k; i++) 36 | { 37 | res += arr[i]; 38 | } 39 | int sum = res; 40 | for (int i = k; i < n; i++) 41 | { 42 | sum += arr[i] - arr[i - k]; 43 | res = max(res, sum); 44 | } 45 | return res; 46 | } 47 | int maxNumberOfPerfect(int arr[], int n, int k) 48 | { 49 | for (int i = 0; i < n; i++) 50 | { 51 | if (isNumberPerfect(arr[i])) 52 | { 53 | arr[i] = 1; 54 | } 55 | else 56 | { 57 | arr[i] = 0; 58 | } 59 | } 60 | return maxSum(arr, n, k); 61 | } 62 | int main() 63 | { 64 | int arr[] = {28, 2, 3, 6, 496, 99, 8128, 24}; 65 | int k = 4; 66 | int n = 8; 67 | cout << maxNumberOfPerfect(arr, n, k) << endl; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /DP_matrixchainmultiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 102, MOD = 1e9 + 7; 14 | int a[N]; 15 | int dp[N][N]; 16 | // int mcm(int i, int j) 17 | // { 18 | // if (i == j) 19 | // { 20 | // return 0; 21 | // } 22 | // if (dp[i][j] != -1) 23 | // { 24 | // return dp[i][j]; 25 | // } 26 | // dp[i][j] = MOD; 27 | // for (int k = i; k < j; k++) 28 | // { 29 | // dp[i][j] = min(dp[i][j], mcm(i, k) + mcm(k + 1, j) + a[i - 1] * a[k] * a[j]); 30 | // } 31 | // return dp[i][j]; 32 | // } 33 | int main() 34 | { 35 | int n; 36 | cin >> n; 37 | // memset(dp, -1, sizeof(dp)); 38 | rep(i, 0, n) 39 | { 40 | cin >> a[i]; 41 | } 42 | // cout << mcm(1, n - 1) << endl; 43 | rep(i, 1, n) 44 | { 45 | dp[i][i] = 0; 46 | } 47 | for (int l = 2; l < n; l++) 48 | { 49 | for (int i = 1; i < n - l + 1; i++) 50 | { 51 | int j = i + l - 1; 52 | dp[i][j] = MOD; 53 | for (int k = i; k < j; k++) 54 | { 55 | dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + a[i - 1] * a[k] * a[j]); 56 | } 57 | } 58 | } 59 | cout << dp[1][n - 1] << endl; 60 | return 0; 61 | } -------------------------------------------------------------------------------- /DP_coinchangeprob.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e3 + 2, MOD = 1e9 + 7; 14 | 15 | // int dp[N][N]; 16 | // int coinChange(vi &a, int n, int x) 17 | // { 18 | // if (x == 0) 19 | // { 20 | // return 1; 21 | // } 22 | // if (x < 0) 23 | // { 24 | // return 0; 25 | // } 26 | // if (n <= 0) 27 | // { 28 | // return 0; 29 | // } 30 | // if (dp[n][x] != -1) 31 | // return dp[n][x]; 32 | 33 | // dp[n][x] = coinChange(a, n, x - a[n - 1]) + coinChange(a, n - 1, x); 34 | // return dp[n][x]; 35 | // } 36 | int main() 37 | { 38 | // rep(i, 0, N) 39 | // { 40 | // rep(j, 0, N) 41 | // dp[i][j] = -1; 42 | // } 43 | int n; 44 | cin >> n; 45 | vi a(n); 46 | rep(i, 0, n) 47 | { 48 | cin >> a[i]; 49 | } 50 | int x; 51 | cin >> x; 52 | // cout << coinChange(a, n, x) << endl; 53 | 54 | vvi dp(n + 1, vi(x + 1, 0)); 55 | dp[0][0] = 1; 56 | rep(i, 1, n + 1) 57 | { 58 | rep(j, 0, x + 1) 59 | { 60 | if (j - a[i - 1] >= 0) 61 | dp[i][j] += dp[i][j - a[i - 1]]; 62 | dp[i][j] += dp[i - 1][j]; 63 | } 64 | } 65 | cout << dp[n][x]; 66 | return 0; 67 | } -------------------------------------------------------------------------------- /DP_longestincreasingsubseq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e5 + 2, MOD = 1e9 + 7; 14 | // int dp[N]; 15 | 16 | // int lis(vi &a, int n) 17 | // { 18 | // if (dp[n] != -1) 19 | // { 20 | // return dp[n]; 21 | // } 22 | // dp[n] = 1; //single element is also an LIS 23 | // rep(i, 0, n) 24 | // { 25 | // if (a[n] > a[i]) 26 | // dp[n] = max(dp[n], 1 + lis(a, i)); 27 | // } 28 | // return dp[n]; 29 | // } 30 | // int main() 31 | // { 32 | // rep(i, 0, N) 33 | // { 34 | // dp[i] = -1; 35 | // } 36 | // int n; 37 | // cin >> n; 38 | // vi a(n); 39 | // rep(i, 0, n) 40 | // { 41 | // cin >> a[i]; 42 | // } 43 | // cout << lis(a, n - 1) << endl; 44 | // return 0; 45 | // } 46 | 47 | int main() 48 | { 49 | int n; 50 | cin >> n; 51 | vi a(n); 52 | rep(i, 0, n) 53 | { 54 | cin >> a[i]; 55 | } 56 | vi dp(n, 1); 57 | int ans = 0; 58 | rep(i, 1, n) 59 | { 60 | rep(j, 0, i) 61 | { 62 | if (a[i] > a[j]) 63 | { 64 | dp[i] = max(dp[i], 1 + dp[j]); 65 | } 66 | } 67 | ans = max(ans, dp[i]); 68 | } 69 | cout << ans << endl; 70 | return 0; 71 | } -------------------------------------------------------------------------------- /stackusingqueue2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Stack 4 | { 5 | int N; 6 | queue q1; 7 | queue q2; 8 | 9 | public: 10 | Stack() 11 | { 12 | N = 0; 13 | } 14 | void push(int val) 15 | { 16 | q1.push(val); 17 | N++; 18 | } 19 | void pop() 20 | { 21 | if (q1.empty()) 22 | { 23 | return; 24 | } 25 | while (q1.size() != 1) 26 | { 27 | q2.push(q1.front()); 28 | q1.pop(); 29 | } 30 | q1.pop(); 31 | N--; 32 | queue temp = q1; 33 | q1 = q2; 34 | q2 = temp; 35 | } 36 | int top() 37 | { 38 | if (q1.empty()) 39 | { 40 | return -1; 41 | } 42 | while (q1.size() != 1) 43 | { 44 | q2.push(q1.front()); 45 | q1.pop(); 46 | } 47 | int ans = q1.front(); 48 | q2.push(ans); 49 | queue temp = q1; 50 | q1 = q2; 51 | q2 = temp; 52 | return ans; 53 | } 54 | int size() 55 | { 56 | return N; 57 | } 58 | }; 59 | int main() 60 | { 61 | Stack st; 62 | st.push(1); 63 | st.push(2); 64 | st.push(3); 65 | st.push(4); 66 | cout << st.top() << endl; 67 | st.pop(); 68 | cout << st.top() << endl; 69 | st.pop(); 70 | cout << st.top() << endl; 71 | st.pop(); 72 | cout << st.top() << endl; 73 | st.pop(); 74 | cout << st.size() << endl; 75 | return 0; 76 | } -------------------------------------------------------------------------------- /cycledetectundirgaphDSU.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e5 + 6; 4 | vector parent(N); 5 | vector sz(N); 6 | void make_set(int v) 7 | { 8 | parent[v] = v; 9 | sz[v] = 1; 10 | } 11 | int find_set(int v) 12 | { 13 | if (v == parent[v]) 14 | { 15 | return v; 16 | } 17 | else 18 | { 19 | return parent[v] = find_set(parent[v]); //path compression 20 | } 21 | } 22 | void union_sets(int a, int b) 23 | { 24 | a = find_set(a); 25 | b = find_set(b); 26 | if (a != b) 27 | { 28 | if (sz[a] < sz[b]) 29 | { 30 | swap(a, b); 31 | } 32 | parent[b] = a; 33 | sz[a] += sz[b]; 34 | } 35 | } 36 | int main() 37 | { 38 | for (int i = 0; i < N; i++) 39 | { 40 | make_set(i); 41 | } 42 | int n, m; 43 | cin >> n >> m; 44 | vector> edges; 45 | for (int i = 0; i < m; i++) 46 | { 47 | int u, v; 48 | cin >> u >> v; 49 | edges.push_back({u, v}); 50 | } 51 | bool cycle = false; 52 | for (auto i : edges) 53 | { 54 | int u = i[0]; 55 | int v = i[1]; 56 | int x = find_set(u); 57 | int y = find_set(v); 58 | if (x == y) 59 | { 60 | cycle = true; 61 | } 62 | else 63 | { 64 | union_sets(u, v); 65 | } 66 | } 67 | if (cycle) 68 | cout << "cycle is present!" << endl; 69 | else 70 | cout << "cycle is not present!" << endl; 71 | return 0; 72 | } -------------------------------------------------------------------------------- /kruskalalgoMST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e5 + 6; 4 | vector parent(N); 5 | vector sz(N); 6 | void make_set(int v) 7 | { 8 | parent[v] = v; 9 | sz[v] = 1; 10 | } 11 | int find_set(int v) 12 | { 13 | if (v == parent[v]) 14 | { 15 | return v; 16 | } 17 | else 18 | { 19 | return parent[v] = find_set(parent[v]); //path compression 20 | } 21 | } 22 | void union_sets(int a, int b) 23 | { 24 | a = find_set(a); 25 | b = find_set(b); 26 | if (a != b) 27 | { 28 | if (sz[a] < sz[b]) 29 | { 30 | swap(a, b); 31 | } 32 | parent[b] = a; 33 | sz[a] += sz[b]; 34 | } 35 | } 36 | int main() 37 | { 38 | for (int i = 0; i < N; i++) 39 | { 40 | make_set(i); 41 | } 42 | int n, m; 43 | cin >> n >> m; 44 | vector> edges; 45 | for (int i = 0; i < m; i++) 46 | { 47 | int u, v, w; 48 | cin >> u >> v >> w; 49 | edges.push_back({w, u, v}); 50 | } 51 | sort(edges.begin(), edges.end()); 52 | int cost = 0; 53 | for (auto i : edges) 54 | { 55 | int w = i[0]; 56 | int u = i[1]; 57 | int v = i[2]; 58 | int x = find_set(u); 59 | int y = find_set(v); 60 | if (x == y) 61 | { 62 | continue; 63 | } 64 | else 65 | { 66 | cout << u << " " << v << "\n"; 67 | cost += w; 68 | union_sets(u, v); 69 | } 70 | } 71 | cout << cost; 72 | return 0; 73 | } -------------------------------------------------------------------------------- /largestBSTinBT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | Node *left, *right; 7 | Node(int val) 8 | { 9 | data = val; 10 | left = NULL; 11 | right = NULL; 12 | } 13 | }; 14 | struct Info 15 | { 16 | int size; 17 | int max; 18 | int min; 19 | int ans; 20 | bool isBST; 21 | }; 22 | Info largestBSTinBT(Node *root) 23 | { 24 | if (root == NULL) 25 | { 26 | return {0, INT_MIN, INT_MAX, 0, true}; 27 | } 28 | if (root->left == NULL && root->right == NULL) 29 | { 30 | return {1, root->data, root->data, 1, true}; 31 | } 32 | 33 | Info leftInfo = largestBSTinBT(root->left); 34 | Info rightInfo = largestBSTinBT(root->right); 35 | 36 | Info curr; 37 | curr.size = (1 + leftInfo.size + rightInfo.size); 38 | 39 | if (leftInfo.isBST && rightInfo.isBST && leftInfo.max < root->data && rightInfo.min > root->data) 40 | { 41 | curr.min = min(leftInfo.min, min(rightInfo.min, root->data)); 42 | curr.min = min(rightInfo.min, min(leftInfo.min, root->data)); 43 | 44 | curr.ans = curr.size; 45 | curr.isBST = true; 46 | return curr; 47 | } 48 | curr.ans = max(leftInfo.ans, rightInfo.ans); 49 | curr.isBST = false; 50 | return curr; 51 | } 52 | int main() 53 | { 54 | Node *root = new Node(15); 55 | root->left = new Node(20); 56 | root->right = new Node(30); 57 | root->left->left = new Node(5); 58 | 59 | cout << "Largest BST in BT: " << largestBSTinBT(root).ans << endl; 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /mergeksortedarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // #define int long long 13 | #define vi vector 14 | #define pii pair 15 | #define vii vector 16 | #define rep(i, a, b) for (int i = a; i < b; i++) 17 | #define ff first 18 | #define ss second 19 | #define setBits(x) builtin_popcount(x) 20 | 21 | signed main() 22 | { 23 | int k; 24 | cin >> k; 25 | 26 | vector> a(k); // 2D vector 27 | 28 | rep(i, 0, k) 29 | { 30 | int size; 31 | cin >> size; 32 | a[i] = vector(size); 33 | rep(j, 0, size) 34 | { 35 | cin >> a[i][j]; 36 | } 37 | } 38 | vector idx(k, 0); 39 | priority_queue, greater> pq; 40 | rep(i, 0, k) 41 | { 42 | pii p; 43 | p.ff = a[i][0]; 44 | p.ss = i; 45 | pq.push(p); 46 | } 47 | vi ans; 48 | while (!pq.empty()) 49 | { 50 | pii x = pq.top(); 51 | pq.pop(); 52 | 53 | ans.push_back(x.ff); 54 | 55 | if (idx[x.ss] + 1 < a[x.ss].size()) 56 | { 57 | pii p; 58 | p.ff = a[x.ss][idx[x.ss] + 1]; 59 | p.ss = x.ss; 60 | pq.push(p); 61 | } 62 | idx[x.ss] += 1; 63 | } 64 | rep(i, 0, ans.size()) 65 | { 66 | cout << ans[i] << endl; 67 | } 68 | cout << endl; 69 | return 0; 70 | } -------------------------------------------------------------------------------- /arrqueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define n 20 4 | class queue 5 | { 6 | int *arr; 7 | int front; 8 | int back; 9 | 10 | public: 11 | queue() 12 | { 13 | arr = new int[n]; 14 | front = -1; 15 | back = -1; 16 | } 17 | void push(int x) 18 | { 19 | if (back == n - 1) 20 | { 21 | cout << "Queue overflow" << endl; 22 | return; 23 | } 24 | back++; 25 | arr[back] = x; 26 | 27 | if (front == -1) 28 | { 29 | front++; 30 | } 31 | } 32 | void pop() 33 | { 34 | if (front == -1 || front > back) 35 | { 36 | cout << "No elements in queue" << endl; 37 | return; 38 | } 39 | front++; 40 | } 41 | int peek() 42 | { 43 | if (front == -1 || front > back) 44 | { 45 | cout << "No elements in queue" << endl; 46 | return -1; 47 | } 48 | return arr[front]; 49 | } 50 | bool empty() 51 | { 52 | if (front == -1 || front > back) 53 | { 54 | return true; 55 | } 56 | return false; 57 | } 58 | }; 59 | int main() 60 | { 61 | queue q; 62 | q.push(1); 63 | q.push(2); 64 | q.push(3); 65 | q.push(4); 66 | cout << q.peek() << endl; 67 | q.pop(); 68 | cout << q.peek() << endl; 69 | q.pop(); 70 | cout << q.peek() << endl; 71 | q.pop(); 72 | cout << q.peek() << endl; 73 | q.pop(); 74 | cout << q.empty() << endl; 75 | return 0; 76 | } -------------------------------------------------------------------------------- /possibleBSTs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | Node *left, *right; 7 | Node(int val) 8 | { 9 | data = val; 10 | left = NULL; 11 | right = NULL; 12 | } 13 | }; 14 | vector constructTrees(int start, int end) 15 | { 16 | vector trees; 17 | if (start > end) 18 | { 19 | trees.push_back(NULL); 20 | return trees; 21 | } 22 | for (int i = start; i <= end; i++) 23 | { 24 | vector leftSubtrees = constructTrees(start, i - 1); 25 | vector rightSubtrees = constructTrees(i + 1, end); 26 | for (int j = 0; j < leftSubtrees.size(); j++) 27 | { 28 | Node *left = leftSubtrees[j]; 29 | for (int k = 0; k < rightSubtrees.size(); k++) 30 | { 31 | Node *right = rightSubtrees[k]; 32 | Node *node = new Node(i); 33 | node->left = left; 34 | node->right = right; 35 | trees.push_back(node); 36 | } 37 | } 38 | } 39 | return trees; 40 | } 41 | void preorder(Node *root) 42 | { 43 | if (root == NULL) 44 | { 45 | return; 46 | } 47 | cout << root->data << " "; 48 | preorder(root->left); 49 | preorder(root->right); 50 | } 51 | int main() 52 | { 53 | vector totalTress = constructTrees(1, 3); 54 | for (int i = 0; i < totalTress.size(); i++) 55 | { 56 | cout << (i + 1) << " : "; 57 | preorder(totalTress[i]); 58 | cout << endl; 59 | } 60 | return 0; 61 | } -------------------------------------------------------------------------------- /shortestdistance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int val) 9 | { 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | Node* LCA(Node* root,int n1,int n2){ 16 | if(root==NULL){ 17 | return NULL; 18 | } 19 | if(root->data==n1 || root->data==n2){ 20 | return root; 21 | } 22 | Node* left=LCA(root->left,n1,n2); 23 | Node* right=LCA(root->right,n1,n2); 24 | 25 | if(left!=NULL && right!=NULL){ 26 | return root; 27 | } 28 | if(left==NULL && right ==NULL){ 29 | return NULL; 30 | } 31 | if(left!=NULL){ 32 | return LCA(root->left,n1,n2); 33 | } 34 | return LCA(root->right,n1,n2); 35 | } 36 | int findDist(Node* root,int k,int dist){ 37 | if(root==NULL){ 38 | return -1; 39 | } 40 | if(root->data==k){ 41 | return dist; 42 | } 43 | int left=findDist(root->left,k,dist+1); 44 | if(left!=-1){ 45 | return left; 46 | } 47 | return findDist(root->right,k,dist+1); 48 | } 49 | int distBtwNodes(Node* root,int n1,int n2){ 50 | Node* lca=LCA(root,n1,n2); 51 | int d1=findDist(lca,n1,0); 52 | int d2=findDist(lca,n2,0); 53 | return d1+d2; 54 | } 55 | int main() 56 | { 57 | struct Node *root = new Node(1); 58 | root->left = new Node(2); 59 | root->right = new Node(3); 60 | root->left->left = new Node(4); 61 | root->right->right = new Node(5); 62 | //test 63 | cout< 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 insertAtTail(node *&head, int val) 15 | { 16 | node *n = new node(val); 17 | if (head == NULL) 18 | { 19 | head = n; 20 | return; 21 | } 22 | node *temp = head; 23 | while (temp->next != NULL) 24 | { 25 | temp = temp->next; 26 | } 27 | temp->next = n; 28 | } 29 | void display(node *head) 30 | { 31 | node *temp = head; 32 | while (temp != NULL) 33 | { 34 | cout << temp->data << "->"; 35 | temp = temp->next; 36 | } 37 | cout << "NULL" << endl; 38 | } 39 | node* reversek(node* &head,int k){ //both 3 pointer approach and recursion needed here. 40 | node* prevptr=NULL; 41 | node* currptr=head; 42 | node* nextptr; 43 | int count=0; 44 | while(currptr!=NULL && countnext; 46 | currptr->next=prevptr; 47 | prevptr=currptr; 48 | currptr=nextptr; 49 | count++; 50 | } 51 | if(nextptr!=NULL){ 52 | head->next=reversek(nextptr,k); 53 | } 54 | return prevptr; 55 | } 56 | int main() 57 | { 58 | node *head = NULL; 59 | insertAtTail(head, 1); 60 | insertAtTail(head, 2); 61 | insertAtTail(head, 3); 62 | insertAtTail(head, 4); 63 | insertAtTail(head, 5); 64 | insertAtTail(head, 6); 65 | display(head); 66 | int k=2; 67 | node* newhead=reversek(head,k); 68 | display(newhead); 69 | return 0; 70 | } -------------------------------------------------------------------------------- /countinversionmerge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | long long merge(int arr[], int l, int mid, int r) 4 | { 5 | long long inv = 0; 6 | int n1 = mid - l + 1; 7 | int n2 = r - mid; 8 | int a[n1]; 9 | int b[n2]; 10 | for (int i = 0; i < n1; i++) 11 | { 12 | a[i] = arr[l + i]; 13 | } 14 | for (int i = 0; i < n2; i++) 15 | { 16 | b[i] = arr[mid + i + 1]; 17 | } 18 | int i = 0, j = 0, k = 1; 19 | while (i < n1 && j < n2) 20 | { 21 | if (a[i] <= b[j]) 22 | { 23 | arr[k] = a[i]; 24 | k++; 25 | i++; 26 | } 27 | else 28 | { 29 | arr[k] = b[j]; 30 | inv += n1 - i; 31 | // a[i],a[i+1],a[i+2].......a[]>b[j] and i> n; 66 | int arr[n]; 67 | for (int i = 0; i < n; i++) 68 | { 69 | cin >> arr[i]; 70 | } 71 | cout << mergeSort(arr, 0, n - 1); 72 | return 0; 73 | } 74 | 75 | //error -------------------------------------------------------------------------------- /graphrepresent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e5 + 2, MOD = 1e9 + 7; 14 | //representation of graphs 15 | 16 | vi adj[N]; 17 | signed main() 18 | { 19 | int n, m; //n=number of nodes & m=number of edges 20 | cin >> n >> m; 21 | vvi adjm(n + 1, vi(n + 1, 0)); //2D vector 22 | rep(i, 0, m) 23 | { 24 | int x, y; 25 | cin >> x >> y; 26 | adjm[x][y] = 1; 27 | adjm[y][x] = 1; 28 | } 29 | cout << "adjcency matrix of above graph is given by:" << endl; 30 | rep(i, 1, n + 1) 31 | { 32 | rep(j, 1, n + 1) 33 | { 34 | cout << adjm[i][j] << " "; 35 | } 36 | cout << endl; 37 | } 38 | if (adjm[3][7] == 1) 39 | { 40 | cout << "there is a edge between 3 and 7" << endl; 41 | } 42 | else 43 | { 44 | cout << "no edge" << endl; 45 | } 46 | cout << endl 47 | << endl; 48 | cin >> n >> m; 49 | rep(i, 0, m) 50 | { 51 | int x, y; 52 | cin >> x >> y; 53 | adj[x].push_back(y); 54 | adj[y].push_back(x); 55 | } 56 | cout << "adjacency list of the above graph is given by:" << endl; 57 | rep(i, 1, n + 1) 58 | { 59 | cout << i << " -> "; 60 | for (int x : adj[i]) 61 | { 62 | cout << x << " "; 63 | } 64 | cout << endl; 65 | } 66 | return 0; 67 | } -------------------------------------------------------------------------------- /llqueue.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 | class queue 15 | { 16 | node *front; 17 | node *back; 18 | 19 | public: 20 | queue() 21 | { 22 | front = NULL; 23 | back = NULL; 24 | } 25 | void push(int x) 26 | { 27 | node *n = new node(x); 28 | if (front == NULL) 29 | { 30 | back = n; 31 | front = n; 32 | } 33 | back->next = n; 34 | back = n; 35 | } 36 | void pop() 37 | { 38 | if (front == NULL) 39 | { 40 | cout << "Queue Underflow" << endl; 41 | return; 42 | } 43 | node *todelete = front; 44 | front = front->next; 45 | delete todelete; 46 | } 47 | int peek() 48 | { 49 | if (front == NULL) 50 | { 51 | cout << "No element in queue" << endl; 52 | return -1; 53 | } 54 | return front->data; 55 | } 56 | bool empty() 57 | { 58 | if (front == NULL) 59 | { 60 | return true; 61 | } 62 | return false; 63 | } 64 | }; 65 | int main() 66 | { 67 | queue q; 68 | q.push(1); 69 | q.push(2); 70 | q.push(3); 71 | q.push(4); 72 | cout << q.peek() << endl; 73 | q.pop(); 74 | cout << q.peek() << endl; 75 | q.pop(); 76 | cout << q.peek() << endl; 77 | q.pop(); 78 | cout << q.peek() << endl; 79 | q.pop(); 80 | cout << q.empty() << endl; 81 | return 0; 82 | } -------------------------------------------------------------------------------- /ratinmaze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool isSafe(int **arr, int x, int y, int n) 4 | { 5 | if (x < n && y < n && arr[x][y] == 1) 6 | { 7 | return true; 8 | } 9 | return false; 10 | } 11 | bool ratInMaze(int **arr, int x, int y, int n, int **solArr) 12 | { 13 | if ((x == (n - 1)) && (y == (n - 1))) 14 | { 15 | solArr[x][y] = 1; 16 | return true; 17 | } 18 | if (isSafe(arr, x, y, n)) 19 | { 20 | solArr[x][y] = 1; 21 | if (ratInMaze(arr, x + 1, y, n, solArr)) 22 | { 23 | return true; 24 | } 25 | if (ratInMaze(arr, x, y + 1, n, solArr)) 26 | { 27 | return true; 28 | } 29 | solArr[x][y] = 0; //backtracking 30 | return false; 31 | } 32 | return false; 33 | } 34 | int main() 35 | { 36 | int n; 37 | cin >> n; 38 | int **arr = new int *[n]; 39 | for (int i = 0; i < n; i++) 40 | { 41 | arr[i] = new int[n]; 42 | } 43 | for (int i = 0; i < n; i++) 44 | { 45 | for (int j = 0; j < n; j++) 46 | cin >> arr[i][j]; 47 | } 48 | int **solArr = new int *[n]; 49 | for (int i = 0; i < n; i++) 50 | { 51 | solArr[i] = new int[n]; 52 | for (int j = 0; j < n; j++) 53 | { 54 | solArr[i][j] = 0; 55 | } 56 | } 57 | if (ratInMaze(arr, 0, 0, n, solArr)) 58 | { 59 | for (int i = 0; i < n; i++) 60 | { 61 | for (int j = 0; j < n; j++) 62 | { 63 | cout << solArr[i][j] << " "; 64 | } 65 | cout << endl; 66 | } 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /infix2prefix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int prec(char c) 6 | { 7 | if (c == '^') 8 | { 9 | return 3; 10 | } 11 | else if (c == '*' || '/') 12 | { 13 | return 2; 14 | } 15 | else if (c == '+' || c == '-') 16 | { 17 | return 1; 18 | } 19 | else 20 | { 21 | return -1; 22 | } 23 | } 24 | string infixToPrefix(string s) 25 | { 26 | reverse(s.begin(), s.end()); 27 | stack st; 28 | string res; 29 | for (int i = 0; i < s.length(); i++) 30 | { 31 | if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) 32 | { 33 | res += s[i]; 34 | } 35 | else if (s[i] == ')') 36 | { 37 | st.push(s[i]); 38 | } 39 | else if (s[i] == '(') 40 | { 41 | while (!st.empty() && st.top() != ')') 42 | { 43 | res += st.top(); 44 | st.pop(); 45 | } 46 | if (!st.empty()) 47 | { 48 | st.pop(); 49 | } 50 | } 51 | else 52 | { 53 | while (!st.empty() && prec(st.top()) >= prec(s[i])) 54 | { 55 | res += st.top(); 56 | st.pop(); 57 | } 58 | st.push(s[i]); 59 | } 60 | } 61 | while (!st.empty()) 62 | { 63 | res += st.top(); 64 | st.pop(); 65 | } 66 | reverse(res.begin(), res.end()); 67 | return res; 68 | } 69 | int main() 70 | { 71 | cout << infixToPrefix("(a-b/c)*(a/k-l)") << endl; 72 | return 0; 73 | } 74 | 75 | //ERROR -------------------------------------------------------------------------------- /verticalorderprint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // #define int long long 13 | #define vi vector 14 | #define pii pair 15 | #define vii vector 16 | #define rep(i, a, b) for (int i = a; i < b; i++) 17 | #define ff first 18 | #define ss second 19 | #define setBits(x) builtin_popcount(x) 20 | 21 | struct Node 22 | { 23 | int key; 24 | Node *left, *right; 25 | }; 26 | Node *newNode(int key) 27 | { 28 | Node *node = new Node; 29 | node->key = key; 30 | node->left = node->right = NULL; 31 | return node; 32 | } 33 | void getVerticalOrder(Node *root, int hdis, map &m) 34 | { 35 | if (root == NULL) 36 | { 37 | return; 38 | } 39 | m[hdis].push_back(root->key); 40 | getVerticalOrder(root->left, hdis - 1, m); 41 | getVerticalOrder(root->right, hdis + 1, m); 42 | } 43 | signed main() 44 | { 45 | Node *root = newNode(10); 46 | root->left = newNode(7); 47 | root->right = newNode(4); 48 | root->left->left = newNode(3); 49 | root->left->right = newNode(11); 50 | root->right->left = newNode(14); 51 | root->right->right = newNode(6); 52 | 53 | map> m; 54 | int hdis = 0; 55 | 56 | getVerticalOrder(root, hdis, m); 57 | map::iterator it; 58 | for (it = m.begin(); it != m.end(); it++) 59 | { 60 | for (int i = 0; i < (it->ss).size(); i++) 61 | { 62 | cout << (it->ss)[i] << " "; 63 | } 64 | cout << endl; 65 | } 66 | return 0; 67 | } -------------------------------------------------------------------------------- /DP_longestcommonsubseq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define vi vector 5 | #define vvi vector 6 | #define pii pair 7 | #define vii vector 8 | #define rep(i, a, b) for (int i = a; i < b; i++) 9 | #define ff first 10 | #define ss second 11 | #define setBits(x) builtin_popcount(x) 12 | 13 | const int N = 1e3 + 2, MOD = 1e9 + 7; 14 | // int dp[N][N]; 15 | // int lcs(string &s1, string &s2, int n, int m) 16 | // { 17 | // if (n == 0 || m == 0) 18 | // { 19 | // return 0; 20 | // } 21 | // if (dp[n][m] != -1) 22 | // return dp[n][m]; 23 | // if (s1[n - 1] == s2[m - 1]) 24 | // { 25 | // dp[n][m] = 1 + lcs(s1, s2, n - 1, m - 1); 26 | // } 27 | // else 28 | // { 29 | // dp[n][m] = max(lcs(s1, s2, n, m - 1), lcs(s1, s2, n - 1, m)); 30 | // } 31 | // return dp[n][m]; 32 | // } 33 | int main() 34 | { 35 | // rep(i, 0, N) 36 | // { 37 | // rep(j, 0, N) 38 | // { 39 | // dp[i][j] = -1; 40 | // } 41 | // } 42 | string s1, s2; 43 | cin >> s1 >> s2; 44 | int n = s1.size(), m = s2.size(); 45 | // cout << lcs(s1, s2, n, m) << endl; 46 | vvi dp(n + 1, vi(m + 1, -1)); 47 | rep(i, 0, n + 1) 48 | { 49 | rep(j, 0, m + 1) 50 | { 51 | if (i == 0 || j == 0) 52 | { 53 | dp[i][j] = 0; 54 | continue; 55 | } 56 | if (s1[i - 1] == s2[j - 1]) 57 | dp[i][j] = 1 + dp[i - 1][j - 1]; 58 | else 59 | { 60 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 61 | } 62 | } 63 | } 64 | cout << dp[n][m] << endl; 65 | return 0; 66 | } -------------------------------------------------------------------------------- /expedi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin >> t; 7 | while (t--) 8 | { 9 | int n; 10 | cin >> n; 11 | 12 | vector> a(n); 13 | for (int i = 0; i < n; i++) 14 | { 15 | cin >> a[i].first >> a[i].second; 16 | } 17 | int l, p; 18 | cin >> l >> p; 19 | for (int i = 0; i < n; i++) 20 | { 21 | a[i].first = l - a[i].first; 22 | } 23 | sort(a.begin(), a.end()); 24 | int ans = 0; 25 | int curr = p; 26 | priority_queue> pq; 27 | bool flag = 0; 28 | for (int i = 0; i < n; i++) 29 | { 30 | if (curr >= l) 31 | { 32 | break; 33 | } 34 | while (curr < a[i].first) 35 | { 36 | if (pq.empty()) 37 | { 38 | flag = 1; 39 | break; 40 | } 41 | ans++; 42 | curr += pq.top(); 43 | pq.pop(); 44 | } 45 | if (flag) 46 | { 47 | break; 48 | } 49 | pq.push(a[i].second); 50 | } 51 | if (flag) 52 | { 53 | cout << "-1" << endl; 54 | continue; 55 | } 56 | while (!pq.empty() && curr < l) 57 | { 58 | curr += pq.top(); 59 | pq.pop(); 60 | ans++; 61 | } 62 | if (curr < l) 63 | { 64 | cout << "-1" << endl; 65 | continue; 66 | } 67 | cout << ans << endl; 68 | } 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /surroundedregion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void change(vector> &A, int x, int y) 4 | { 5 | A[x][y] = '*'; 6 | int dx[] = {0, 0, -1, 1}; 7 | int dy[] = {1, -1, 0, 0}; 8 | for (int i = 0; i < 4; i++) 9 | { 10 | int cx = x + dx[i]; 11 | int cy = y + dy[i]; 12 | if (cx >= 0 and cx < A.size() and cy >= 0 and cy < A[0].size() and A[cx][cy] == 'O') 13 | { 14 | change(A, cx, cy); 15 | } 16 | } 17 | } 18 | int main() 19 | { 20 | int n, m; 21 | cin >> n >> m; 22 | vector> A(n, vector(m)); 23 | for (int i = 0; i < n; i++) 24 | { 25 | for (int j = 0; j < m; j++) 26 | { 27 | cin >> A[i][j]; 28 | } 29 | } 30 | for (int i = 0; i < n; i++) 31 | { 32 | for (int j = 0; j < m; j++) 33 | { 34 | if (i == 0 or i == n - 1 or j == 0 or j == m - 1) 35 | { 36 | if (A[i][j] == 'O') 37 | change(A, i, j); // * convert 38 | } 39 | } 40 | } 41 | for (int i = 0; i < n; i++) 42 | { 43 | for (int j = 0; j < m; j++) 44 | { 45 | if (A[i][j] == 'O') 46 | { 47 | A[i][j] = 'X'; 48 | } 49 | } 50 | } 51 | for (int i = 0; i < n; i++) 52 | { 53 | for (int j = 0; j < m; j++) 54 | { 55 | if (A[i][j] == '*') 56 | { 57 | A[i][j] = 'O'; 58 | } 59 | } 60 | } 61 | for (int i = 0; i < n; i++) 62 | { 63 | for (int j = 0; j < m; j++) 64 | { 65 | cout << A[i][j] << " "; 66 | } 67 | cout << "\n"; 68 | } 69 | return 0; 70 | } --------------------------------------------------------------------------------