├── .DS_Store ├── Doubtclass ├── alternatingbits.cpp ├── arraytargetsumtriplet ├── arraytargetsumtriplet.cpp ├── formbiggestno ├── formbiggestno.cpp ├── medianoftwosortedarrays ├── medianoftwosortedarrays.cpp ├── stringsort └── stringsort.cpp ├── Lecture1 ├── .vscode │ └── settings.json ├── Helloworld.cpp ├── a.out ├── checkprime.cpp ├── ifelse.cpp ├── largestofthree.cpp ├── simpleintrest.cpp ├── takinginput.cpp └── while.cpp ├── Lecture12 ├── insertionsortusingpointers ├── insertionsortusingpointers.cpp ├── pigeonholeprinciple ├── pigeonholeprinciple.cpp ├── pointers ├── pointers.cpp ├── pointersandarrays ├── pointersandarrays.cpp ├── pointersandfunctions ├── pointersandfunctions.cpp ├── selectionsortusingpointers └── selectionsortusingpointers.cpp ├── Lecture17 ├── Bubblesortusingrecursion ├── Bubblesortusingrecursion.cpp ├── Multiply ├── Multiply.cpp ├── Toh ├── Toh.cpp ├── binarysearchusingrecursion ├── binarysearchusingrecursion.cpp ├── check7 ├── check7.cpp ├── problem2048 ├── problem2048.cpp ├── stringtoint └── stringtoint.cpp ├── Lecture2 ├── checkingasciivalues ├── checkingasciivalues.cpp ├── fertocel ├── fertocel.cpp ├── islowerorupper ├── islowerorupper.cpp ├── minmaxmean ├── minmaxmean.cpp ├── pattern232 ├── pattern232.cpp ├── returnstatement ├── returnstatement.cpp ├── test ├── test.cpp └── untitled ├── Lecture5 ├── breakstatement ├── breakstatement.cpp ├── continuestatement ├── continuestatement.cpp ├── dowhile ├── dowhile.cpp ├── printprimesbw2ton ├── printprimesbw2ton.cpp ├── scopeofavariable ├── scopeofavariable.cpp ├── squareroot ├── squareroot.cpp ├── squarerootwithprecision └── squarerootwithprecision.cpp ├── QpsRecursion ├── .DS_Store ├── dictionaryorderlarger ├── dictionaryorderlarger.cpp ├── dictionarysmallerorder ├── dictionarysmallerorder.cpp ├── nstairs ├── nstairs.cpp ├── ratchasesitscheese ├── ratchasesitscheese.cpp ├── smartkeypad └── smartkeypad.cpp ├── doubtclass2 ├── aggressivecows ├── aggressivecows.cpp ├── cbscholarship ├── cbscholarship.cpp ├── classAssignment ├── classAssignment.cpp ├── lowerbound ├── lowerbound.cpp ├── optimalgamestretegy ├── optimalgamestretegy.cpp ├── recursionkeypadcodes └── recursionkeypadcodes.cpp ├── lecture10 ├── callbyreference ├── callbyreference.cpp ├── callbyvalue ├── callbyvalue.cpp ├── inbuildsort ├── inbuildsort.cpp ├── mergesortedarrays ├── mergesortedarrays.cpp ├── passingarraysintofunctions ├── passingarraysintofunctions.cpp ├── selectionsortusingfunctions ├── selectionsortusingfunctions.cpp ├── sumofelementsofarray └── sumofelementsofarray.cpp ├── lecture11 ├── pointers ├── pointers.cpp ├── sortcomperator ├── sortcomperator.cpp ├── switchcase ├── switchcase.cpp ├── tertiaryoperator └── tertiaryoperator.cpp ├── lecture13 ├── defaultarguments ├── defaultarguments.cpp ├── initializationofchararray ├── initializationofchararray.cpp ├── lengthofstrings ├── lengthofstrings.cpp ├── palindromestring ├── palindromestring.cpp ├── reverseastring ├── reverseastring.cpp ├── stringandno └── stringandno.cpp ├── lecture14 ├── charactertwoDarray ├── charactertwoDarray.cpp ├── findingelementinanarray ├── findingelementinanarray.cpp ├── spiralprint ├── spiralprint.cpp ├── twoDarray ├── twoDarray.cpp ├── waveprint └── waveprint.cpp ├── lecture15 ├── Bookallocationproblem ├── Bookallocationproblem.cpp ├── binarysearch ├── binarysearch.cpp ├── strings ├── strings.cpp ├── stringtokenizer └── stringtokenizer.cpp ├── lecture16 ├── Fibonacci ├── Fibonacci.cpp ├── factorial ├── factorial.cpp ├── incdecprinting ├── incdecprinting.cpp ├── isarraysortedornot ├── isarraysortedornot.cpp ├── power ├── power.cpp ├── sumofarrayelements └── sumofarrayelements.cpp ├── lecture18 ├── mergesort ├── mergesort.cpp ├── subsequence └── subsequence.cpp ├── lecture19 ├── moveallxatend ├── moveallxatend.cpp ├── permutation ├── permutation.cpp ├── ratinamaze └── ratinamaze.cpp ├── lecture20 ├── a.out ├── nqueen ├── nqueen.cpp ├── sudokusolver └── sudokusolver.cpp ├── lecture21 ├── Dma1d ├── Dma1d.cpp ├── pointerfunctions ├── pointerfunctions.cpp ├── twodarray └── twodarray.cpp ├── lecture22 ├── .DS_Store ├── oops ├── oops.cpp └── test ├── lecture23 ├── .DS_Store ├── pair ├── pair.cpp ├── vectors ├── vectors.cpp └── vectorsimplementation.cpp ├── lecture24 ├── vectorimplementation └── vectorimplementation.cpp ├── lecture25 ├── linkedlist └── linkedlist.cpp ├── lecture26 ├── linkedlist └── linkedlist.cpp ├── lecture27 ├── linkedlist └── linkedlist.cpp ├── lecture28 ├── generalstack ├── generalstack.cpp ├── inbuiltstack ├── inbuiltstack.cpp ├── isbalancedornot ├── isbalancedornot.cpp ├── node.h ├── reverseastack ├── reverseastack.cpp ├── stack.h ├── stackimplementation ├── stackimplementation.cpp ├── stackusinglinkedlist ├── stackusinglinkedlist.cpp ├── userdefinedstack └── userdefinedstack.cpp ├── lecture29 ├── inbuiltqueue ├── inbuiltqueue.cpp ├── queuesusingarrays ├── queuesusingarrays.cpp ├── queuesusingll ├── queuesusingll.cpp ├── quicksort ├── quicksort.cpp ├── stackusingtwoqueue └── stackusingtwoqueue.cpp ├── lecture3 ├── boolvalue ├── boolvalue.cpp ├── incrementoperator ├── incrementoperator.cpp ├── ispartoffibo ├── ispartoffibo.cpp ├── pattern ├── pattern.cpp ├── sizeofoperator ├── sizeofoperator.cpp ├── typeconversion └── typeconversion.cpp ├── lecture30 ├── trees └── trees.cpp ├── lecture31 ├── test └── test.cpp ├── lecture32 ├── binarytree ├── binarytree.cpp ├── bst └── bst.cpp ├── lecture33 ├── test └── test.cpp ├── lecture34 ├── inbuildpriorityqueue ├── inbuildpriorityqueue.cpp ├── test ├── test.cpp ├── top3elements └── top3elements.cpp ├── lecture35 ├── test └── test.cpp ├── lecture36 ├── dynamicprogrammingfibo ├── dynamicprogrammingfibo.cpp ├── hashmapcontinue ├── hashmapcontinue.cpp ├── inbuildhashmap └── inbuildhashmap.cpp ├── lecture37 ├── mindenominations ├── mindenominations.cpp ├── minstepstoone ├── minstepstoone.cpp └── test.cpp ├── lecture38 ├── mincostpath ├── mincostpath.cpp ├── mindenominations.cpp ├── test.cpp ├── wineproblem └── wineproblem.cpp ├── lecture39 ├── lcs ├── lcs.cpp ├── nstaircase ├── nstaircase.cpp ├── test └── test.cpp ├── lecture4 ├── bitwiseoperators ├── bitwiseoperators.cpp ├── constants ├── constants.cpp ├── doubt ├── doubt.cpp ├── fertocelforloop ├── fertocelforloop.cpp ├── forloop ├── forloop.cpp ├── incdec ├── incdec.cpp ├── printevennos └── printevennos.cpp ├── lecture40 ├── editdistance ├── editdistance.cpp ├── lis ├── lis.cpp └── test.cpp ├── lecture41 ├── graphs ├── graphs.cpp ├── knapsackproblem ├── knapsackproblem.cpp ├── linkedliststl ├── linkedliststl.cpp └── test.cpp ├── lecture42 ├── test └── test.cpp ├── lecture43 ├── graphdfs ├── graphdfs.cpp ├── weightedgraph └── weightedgraph.cpp ├── lecture44 ├── test └── test.cpp ├── lecture6 ├── arrays ├── arrays.cpp ├── counttill$ ├── counttill$.cpp ├── directions ├── directions.cpp ├── patternABCDEEDCBA ├── patternABCDEEDCBA.cpp ├── specialcharacters └── specialcharacters.cpp ├── lecture7 ├── bubblesort ├── bubblesort.cpp ├── findlargestinarray ├── findlargestinarray.cpp ├── linearsearch ├── linearsearch.cpp ├── optimizedbubblesort ├── optimizedbubblesort.cpp ├── selectionsort └── selectionsort.cpp ├── lecture8 ├── functions.cpp ├── functionshello ├── functionshello.cpp ├── insertionsort ├── insertionsort.cpp ├── kadenesalgo ├── kadenesalgo.cpp ├── maximumsubarraysum └── maximumsubarraysum.cpp └── lecture9 ├── .DS_Store ├── fertocelusingfunctions ├── fertocelusingfunctions.cpp ├── functionfactorial ├── functionfactorial.cpp ├── functions ├── functions.cpp ├── intresturntype ├── intresturntype.cpp ├── isevenorodd ├── isevenorodd.cpp ├── isprimeornot ├── isprimeornot.cpp ├── ncrfunction ├── ncrfunction.cpp ├── printprimes2ton ├── printprimes2ton.cpp ├── sortcomperator.cpp ├── sumoftwonumbers ├── sumoftwonumbers.cpp └── test /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/.DS_Store -------------------------------------------------------------------------------- /Doubtclass/alternatingbits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool alternatingbits(int n){ //1010 4 | n=n^(n>>1); //0101 n--->1111; 5 | bool ans=n&(n+1); 6 | if(ans==0){ 7 | return true; 8 | 9 | } 10 | return false; 11 | 12 | } 13 | 14 | 15 | int main(){ 16 | int n; 17 | cin>>n; 18 | bool result=alternatingbits(n); 19 | if(result==true){ 20 | cout<<"True"; 21 | } 22 | else{ 23 | cout<<"False"; 24 | } 25 | 26 | 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Doubtclass/arraytargetsumtriplet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/Doubtclass/arraytargetsumtriplet -------------------------------------------------------------------------------- /Doubtclass/arraytargetsumtriplet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int arr[1005]; 9 | // 5 7 9 1 2 4 6 8 3 10 | for (int i = 0; i < n; ++i) 11 | { 12 | cin>>arr[i]; 13 | /* code */ 14 | } 15 | int target; 16 | cin>>target; 17 | sort(arr,arr+n); 18 | // 1 2 3 4 5 6 7 8 9 19 | for(int i=0;i<=n-3;i++){ 20 | int left=i+1; 21 | int right=n-1; 22 | while(lefttarget){ 29 | right--; 30 | } 31 | else{ 32 | left++; 33 | } 34 | 35 | } 36 | } 37 | 38 | 39 | 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Doubtclass/formbiggestno: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/Doubtclass/formbiggestno -------------------------------------------------------------------------------- /Doubtclass/formbiggestno.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int countdigits(int n){ 6 | int count=0; 7 | while (n>0) 8 | { 9 | count++; 10 | n=n/10; 11 | /* code */ 12 | } 13 | 14 | return count; 15 | 16 | } 17 | int main(){ 18 | int t,n,arr[200]={0}; 19 | cin>>t; 20 | while(t--){ 21 | 22 | cin>>n; 23 | 24 | for (int i = 0; i >arr[i]; 27 | /* code */ 28 | } 29 | 30 | for (int i = 0; i <=n-2; i++) 31 | { 32 | 33 | for (int j = 0; j <=n-2-i; j++) 34 | { 35 | 36 | int fn=arr[j]; 37 | int sn=arr[j+1]; 38 | 39 | int fd=countdigits(fn); 40 | int sd=countdigits(sn); 41 | 42 | long long int no1=fn*pow(10,sd)+sn; 43 | long long int no2=sn*pow(10,fd)+fn; 44 | 45 | if(no1>no2){ 46 | int temp=arr[j]; 47 | arr[j]=arr[j+1]; 48 | arr[j+1]=temp; 49 | 50 | } 51 | /* code */ 52 | } 53 | 54 | /* code */ 55 | } 56 | 57 | for(int i=n-1;i>=0;i--){ 58 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | int n,i; 7 | cin>>n; 8 | int arr[1005]; 9 | for (i = 0; i < n; ++i) 10 | { 11 | cin>>arr[i]; //1 3 5 7 9 12 | 13 | } 14 | // i--->5 15 | for( ;i<2*n;i++){ 16 | cin>>arr[i]; //2 4 6 8 10 17 | } 18 | sort(arr,arr+2*n); //1 2 3 4 5 6 7 8 9 10 19 | 20 | cout< 2 | using namespace std; 3 | 4 | bool compare(string s1,string s2){ 5 | 6 | if(s1.length()>=s2.length()&&(s1.substr(0,s2.length())==s2)){ 7 | return true; 8 | //s1-->rainbow s2-->rain 4 9 | } 10 | else{ 11 | return s1>n; 20 | // string s; 21 | // cin>>s; 22 | // string x; 23 | // cin>>x; 24 | string arr[1005]; 25 | for (int i = 0; i < n; ++i) 26 | { 27 | cin>>arr[i]; 28 | /* code */ 29 | } 30 | sort(arr,arr+n,compare); 31 | for (int i = 0; i < n; ++i) 32 | { 33 | cout< //hederfile 2 | using namespace std; // we are using standard namespace 3 | int main(){ 4 | cout<<"Hello world i am new to CB"<<'\n'; //semicolon is must 5 | cout<<"i am 4th year Student"<<'\n'; 6 | 7 | cout<<200<<'\n'; 8 | cout<<"Coding Blocks"< //hederfile 2 | using namespace std; // we are using standard namespace 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int i=2; //initialization 7 | 8 | while (i<=n-1) //condition 9 | { 10 | 11 | if(n%i==0){ 12 | cout<<"not prime"< //hederfile 2 | using namespace std; // we are using standard namespace 3 | int main(){ 4 | 5 | int a; 6 | cin>>a; 7 | if(a>=50&&a<=60){ 8 | cout << "a>=50&&a<=60"<=60&&a<=70) 12 | { 13 | cout << "a>=60&&a<=70"<=60&&a<=100){ 17 | cout << ">=60||a<=100"< //hederfile 2 | using namespace std; // we are using standard namespace 3 | int main(){ 4 | int a,b,c; 5 | cin>>a>>b>>c; 6 | if(a>=b&&a>=c){ 7 | cout << "a is largest with value:"<=a&&b>=c){ 10 | cout << "b is largest with value:"< //hederfile 2 | using namespace std; // we are using standard namespace 3 | int main(){ 4 | 5 | int p,r,t; 6 | // int p; 7 | // int r; 8 | // int t; 9 | 10 | // cin>>p; 11 | // cin>>r; 12 | // cin>>t; 13 | cin>>p>>r>>t; 14 | 15 | float si; 16 | si=p*r*t/100; 17 | 18 | cout << si< //hederfile 2 | using namespace std; // we are using standard namespace 3 | int main(){ 4 | int a=10,b=60; //initialization 5 | cout<>n; //to take input 15 | cout << " value of n is :" < //hederfile 2 | using namespace std; // we are using standard namespace 3 | int main(){ 4 | // cout << 1< 2 | using namespace std; 3 | int main(){ 4 | int n,i,j; 5 | cin>>n; 6 | int arr[100]; 7 | for(int i=0;i>arr[i]; 9 | } 10 | cout<<"array Before insertion sort:"<=0&&arr[j]>current;j--){ 23 | arr[j+1]=arr[j]; 24 | } 25 | arr[j+1]=current; 26 | } 27 | //after insertion sort 28 | 29 | cout<<"array after insertion sort:"< 2 | using namespace std; 3 | #define ll long long int 4 | 5 | ll a[1000005],prefix[1000005]; 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int n; 11 | cin>>n; 12 | for(int i=0;i<1000005;i++){ 13 | prefix[i]=0; 14 | 15 | } 16 | ll sum=0; 17 | prefix[0]=1; 18 | for(int i=0;i>a[i]; 20 | sum+=a[i]; 21 | sum=sum%n; 22 | sum=(sum+n)%n; //negative values 23 | prefix[sum]++; 24 | 25 | } 26 | ll ans=0; 27 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int radius=10; 5 | // int *rptr=&radius; //initilization 6 | 7 | // int *rptr; 8 | 9 | int *rptr=NULL; //pointer point to the 0 address memory location 10 | 11 | // int *rptr=0; 12 | 13 | 14 | cout<<"Circumference of circle is :"; 15 | // cout<<2*3.14*(radius)< 2 | using namespace std; 3 | 4 | int main(){ 5 | int a[]={1,2,3,4,5}; 6 | cout<<"Address of a is"< 2 | using namespace std; 3 | void update(int *z){ 4 | 5 | *z=*z+2; 6 | 7 | 8 | } 9 | int main(){ 10 | int x=10; 11 | int* y=&x; 12 | cout<<"Value of x is :"< 2 | #include 3 | using namespace std; 4 | 5 | 6 | void selectionsort(int a[],int n){ 7 | for(int i=0;i<=n-2;i++){ 8 | int min=i; 9 | 10 | for(int j=i+1;j<=n-1;j++){ 11 | if(*(a+min)>*(a+j)){ 12 | min=j; 13 | } 14 | 15 | } 16 | swap(*(a+min),*(a+i)); 17 | } 18 | 19 | 20 | } 21 | int main(){ 22 | int arr[100]; 23 | cout<<"enter the number of elements:"<>n; 28 | for(int i=0;i>arr[i]; 30 | } 31 | cout<<"Array Before sorting"< 2 | using namespace std; 3 | 4 | void bubblesortrecursion(int*arr,int n,int i){ 5 | // base case 6 | if(i==n-1){ 7 | return; 8 | } 9 | 10 | 11 | // recursive case 12 | for(int j=0;jarr[j+1]){ 14 | swap(arr[j],arr[j+1]); 15 | 16 | 17 | } 18 | } 19 | 20 | bubblesortrecursion(arr,n,i+1); 21 | } 22 | 23 | void bubblesortrecursion2(int *arr,int n,int i,int j){ 24 | 25 | // Base case 26 | 27 | if(i==n-1){ 28 | return; 29 | } 30 | if(j==n-1-i){ 31 | bubblesortrecursion2(arr,n,i+1,0); 32 | 33 | 34 | } 35 | else{ 36 | if(arr[j]>arr[j+1]){ 37 | swap(arr[j],arr[j+1]); 38 | 39 | 40 | } 41 | bubblesortrecursion2(arr,n,i,j+1); 42 | 43 | } 44 | 45 | 46 | } 47 | 48 | 49 | 50 | 51 | int main(){ 52 | 53 | int arr[100]; 54 | cout<<"enter the number of elements:"<>n; 59 | for(int i=0;i>arr[i]; 61 | } 62 | 63 | // bubblesortrecursion(arr,n,0); 64 | bubblesortrecursion2(arr,n,0,0); 65 | 66 | for(int i=0;i 2 | using namespace std; 3 | int multiply(int a,int b){ 4 | 5 | // base case 6 | if(b==0){ 7 | return 0; 8 | } 9 | 10 | 11 | // if(b-1==0){ 12 | // return a; 13 | // } 14 | // recursive case 15 | // int chotaans=multiply(a,b-1); 16 | // int badaans=a+chotaans; 17 | // return badaans; 18 | 19 | return a+multiply(a,b-1); 20 | } 21 | 22 | int main(){ 23 | int a,b; 24 | cin>>a>>b; 25 | // int x=multiply(a,b) 26 | 27 | cout< 2 | using namespace std; 3 | 4 | void toh(int n,char src,char helper,char dest){ 5 | // base case 6 | if(n==0){ 7 | return; 8 | } 9 | 10 | 11 | // Recursive case 12 | 13 | toh(n-1,src,dest,helper); 14 | cout<<"Take "<>n; 23 | toh(n,'A','B','C'); 24 | 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Lecture17/binarysearchusingrecursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/Lecture17/binarysearchusingrecursion -------------------------------------------------------------------------------- /Lecture17/binarysearchusingrecursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarysearchrecursion(int *arr,int key,int s,int e){ 5 | // base case 6 | if(s>e){ 7 | return -1; 8 | 9 | } 10 | // recursive case 11 | 12 | int mid=(s+e)/2; 13 | if(arr[mid]>key){ 14 | return binarysearchrecursion(arr,key,s,mid-1); 15 | 16 | } 17 | else if(arr[mid]==key){ 18 | return mid; 19 | } 20 | else{ 21 | return binarysearchrecursion(arr,key,mid+1,e); 22 | 23 | } 24 | 25 | 26 | 27 | } 28 | 29 | 30 | 31 | int main(){ 32 | 33 | int arr[100]; 34 | cout<<"enter the number of elements:"<>n; 39 | for(int i=0;i>arr[i]; 41 | } 42 | int key; 43 | cin>>key; 44 | cout< 2 | using namespace std; 3 | 4 | bool is7present(int *arr,int n){ 5 | // base case 6 | if(n==0){ 7 | return false; 8 | } 9 | 10 | 11 | // Recursive case 12 | if(arr[0]==7){ 13 | return true; 14 | } 15 | return is7present(arr+1,n-1); 16 | } 17 | 18 | int first7(int *arr,int n,int i){ 19 | // base case 20 | if(i==n){ 21 | return -1; 22 | } 23 | 24 | 25 | // Recursive case 26 | 27 | if(arr[i]==7){ 28 | return i; 29 | } 30 | return first7(arr,n,i+1); 31 | 32 | } 33 | void allindices(int *arr,int n,int i){ 34 | // base case 35 | if(i==n){ 36 | return; 37 | } 38 | 39 | 40 | // Recursive case 41 | 42 | if(arr[i]==7){ 43 | cout< 2 | using namespace std; 3 | string s[]={"Zero","one","Two","Three","Four","Five","six","seven","eight","nine"}; 4 | 5 | // 2048 -->eight four zero two 6 | void inttostringreverse(int n){ // 7 | 8 | 9 | // base case 10 | if(n==0){ 11 | return; 12 | } 13 | // recursive case 14 | int digit=n%10; //8 15 | cout<two zero four eight 19 | void inttostring(int n){ // 20 | // Base case 21 | if(n==0){ 22 | return; 23 | } 24 | // Recursive case 25 | int digit=n%10; //8 26 | 27 | inttostring(n/10); 28 | cout<>n; 36 | 37 | 38 | inttostringreverse(n); 39 | cout< 2 | using namespace std; 3 | 4 | int stringtoint(char *a,int n){ 5 | // base case 6 | if(n==0){ 7 | return 0; 8 | } 9 | 10 | 11 | // recursive case 12 | int digit=a[n-1]-'0'; 13 | return stringtoint(a,n-1)*10+digit; 14 | } 15 | 16 | 17 | 18 | int main(){ 19 | 20 | char arr[100]; 21 | cin>>arr; //"54123" -->o/p 54123+100 22 | int l=strlen(arr); //length -->5 23 | int x=stringtoint(arr,l); 24 | int y=x+100; 25 | 26 | cout< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | 6 | char ch=' '; 7 | int x=ch; 8 | cout<<"value of x is : "< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int ival,fval,step; 6 | cin>>ival>>fval>>step; 7 | 8 | 9 | int f=ival; 10 | 11 | while(f<=fval){ 12 | 13 | 14 | int cel=(5/9.0)*(f-32); 15 | 16 | // int cel=((5)*(f-32))/9; 17 | 18 | cout< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | 6 | char ch; 7 | cin>>ch; 8 | // int x=ch; 9 | 10 | // 1 sol 11 | 12 | 13 | 14 | // if(x>=65&&x<=90){ 15 | // cout<<"uppercase"<=97&&x<=122){ 19 | // cout<<"lowercase"<='A'&&x<='Z'){ 29 | // cout<<"uppercase"<='a'&&x<='z'){ 33 | // cout<<"lowercase"<='A'&&ch<='Z'){ 43 | cout<<"uppercase"<='a'&&ch<='z'){ 47 | cout<<"lowercase"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n,min=INT_MAX,max=INT_MIN; 6 | int sum=0; 7 | cin>>n; 8 | int i=1; //initialization 9 | 10 | while(i<=n){ //condition check 11 | 12 | int no; 13 | cin>>no; 14 | //minimum 15 | 16 | if(nomax){ 20 | max=no; 21 | } 22 | sum=sum+no; 23 | i=i+1; //updation 24 | } 25 | float mean=sum/n*1.0; 26 | cout<<"minimum value among n numbers is :"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int rowno=1; 8 | while(rowno<=n){ 9 | 10 | 11 | //spaces print 12 | 13 | int col=1; 14 | int spaces=n-rowno; 15 | while(col<=spaces){ 16 | cout<<" "<<'\t'; 17 | col=col+1; 18 | } 19 | 20 | //nos in increasing order 21 | 22 | 23 | int i=1; 24 | int val=rowno; 25 | while(i<=rowno){ 26 | cout< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | 6 | 7 | cout<<"this is a pen"< //hederfile 23 | // using namespace std; // we are using standard namespace 24 | // int main(){ 25 | // int n; 26 | // cin>>n; 27 | // int i=2; //initialization 28 | 29 | // while (i<=n-1) //condition 30 | // { 31 | 32 | // if(n%i==0){ 33 | // cout<<"not prime"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int rowno=1; 8 | while(rowno<=n){ 9 | 10 | 11 | //spaces print 12 | 13 | int col=1; 14 | int spaces=n-rowno; 15 | while(col<=spaces){ 16 | cout<<" "; 17 | col=col+1; 18 | } 19 | 20 | //nos in increasing order 21 | 22 | 23 | int i=1; 24 | int val=rowno; 25 | while(i<=rowno){ 26 | cout< 2 | using namespace std; 3 | int main(){ 4 | 5 | for(int i=0;i<=10;i++){ 6 | 7 | cout<<"value of i is : "< 2 | using namespace std; 3 | int main(){ 4 | 5 | for(int i=0;i<=10;i++){ 6 | //control comes to i++ i.e; loop ; 7 | 8 | 9 | // cout<<"value of i is : "< 2 | using namespace std; 3 | int main(){ 4 | 5 | 6 | // int i=10; 7 | 8 | // // ek baar loop chalega hi chalega //also k/as exit control loop 9 | // do{ 10 | // cout<<"value of i is :"<0); infinite loop 16 | 17 | int i=10; 18 | 19 | // ek baar loop chalega hi chalega //also k/as exit control loop 20 | do{ 21 | cout<<"value of i is :"< 2 | using namespace std; 3 | int main(){ 4 | 5 | 6 | int n,no; 7 | cin>>no; 8 | 9 | 10 | for(int n=2;n<=no;n++){ 11 | 12 | 13 | int i=2; 14 | while(i<=n-1){ 15 | if(n%i==0){ 16 | // cout<<"not prime"< 2 | using namespace std; 3 | int y=40; //global variable 4 | int main(){ 5 | int y=60; 6 | 7 | // int x=10; 8 | // cout<<"value of x is : "<0){ 11 | // int x=30; 12 | // int y=20; 13 | // cout<<"value of x is : "<0){ 27 | // int y=20; 28 | // cout<<"value of x is : "<0){ 43 | int y=20; 44 | cout<<"value of x is : "< 2 | using namespace std; 3 | int main(){ 4 | 5 | int no; 6 | cin>>no; 7 | int i=0; 8 | while(i*i<=no){ 9 | i++; 10 | } 11 | cout< 2 | using namespace std; 3 | int main(){ 4 | 5 | int no,precision; 6 | cin>>no; 7 | cin>>precision; 8 | 9 | float i=0; 10 | float inc=1; 11 | 12 | for(int j=0;j<=precision;j++){ 13 | 14 | while(i*i<=no){ 15 | i=i+inc; 16 | } 17 | i=i-inc; 18 | inc=inc/10; 19 | 20 | 21 | 22 | 23 | 24 | } 25 | 26 | cout< 2 | #include 3 | using namespace std; 4 | string s1; 5 | void dictionaryorder(string in,string out){ 6 | // Base Case 7 | if(in.length()==0){ 8 | if(out>s1){ 9 | cout<>s; 29 | s1=s; 30 | sort(s.begin(),s.end()); //s-->abc 31 | // cout< 2 | #include 3 | using namespace std; 4 | string s1; 5 | void dictionaryorder(string in,string out){ 6 | // Base Case 7 | if(in.length()==0){ 8 | if(out>s; 29 | s1=s; 30 | sort(s.begin(),s.end()); //s-->abc 31 | // cout< 2 | using namespace std; 3 | int Nstairs(int n){ //k 3 4 | // Base Case 5 | if(n==0){ 6 | return 1; 7 | } 8 | if(n<0){ 9 | return 0; 10 | } 11 | // recursive case 12 | return Nstairs(n-1)+Nstairs(n-2)+Nstairs(n-3); 13 | 14 | 15 | 16 | 17 | } 18 | int solvenstairs(int n,int k){ 19 | if(n==0){ 20 | return 1; 21 | } 22 | if(n<0){ 23 | return 0; 24 | } 25 | int ans=0; 26 | for(int i=1;i<=k;i++){ 27 | ans+=solvenstairs(n-i,k); 28 | } 29 | return ans; 30 | 31 | } 32 | int main(){ 33 | int n; 34 | cin>>n; 35 | cout<>k; //3 38 | cout< 2 | using namespace std; 3 | int sol[1001][1001]; 4 | bool visited[1001][1001]; 5 | bool ratinamaze(char maze[][1001],int i,int j,int m,int n){ 6 | // base case 7 | if(i==m-1&&j==n-1){ 8 | sol[i][j]=1; 9 | // printing 10 | for(int k=0;k=0&&!visited[i][j-1]){ 51 | bool kyaleftsepathexistkartahai=ratinamaze(maze,i,j-1,m,n); 52 | if(kyaleftsepathexistkartahai==true){ 53 | return true; 54 | } 55 | 56 | } 57 | // // //upwards 58 | if((i-1>=0)&&!visited[i-1][j]){ 59 | bool kyauperpathexistkartahai=ratinamaze(maze,i-1,j,m,n); 60 | if(kyauperpathexistkartahai==true){ 61 | return true; 62 | } 63 | } 64 | 65 | sol[i][j]=0; 66 | return false; 67 | 68 | 69 | 70 | 71 | } 72 | 73 | 74 | int main(){ 75 | char maze[1001][1001]; 76 | int n,m; 77 | cin>>m>>n; //m-->rows, n-->cols 78 | for(int i=0;i>maze[i][j]; 83 | if(maze[i][j]=='X') 84 | visited[i][j]=1; 85 | } 86 | } 87 | bool ans=ratinamaze(maze,0,0,m,n); 88 | if(ans==false){ 89 | cout<<"NO PATH FOUND"< 2 | using namespace std; 3 | char keys[10][10]={" "," ","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"}; 4 | 5 | void phonekeypad(char inp[],int i,char out[],int j){ 6 | // Base case 7 | if(inp[i]=='\0'){ 8 | out[j]='\0'; 9 | cout<2 extract 16 | int digit=inp[i]-'0';//'2' abc 17 | // 3->DEF\0 18 | for(int k=0;keys[digit][k]!='\0';k++){ 19 | out[j]=keys[digit][k]; 20 | phonekeypad(inp,i+1,out,j+1); 21 | 22 | 23 | 24 | 25 | 26 | } 27 | } 28 | int main(){ 29 | char inp[100]; 30 | cin>>inp; 31 | char out[100]; 32 | phonekeypad(inp,0,out,0); 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /doubtclass2/aggressivecows: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/doubtclass2/aggressivecows -------------------------------------------------------------------------------- /doubtclass2/aggressivecows.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool amiabletoplaceatcowsdistanceofmid(int *arr,int n,int dis,int tc){ 4 | 5 | 6 | int cp=1; //cows placed 7 | int prev=arr[0]; //pehli cow at prev 8 | 9 | for(int i=1;i=dis){ 11 | cp++; 12 | prev=arr[i]; 13 | if(tc==cp){ 14 | return true; 15 | } 16 | 17 | } 18 | 19 | } 20 | if(cptotal stalls, c-->total cows 31 | 32 | cin>>n>>c; 33 | int arr[100000]; 34 | for (int i = 0; i < n; ++i) 35 | { 36 | cin>>arr[i]; 37 | /* code */ 38 | } 39 | sort(arr,arr+n); 40 | int si=0; 41 | int ei=arr[n-1]; 42 | int ans=0; 43 | while(si<=ei){ 44 | int mid=(si+ei)/2; 45 | if(amiabletoplaceatcowsdistanceofmid(arr,n,mid,c)){ 46 | ans=mid; 47 | si=mid+1; 48 | 49 | } 50 | else { 51 | ei=mid-1; 52 | } 53 | 54 | 55 | } 56 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | ll n,m,x,y; //n->total students ,m-> no of copons ,x->copons required to get 100% sch 5 | bool kyamaimidnoofstudentskoscholarshipdepaarahi(int ans){ 6 | return (ans*x<=m+(n-ans)*y); 7 | 8 | } 9 | 10 | int main(){ 11 | 12 | cin>>n>>m>>x>>y;//y-->copons return if you don't perform well 13 | 14 | int si=0; 15 | int ei=n; 16 | int ans=0; 17 | while(si<=ei){ 18 | int mid=(si+ei)/2; 19 | if(kyamaimidnoofstudentskoscholarshipdepaarahi(mid)){ 20 | ans=mid; 21 | si=mid+1; 22 | 23 | } 24 | else{ 25 | ei=mid-1; 26 | } 27 | 28 | } 29 | cout< 2 | using namespace std; 3 | int findno(int n){ 4 | // base case 5 | if(n==1||n==2){ 6 | return n+1; 7 | } 8 | 9 | // recursive case 10 | return findno(n-1)+findno(n-2); 11 | } 12 | int main(){ 13 | int t; 14 | cin>>t; 15 | int k=1; 16 | while(t--){ 17 | int n; 18 | cin>>n; 19 | int x=findno(n); 20 | 21 | 22 | cout<<"#"< 2 | using namespace std; 3 | int upperbound(int key,int *arr,int n){ 4 | int si=0; 5 | int ei=n-1; 6 | int ans=-1; 7 | while(si<=ei){ 8 | int mid=(si+ei)/2; 9 | if(arr[mid]==key){ 10 | ans=mid; 11 | si=mid+1; 12 | } 13 | else if(arr[mid]>key){ 14 | ei=mid-1; 15 | } 16 | else{ 17 | si=mid+1; 18 | } 19 | 20 | } 21 | return ans; 22 | } 23 | 24 | int lowerbound(int key,int *arr,int n){ 25 | int si=0; 26 | int ei=n-1; 27 | int ans=-1; 28 | while(si<=ei){ 29 | int mid=(si+ei)/2; 30 | if(arr[mid]==key){ 31 | ans=mid; 32 | ei=mid-1; 33 | } 34 | else if(arr[mid]>key){ 35 | ei=mid-1; 36 | } 37 | else{ 38 | si=mid+1; 39 | } 40 | 41 | } 42 | return ans; 43 | } 44 | int main(){ 45 | int n; 46 | cin>>n; 47 | int arr[100000]; 48 | for (int i = 0; i < n; ++i) 49 | { 50 | cin>>arr[i]; 51 | /* code */ 52 | } 53 | int t; 54 | cin>>t; 55 | while(t--){ 56 | int key; 57 | cin>>key; 58 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | int coins[40]; 5 | ll optimalgamestretegy(int i,int j){ 6 | // Base case 7 | if(i>j){ 8 | return 0; 9 | } 10 | 11 | // Recursive case 12 | 13 | // case 1 //piyush 14 | // 4 1 3 2 //number->index 4->0 1->1 3->2 2->3 15 | ll firstpick=coins[i]+min(optimalgamestretegy(i+2,j),optimalgamestretegy(i+1,j-1)); 16 | 17 | 18 | // case 2 19 | // 1 2 3 20 | ll lastpick=coins[j]+min(optimalgamestretegy(i,j-2),optimalgamestretegy(i+1,j-1)); 21 | 22 | return max(firstpick,lastpick); 23 | } 24 | 25 | int main(){ 26 | int n; 27 | cin>>n; 28 | for (int i = 0; i < n; ++i) 29 | { 30 | cin>>coins[i]; 31 | 32 | } 33 | cout< 2 | using namespace std; 3 | char keys[10][10]={" ","abc","def","ghi","jkl","mno","pqrs","tuv","wx","yz"}; 4 | int cou=0; 5 | void phonekeypad(char inp[],int i,char out[],int j){ 6 | // Base case 7 | if(inp[i]=='\0'){ 8 | out[j]='\0'; 9 | cout<2 extract 18 | int digit=inp[i]-'0';//'2' abc 19 | // 3->DEF\0 20 | for(int k=0;keys[digit][k]!='\0';k++){ 21 | out[j]=keys[digit][k]; 22 | phonekeypad(inp,i+1,out,j+1); 23 | 24 | 25 | 26 | 27 | 28 | } 29 | } 30 | int main(){ 31 | char inp[100]; 32 | cin>>inp; 33 | char out[100]; 34 | phonekeypad(inp,0,out,0); 35 | cout< 2 | using namespace std; 3 | 4 | void update(int &a){ 5 | int &z=a; 6 | a=a+1; //update 7 | cout<<"value of a inside update is "< 2 | using namespace std; 3 | 4 | void update(int a){ 5 | a=a+1; //update 6 | cout<<"value of a inside update is "< 2 | #include 3 | using namespace std; 4 | 5 | void printarray(int arr[],int x){ 6 | cout<<"inside print function"< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int a[7]={6,6,7}; 7 | int b[4]={1,2,6,7}; 8 | int m = 3,n = 4; 9 | 10 | int i = m-1, j = n-1, k = m+n-1; 11 | 12 | while(i>=0 && j>=0){ 13 | if(a[i]=0){ 24 | a[k--] = b[j--]; 25 | } 26 | 27 | for(int i = 0 ; i < m+n ; i++){ 28 | cout< 2 | using namespace std; 3 | 4 | // void printarray(int a[],int x){ 5 | void printarray(int arr[10000],int x){ 6 | cout<<"inside print function"<>n; 35 | for(int i=0;i>arr[i]; 37 | 38 | } 39 | 40 | cout<<"array Before updation"< 2 | #include 3 | using namespace std; 4 | 5 | 6 | void selectionsort(int a[],int n){ 7 | for(int i=0;i<=n-2;i++){ 8 | int min=i; 9 | 10 | for(int j=i+1;j<=n-1;j++){ 11 | if(a[min]>a[j]){ 12 | min=j; 13 | } 14 | 15 | } 16 | swap(a[min],a[i]); 17 | } 18 | 19 | 20 | } 21 | int main(){ 22 | int arr[100]; 23 | cout<<"enter the number of elements:"<>n; 28 | for(int i=0;i>arr[i]; 30 | } 31 | cout<<"Array Before sorting"< 2 | using namespace std; 3 | 4 | 5 | int sumofarray(int a[],int n){ 6 | int sum=0; 7 | for(int i=0;i>n; 18 | for(int i=0;i>a[i]; 20 | 21 | } 22 | 23 | } 24 | 25 | 26 | int main(){ 27 | //input manually 28 | // int a[]={1,2,6,4,5,3,9,40}; 29 | // int n=sizeof(a)/sizeof(int); 30 | 31 | // int sum=0; 32 | // for(int i=0;i>n; 42 | 43 | 44 | // for(int i=0;i>a[i]; 46 | 47 | // } 48 | takeinput(a,n); 49 | 50 | cout<<"total sum is : "; 51 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int a=10; 7 | float f=10.14; 8 | double d=40.345; 9 | 10 | // cout<<"value of a is "< 2 | #include 3 | using namespace std; 4 | 5 | bool my(int a,int b){ 6 | 7 | 8 | cout<<"the value of a is "<b; //decreasing 12 | } 13 | int main(){ 14 | int a[]={4,2,67,83,24,12}; 15 | int n=sizeof(a)/sizeof(int); 16 | cout<<"before sorting : "; 17 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | char ch; 7 | cin>>ch; 8 | // if(ch=='A'){ 9 | // cout<<"value of a is "< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int age=10; 7 | if(age<8){ 8 | 9 | cout<<"age is less than 8"< 2 | using namespace std; 3 | 4 | int add(int a=0,int b=0,int c=0,int d=0){ 5 | return a+b+c+d; 6 | } 7 | int main() 8 | { 9 | 10 | 11 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | 6 | //initialization 7 | // char arr[]={'A','b','c','D','\0'}; 8 | // char arr1[]={'A','b','c','D','\0'}; 9 | // cout<>arr3; 22 | // cout<and returns it 28 | // int i=0; 29 | // while(ch!='\n'){ // \n is the by default delimiter 30 | // arr4[i]=ch; 31 | // i++; 32 | // ch=cin.get(); 33 | 34 | 35 | 36 | // } 37 | // cout< 2 | using namespace std; 3 | 4 | // int length(char a[]) 5 | int length(char *a){ 6 | int i=0; 7 | int co=0; 8 | while(a[i]!='\0'){ 9 | co++; 10 | i++; 11 | } 12 | 13 | return co; 14 | } 15 | int main() 16 | { 17 | char a[100]; 18 | // cin>>a; 19 | cin.getline(a,100); 20 | cout<<"length of array is "< 2 | using namespace std; 3 | 4 | // int length(char a[]) 5 | int length(char *a){ 6 | int i=0; 7 | int co=0; 8 | while(a[i]!='\0'){ 9 | co++; 10 | i++; 11 | } 12 | 13 | return co; 14 | } 15 | 16 | bool ispalinornot(char *a){ 17 | int i=0; 18 | int j=length(a)-1; 19 | while(i>a; 33 | // cin.getline(a,100); 34 | // cout<<"length of array is "< 2 | using namespace std; 3 | 4 | // int length(char a[]) 5 | int length(char a[]){ 6 | int i=0; 7 | int co=0; 8 | while(a[i]!='\0'){ 9 | co++; 10 | i++; 11 | } 12 | 13 | return co; 14 | } 15 | 16 | 17 | void reverse(char*a){ 18 | int i=0; 19 | int j=length(a)-1; 20 | while(i>a; 32 | cout<<"Before reversing "< 2 | using namespace std; 3 | int main() 4 | { 5 | char arr[100]; 6 | 7 | char ch; 8 | 9 | int n; 10 | cin>>n; 11 | cin.ignore(); //ignores 1 character 12 | // ch=cin.get(); 13 | 14 | cin.getline(arr,100); 15 | cout< 2 | using namespace std; 3 | int main() { 4 | char arr[][3]={ 5 | {'A','B','c'}, 6 | {'D','E','F'}, 7 | {'G','H','i'} 8 | }; 9 | 10 | for(int i=0;i<3;i++){ 11 | for(int j=0;j<3;j++){ 12 | cout< 2 | using namespace std; 3 | int main() { 4 | 5 | 6 | int arr[100][100]; 7 | int rows,cols; 8 | cin>>rows>>cols; 9 | for(int i=0;i>arr[i][j]; 12 | 13 | } 14 | // cout<>target; 28 | 29 | for(i=0;i 2 | using namespace std; 3 | 4 | void spiralprint(int arr[100][100],int rows,int cols){ 5 | int sr=0,sc=0,er=rows-1,ec=cols-1; 6 | 7 | 8 | while(sc<=ec&&sr<=er){ 9 | 10 | 11 | for(int i=sc;i<=ec;i++){ 12 | cout<=sc;i--){ 21 | cout<=sr;j--){ 31 | cout<>rows>>cols; 50 | for(int i=0;i>arr[i][j]; 53 | 54 | } 55 | // cout< 2 | using namespace std; 3 | int main() { 4 | 5 | //1st way 6 | // int arr[3][3]={ 7 | // {1,2,3}, 8 | // {4,5,6}, 9 | // {7,8,9} 10 | 11 | // }; 12 | //you can skip number of rows 13 | //2nd way 14 | // int arr[][3]={ 15 | // {1,2,3}, 16 | // {4,5,6}, 17 | // {7,8,9} 18 | 19 | // }; 20 | 21 | 22 | 23 | // int arr[][3]={ 24 | // {1,2,3}, 25 | // {4,5,6}, 26 | // {7,8} 27 | 28 | // }; 29 | 30 | // for(int row=0;row<3;row++){ 31 | // for(int col=0;col<3;col++){ 32 | // cout<>rows>>cols; 41 | 42 | for(int i=0;i 2 | using namespace std; 3 | int main() { 4 | 5 | 6 | int arr[100][100]; 7 | int rows,cols; 8 | cin>>rows>>cols; 9 | for(int i=0;i>arr[i][j]; 12 | 13 | } 14 | // cout<=0;j--){ 41 | cout< 2 | using namespace std; 3 | 4 | bool isvalid(int books[],int n,int k,int ans){ 5 | int student=1,i; 6 | int current=0; 7 | for(i=0;ians){ 9 | current=books[i]; 10 | student++; 11 | if(student>k){ 12 | return false; 13 | } 14 | 15 | } 16 | else{ 17 | current+=books[i]; 18 | } 19 | } 20 | return true; 21 | } 22 | 23 | int binarysearchbooks(int books[],int n,int k){ 24 | int total_pages=0; 25 | int s=0,e=0; 26 | for(int i=0;i>t; 51 | while(t--){ 52 | cin>>n>>k; 53 | int books[100005]; 54 | for(int i=0;i>books[i]; 56 | } 57 | cout<< binarysearchbooks(books,n,k)< 2 | #include 3 | using namespace std; 4 | int binarysearch(int *a,int n,int key){ 5 | 6 | int s=0; 7 | int e=n-1; 8 | while(s<=e){ 9 | int mid=(s+e)/2; 10 | if(a[mid]>key){ 11 | e=mid-1; 12 | } 13 | else if(a[mid]==key){ 14 | return mid; 15 | } 16 | else{ 17 | s=mid+1; 18 | } 19 | 20 | } 21 | return -1; 22 | } 23 | int main() { 24 | 25 | int n; 26 | cin>>n; 27 | int a[100]; 28 | for(int i=0;i>a[i]; 30 | } 31 | int key; 32 | cin>>key; 33 | int x=binarysearch(a,n,key); 34 | if(x==-1){ 35 | cout<<"key not found"< 2 | #include 3 | using namespace std; 4 | int main() { 5 | //STRING INITILIZATION AND DECLERATION 6 | string s; 7 | // cin>>s; 8 | getline(cin,s); 9 | 10 | cout<a2){ 35 | cout< 2 | #include 3 | using namespace std; 4 | int main() { 5 | char a[100]="1234...$$2434''.435...498787''%"; 6 | 7 | 8 | char *ptr=strtok(a,".'$%"); 9 | while(ptr!=NULL){ 10 | cout< 2 | using namespace std; 3 | 4 | int fibono(int n){ 5 | //base case 6 | 7 | if(n==0||n==1){ 8 | return n; 9 | 10 | } 11 | 12 | // Recursive case 13 | 14 | // int chotiproblem1=fibono(n-1); 15 | // int chotiproblem2=fibono(n-2); 16 | // int badiproblem=chotiproblem1+chotiproblem2; 17 | // return badiproblem; 18 | 19 | return fibono(n-1)+fibono(n-2); 20 | } 21 | int main(){ 22 | int n; 23 | cin>>n; 24 | int x=fibono(n); 25 | cout< 2 | using namespace std; 3 | 4 | int fact(int n){ 5 | //base case 6 | if(n==0){ 7 | return 1; 8 | } 9 | // recursive case 10 | 11 | int chotiproblem=fact(n-1); //pure assumption 12 | 13 | // //find ans with the help of above two 14 | 15 | int badiproblem=n*chotiproblem; 16 | 17 | return badiproblem; 18 | 19 | // return n*fact(n-1); //without storing 20 | } 21 | int main(){ 22 | 23 | int n; 24 | cin>>n; 25 | cout< 2 | using namespace std; 3 | 4 | void dec(int n){ 5 | 6 | //base case 7 | if(n==0){ 8 | return; 9 | 10 | } 11 | 12 | // if(n==1){ 13 | // cout<<1<>n; 50 | dec(n); //n---1 51 | 52 | cout<<"increment is : "< 2 | using namespace std; 3 | bool issortedornot(int *a,int n){ 4 | 5 | //base case 6 | if(n==1){ 7 | return true; 8 | } 9 | 10 | 11 | // Recursive case 12 | bool issor=issortedornot(a+1,n-1); 13 | if(a[0] 2 | using namespace std; 3 | int poweroftn(int x,int n){ 4 | // Base case 5 | if(n==0){ 6 | return 1; 7 | } 8 | 9 | 10 | // Recursive case 11 | return x*poweroftn(x,n-1); 12 | } 13 | int main() { 14 | int x,n; 15 | cin>>x>>n; 16 | cout< 2 | using namespace std; 3 | int sum(int *a,int n){ 4 | 5 | // base case 6 | if(n==0){ 7 | return 0; 8 | } 9 | 10 | // if(n==1){ 11 | // return a[0]; 12 | // } 13 | 14 | // Recursive case 15 | // int chotiproblem=sum(a+1,n-1);//pure assumption 16 | // int badiproblem=a[0]+chotiproblem; 17 | // return badiproblem; 18 | return a[0]+sum(a+1,n-1); 19 | } 20 | 21 | int sum2(int *a,int n,int i){ 22 | 23 | //base case 24 | if(i==n){ 25 | return 0; 26 | } 27 | 28 | 29 | //recursive case 30 | // int chotiproblem=sum(a,n,i+1); 31 | // int badiproblem=a[i]+chotiproblem; 32 | // return badiproblem; 33 | return a[i]+sum2(a,n,i+1); 34 | 35 | 36 | } 37 | int sum3(int *a,int n){ 38 | 39 | // Base Case 40 | if(n==0){ 41 | return 0; 42 | } 43 | 44 | // Recursive case 45 | return sum3(a,n-1)+a[n-1]; 46 | 47 | } 48 | 49 | int main(){ 50 | int a[]={1,2,3,4,5,6}; 51 | int n=sizeof(a)/sizeof(int); 52 | int x1=sum(a,n); 53 | cout<<"sum is "< 2 | using namespace std; 3 | #define ll long long int 4 | 5 | void mergetwosortedarray(ll *arr,ll s,ll e){ 6 | int mid=(s+e)/2; 7 | int i=s; 8 | int j=mid+1; 9 | ll *temp= new ll[2000000]; 10 | int k=s; 11 | while(i<=mid&&j<=e){ 12 | if(arr[i]=e){ 49 | return; 50 | } 51 | 52 | // recursive case 53 | int mid=(s+e)/2; 54 | mergesort(arr,s,mid); // 1 2 4 55 | mergesort(arr,mid+1,e); // 3 5 56 | mergetwosortedarray(arr,s,e); 57 | 58 | 59 | } 60 | int main(){ 61 | int n; 62 | cin>>n; 63 | // int arr[100000000]; 64 | // datatype *variable name=new datatype[size]; //dynamic memory allocation 65 | ll *arr=new ll[2000000]; 66 | for (int i = 0; i < n; ++i) 67 | { 68 | cin>>arr[i]; 69 | /* code */ 70 | } 71 | mergesort(arr,0,n-1); 72 | for (int i = 0; i < n; ++i) 73 | { 74 | cout< 2 | using namespace std; 3 | void subsequence(char in[],int i,char ou[],int j){ 4 | 5 | // Base case 6 | if(in[i]=='\0'){ 7 | ou[j]='\0'; 8 | cout<>input; 29 | 30 | subsequence(input,0,output,0); 31 | 32 | 33 | 34 | 35 | 36 | } -------------------------------------------------------------------------------- /lecture19/moveallxatend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/lecture19/moveallxatend -------------------------------------------------------------------------------- /lecture19/moveallxatend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | string stringmaixkopeechelejao(string s){ 4 | // base case 5 | if(s.length()==0){ 6 | string x=""; 7 | return x; 8 | } 9 | 10 | // recursive case 11 | // xabxxaccccb 12 | string a=s.substr(0,1); //pehla character lega 13 | string chotistring=stringmaixkopeechelejao(s.substr(1));//abxxaccccb-->abaccccbxx 14 | if(a=="x"){ 15 | string badisting=chotistring+a; 16 | return badisting; 17 | 18 | } 19 | else{ 20 | string badisting=a+chotistring; 21 | return badisting; 22 | 23 | } 24 | 25 | 26 | } 27 | 28 | int main(){ 29 | string s; 30 | cin>>s; 31 | 32 | string x=stringmaixkopeechelejao(s); 33 | cout<<"string after moving all x at end is : "< 2 | using namespace std; 3 | void permutation(char in[],int i){ 4 | // base case 5 | if(in[i]=='\0'){ 6 | cout<>in; 26 | 27 | permutation(in,0); 28 | 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /lecture19/ratinamaze: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/lecture19/ratinamaze -------------------------------------------------------------------------------- /lecture19/ratinamaze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sol[100][100]={0}; 4 | bool ratinamaze(char maze[][10],int i,int j,int m,int n){ 5 | // base case 6 | if(i==n-1&&j==m-1){ 7 | sol[i][j]=1; 8 | // printing 9 | for(int k=0;kdenotes colums , n->>>rows 60 | bool ans=ratinamaze(maze,0,0,m,n); 61 | if(ans==false){ 62 | cout<<"Path doesn't exist"< 2 | using namespace std; 3 | int cou=0; 4 | bool kyaqueenplacehosaktihai(int board[][50],int i,int j,int n){ 5 | // rows and cols 6 | for(int k=0;k right 19 | int r=i,c=j; //r=0,c=0,i=0,j=0 20 | while(i>=0&&j=0&&c>=0){ //r=0 c=0 34 | if(board[r][c]==1){ 35 | return false; 36 | 37 | } 38 | r--; 39 | c--; 40 | // r-1 41 | // c=-1 42 | 43 | } 44 | 45 | 46 | return true; 47 | 48 | 49 | } 50 | 51 | bool nqueen(int board[][50],int i,int n){ 52 | // base case 53 | if(i==n){ 54 | for(int k=0;k>n; 103 | nqueen(board,0,n); 104 | cout< 2 | using namespace std; 3 | bool kyamainumberrakhsaktihun(int mat[][9],int i,int j,int number,int n){ 4 | // rows and cols 5 | for(int k=0;k 2 | using namespace std; 3 | int main(){ 4 | // int a=10; 5 | 6 | // primitive data type -->DMA 7 | 8 | int *aptr=new int; 9 | *aptr=50; 10 | cout<<*aptr<>*(arrptr+i); //arr[i] 24 | /* code */ 25 | } 26 | for (int i = 0; i < 5; ++i) 27 | { 28 | cout<<*(arrptr+i)<<" "<DMA 37 | delete aptr; 38 | aptr=NULL; 39 | 40 | delete fptr; 41 | fptr=NULL; 42 | 43 | 44 | // Array delete 45 | 46 | delete []arrptr; 47 | arrptr=NULL; 48 | 49 | 50 | 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /lecture21/pointerfunctions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/lecture21/pointerfunctions -------------------------------------------------------------------------------- /lecture21/pointerfunctions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int* f(int n){ 4 | int *arr=new int[1000]; 5 | for (int i = 0; i < n; ++i) 6 | { 7 | cin>>*(arr+i); //arr[i] 8 | /* code */ 9 | } 10 | return arr; 11 | 12 | 13 | 14 | } 15 | int main(){ 16 | int n; 17 | cin>>n; 18 | 19 | int*aptr=f(n); 20 | 21 | 22 | 23 | for (int i = 0; i < n; ++i) 24 | { 25 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int **arr; 6 | int rows,cols; 7 | cin>>rows>>cols; 8 | arr=new int*[rows]; 9 | for(int i=0;i>arr[i][j]; 20 | arr[i][j]=number++; 21 | // number++; 22 | } 23 | } 24 | 25 | // Priniting array 26 | 27 | for(int i=0;i>arr[i][j]; 30 | cout< 2 | using namespace std; 3 | class Pair{ 4 | public: 5 | int a; 6 | int b; 7 | 8 | // constructor 9 | Pair(int a,int b){ 10 | this->a=a; 11 | this->b=b; 12 | 13 | } 14 | bool operator<(Pair &y){ 15 | if(a 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | // syntax 7 | 8 | 9 | // vector vectoraname; 10 | vector v; 11 | v.push_back(1); 12 | v.push_back(2); 13 | v.push_back(3); 14 | v.push_back(4); 15 | v.push_back(5); 16 | v.push_back(6); 17 | v.push_back(7); 18 | v.push_back(8); 19 | v.push_back(9); 20 | 21 | 22 | // vector v(10); //10 buckets get created and initilized with 0 23 | // // v.push_back(1); 24 | // // v.push_back(2); 25 | // // v.push_back(3); 26 | // // v.push_back(4); 27 | // // v.push_back(5); 28 | // // for (int i = 0;i<12; ++i) 29 | // // { 30 | // // cout<>v[i]; 50 | // /* code */ 51 | // } 52 | 53 | // for (int i = 0; i < 10; ++i) 54 | // { 55 | // cout< 2 | using namespace std; 3 | class Vector{ 4 | public: 5 | int *arr; 6 | int ms; 7 | int cs; 8 | // constructor 9 | Vector(int s=2){ //default argument 10 | ms=s; 11 | cs=0; 12 | arr=new int[ms]; 13 | } 14 | void push_back(int data){ 15 | if(ms==cs){ 16 | int *oldarr=arr; 17 | int oldms=ms; 18 | arr=new int[2*ms]; 19 | ms*=2; 20 | //purani array k elements ko copy kiya 21 | for(int i=0;i0){ 35 | cs--; 36 | } 37 | 38 | } 39 | int size(){ 40 | return cs; 41 | 42 | } 43 | int capacity(){ 44 | return ms; 45 | 46 | } 47 | int operator[](int i){ 48 | return arr[i]; 49 | 50 | } 51 | 52 | }; 53 | int main(){ 54 | 55 | Vector v; 56 | v.push_back(1); 57 | v.push_back(4); 58 | v.push_back(2); 59 | v.push_back(5); 60 | v.push_back(7); 61 | v.push_back(1); 62 | v.push_back(4); 63 | v.push_back(2); 64 | v.push_back(5); 65 | v.push_back(7); 66 | cout<<"size of vector is "< 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node* next; 7 | // constructor 1st way 8 | node(int d){ 9 | data=d; 10 | next=NULL; 11 | } 12 | // // constructor 2nd way 13 | // node(int d):data(d),next(NULL){ 14 | 15 | // } 16 | 17 | }; 18 | void insertatfront(node*&head,node*&tail,int data){ 19 | node*n=new node(data); 20 | 21 | //head ,tail-->NULL 22 | if(head==NULL){ //agar abhi tak ll bana hi nahi 23 | head=n; 24 | tail=n; 25 | 26 | } 27 | else{ 28 | n->next=head; 29 | head=n; 30 | 31 | } 32 | 33 | 34 | 35 | } 36 | 37 | void insertattail(node*&head,node*&tail,int data){ 38 | node*n=new node(data); 39 | if(head==NULL){ //agar abhi tak ll bana hi nahi 40 | head=n; 41 | tail=n; 42 | 43 | } 44 | else{ 45 | tail->next=n; 46 | tail=n; 47 | 48 | } 49 | 50 | 51 | 52 | 53 | } 54 | // iterativeway 55 | int length(node*head){ 56 | int cnt=0; 57 | while(head!=NULL){ 58 | cnt++; 59 | head=head->next; 60 | } 61 | return cnt; 62 | } 63 | // recursive way 64 | int lengthrec(node*head){ 65 | if(head==NULL){ 66 | return 0; 67 | } 68 | return 1+lengthrec(head->next); 69 | } 70 | void insertatanyposition(node*&head,node*&tail,int data,int pos){ 71 | if(pos==0){ 72 | insertatfront(head,tail,data); 73 | } 74 | else if(pos>=length(head)){ 75 | insertattail(head,tail,data); 76 | 77 | } 78 | else{ 79 | node*temp=head; 80 | node*n=new node(data); 81 | for(int i=1;i<=pos-1;i++){// 1,2-->pos3 82 | temp=temp->next; 83 | 84 | } 85 | n->next=temp->next; 86 | temp->next=n; 87 | 88 | } 89 | 90 | 91 | } 92 | void printll(node*head){ 93 | while(head!=NULL){ 94 | cout<data<<"->"; 95 | head=head->next; 96 | 97 | } 98 | cout<data<1 113 | insertatfront(head,tail,3);//3->2->1 114 | insertatfront(head,tail,4);//4->3->2->1 115 | insertatfront(head,tail,5);//5->4->3->2->1 116 | 117 | printll(head); 118 | // insertatfront(head,tail,1); //1 119 | // insertatfront(head,tail,2);// 2->1 120 | // insertatfront(head,tail,3);//3->2->1 121 | // insertatfront(head,tail,4);//4->3->2->1 122 | // insertatfront(head,tail,5);//5->4->3->2->1 123 | // printll(head); 124 | insertattail(head,tail,6);//5->4->3->2->1->6 125 | insertattail(head,tail,7);//5->4->3->2->1->6->7 126 | insertattail(head,tail,8);//5->4->3->2->1->6->7->8 127 | printll(head); 128 | cout<<"length of ll is "< 2 | #include 3 | using namespace std; 4 | template 5 | class Stack{ 6 | 7 | vectorv; 8 | 9 | public: 10 | 11 | U top(){ 12 | return v[v.size()-1]; 13 | } 14 | void push(U data){ 15 | v.push_back(data); 16 | } 17 | void pop(){ 18 | v.pop_back(); 19 | } 20 | int size(){ 21 | return v.size(); 22 | } 23 | bool empty(){ 24 | return v.size()==0; 25 | 26 | } 27 | 28 | 29 | }; 30 | int main(){ 31 | // Stacks; 32 | // s.push(1); 33 | // s.push(2); 34 | // s.push(3); 35 | // s.push(4); 36 | Stacks; 37 | s.push('A'); 38 | s.push('B'); 39 | s.push('C'); 40 | s.push('D'); 41 | s.push('E'); 42 | 43 | 44 | while(!s.empty()){ 45 | cout< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | //syntax -->stack name; 6 | 7 | 8 | // stack s; 9 | 10 | 11 | // s.push(1); 12 | // s.push(2); 13 | // s.push(3); 14 | // s.push(4); 15 | 16 | 17 | stack s; 18 | s.push('A'); 19 | s.push('B'); 20 | s.push('C'); 21 | s.push('D'); 22 | s.push('E'); 23 | 24 | 25 | while(!s.empty()){ 26 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool isbalanced(char *arr){ 6 | stack s; 7 | for (int i = 0; arr[i]!='\0'; ++i) 8 | { 9 | char ch=arr[i]; 10 | switch(ch){ 11 | case '(': 12 | case '{': 13 | case '[': s.push(ch); 14 | break; 15 | 16 | 17 | case ')': 18 | if(!s.empty()&&s.top()=='('){ 19 | s.pop(); 20 | } 21 | else{ 22 | return false; 23 | } 24 | break; 25 | 26 | case '}': 27 | if(!s.empty()&&s.top()=='{'){ 28 | s.pop(); 29 | } 30 | else{ 31 | return false; 32 | } 33 | break; 34 | case ']': 35 | if(!s.empty()&&s.top()=='['){ 36 | s.pop(); 37 | } 38 | else{ 39 | return false; 40 | } 41 | 42 | } 43 | } 44 | if(s.empty()){ 45 | return true; 46 | } 47 | return false; 48 | 49 | 50 | 51 | } 52 | 53 | int main(){ 54 | char arr[]="{(a+b)+c*(d+e)]"; 55 | 56 | if(isbalanced(arr)){ 57 | cout<<"balanced expression"< 3 | class node{ 4 | public: 5 | // int data; 6 | T data; 7 | node*next; 8 | //constructor 9 | // node(int d){ 10 | node(T d){ 11 | data=d; 12 | next=NULL; 13 | 14 | } 15 | }; -------------------------------------------------------------------------------- /lecture28/reverseastack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/lecture28/reverseastack -------------------------------------------------------------------------------- /lecture28/reverseastack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void insertatbottom(stack &s,int te){ 5 | if(s.empty()){ 6 | s.push(te); 7 | return; 8 | } 9 | int top=s.top(); 10 | s.pop(); 11 | insertatbottom(s,te); 12 | s.push(top); 13 | } 14 | void reversestack(stack &s){ 15 | // base case 16 | if(s.empty()){ 17 | return; 18 | } 19 | 20 | 21 | // recursive case 22 | int te=s.top(); 23 | s.pop(); 24 | reversestack(s); 25 | insertatbottom(s,te); 26 | 27 | } 28 | void print(stacks){ 29 | while(!s.empty()){ 30 | cout< s; 41 | s.push(1); 42 | s.push(2); 43 | s.push(3); 44 | s.push(1); 45 | s.push(2); 46 | s.push(3); 47 | s.push(4); 48 | s.push(4); 49 | print(s); 50 | 51 | reversestack(s); 52 | print(s); 53 | 54 | 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /lecture28/stack.h: -------------------------------------------------------------------------------- 1 | #include"node.h" 2 | using namespace std; 3 | template 4 | class Stack{ 5 | private: 6 | node*head; 7 | int len; 8 | 9 | public: 10 | //constructor 11 | Stack(){ 12 | head=NULL; 13 | len=0; 14 | } 15 | // void push(int d){ 16 | void push(T d){ 17 | node*n=new node(d); 18 | n->next=head; 19 | head=n; 20 | len++; 21 | 22 | } 23 | 24 | void pop(){ 25 | if(head==NULL){ //empty 26 | return; 27 | } 28 | else if(head->next==NULL){ 29 | delete head; 30 | head=NULL; 31 | len--; 32 | } 33 | else{ 34 | node*temp=head; 35 | head=head->next; 36 | delete temp; 37 | len--; 38 | } 39 | 40 | 41 | } 42 | bool empty(){ 43 | return head==NULL; 44 | } 45 | int size(){ 46 | return len; 47 | 48 | } 49 | T top(){ 50 | return head->data; 51 | } 52 | 53 | 54 | }; -------------------------------------------------------------------------------- /lecture28/stackimplementation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/lecture28/stackimplementation -------------------------------------------------------------------------------- /lecture28/stackimplementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Stack{ 5 | 6 | vectorv; 7 | 8 | public: 9 | 10 | int top(){ 11 | return v[v.size()-1]; 12 | } 13 | void push(int data){ 14 | v.push_back(data); 15 | } 16 | void pop(){ 17 | v.pop_back(); 18 | } 19 | int size(){ 20 | return v.size(); 21 | } 22 | bool empty(){ 23 | return v.size()==0; 24 | 25 | } 26 | 27 | 28 | }; 29 | int main(){ 30 | Stack s; 31 | s.push(1); 32 | s.push(2); 33 | s.push(3); 34 | s.push(4); 35 | 36 | while(!s.empty()){ 37 | cout< 2 | using namespace std; 3 | class node{ 4 | public: 5 | // int data; 6 | char data; 7 | node*next; 8 | //constructor 9 | // node(int d){ 10 | node(char d){ 11 | data=d; 12 | next=NULL; 13 | 14 | } 15 | }; 16 | class Stack{ 17 | private: 18 | node*head; 19 | int len; 20 | 21 | public: 22 | //constructor 23 | Stack(){ 24 | head=NULL; 25 | len=0; 26 | } 27 | // void push(int d){ 28 | void push(char d){ 29 | node*n=new node(d); 30 | n->next=head; 31 | head=n; 32 | len++; 33 | 34 | } 35 | 36 | void pop(){ 37 | if(head==NULL){ //empty 38 | return; 39 | } 40 | else if(head->next==NULL){ 41 | delete head; 42 | head=NULL; 43 | len--; 44 | } 45 | else{ 46 | node*temp=head; 47 | head=head->next; 48 | delete temp; 49 | len--; 50 | } 51 | 52 | 53 | } 54 | bool empty(){ 55 | return head==NULL; 56 | } 57 | int size(){ 58 | return len; 59 | 60 | } 61 | char top(){ 62 | return head->data; 63 | } 64 | 65 | 66 | }; 67 | int main(){ 68 | Stack s; 69 | // s.push(1); 70 | // s.push(2); 71 | // s.push(3); 72 | // s.push(4); 73 | s.push('A'); 74 | s.push('B'); 75 | s.push('C'); 76 | s.push('D'); 77 | s.push('E'); 78 | 79 | while(!s.empty()){ 80 | cout< 2 | #include"stack.h" 3 | 4 | int main(){ 5 | Stacks; 6 | s.push(1); 7 | s.push(2); 8 | s.push(3); 9 | s.push(4); 10 | // Stacks; 11 | // s.push('A'); 12 | // s.push('B'); 13 | // s.push('C'); 14 | // s.push('D'); 15 | // s.push('E'); 16 | 17 | 18 | while(!s.empty()){ 19 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | queue q; 7 | // q.push(1); 8 | // q.push(2); 9 | // q.push(3); 10 | // q.push(4); 11 | // q.push(5); 12 | // q.push(3); 13 | // q.push(4); 14 | // q.push(5); 15 | // q.push(3); 16 | // q.push(4); 17 | 18 | int n; 19 | cin>>n; 20 | for (int i = 0; i < n; ++i) 21 | { 22 | int temp; 23 | cin>>temp; 24 | q.push(temp); 25 | /* code */ 26 | } 27 | 28 | while(!q.empty()){ 29 | cout< 2 | using namespace std; 3 | 4 | class Queue{ 5 | int *arr; 6 | int f; 7 | int n; 8 | int e; 9 | int cs; 10 | 11 | 12 | public: 13 | 14 | Queue(int s=5){ 15 | n=s; 16 | arr=new int[s]; 17 | f=0; 18 | e=n-1; 19 | cs=0; 20 | 21 | 22 | 23 | } 24 | 25 | void push(int d){ 26 | if(cs overflow"<0){ 41 | f=(f+1)%n; 42 | cs--; 43 | 44 | } 45 | else{ 46 | cout<<"queue is empty-->underflor"< 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node*next; 7 | // constructor 8 | 9 | node(int d){ 10 | data=d; 11 | next=NULL; 12 | } 13 | }; 14 | class Queue{ 15 | node *head; 16 | node*tail; 17 | int len; 18 | // constructor 19 | 20 | 21 | public: 22 | 23 | Queue(){ 24 | head=NULL; 25 | tail=NULL; 26 | len=0; 27 | } 28 | 29 | void push(int d){ 30 | node*n=new node(d); 31 | if(head==NULL){ 32 | head=tail=n; 33 | 34 | } 35 | else{ 36 | tail->next=n; 37 | tail=n; 38 | 39 | } 40 | len++; 41 | 42 | } 43 | void pop(){ 44 | if(head==NULL){ 45 | return; 46 | } 47 | else if(head->next==NULL){ 48 | delete head; 49 | head=NULL; 50 | tail=NULL; 51 | len--; 52 | } 53 | else{ 54 | node*n=head; 55 | head=head->next; 56 | delete n; 57 | len--; 58 | } 59 | 60 | } 61 | int front(){ 62 | return head->data; 63 | 64 | } 65 | bool empty(){ 66 | return head==NULL; 67 | 68 | } 69 | int size(){ 70 | return len; 71 | 72 | } 73 | }; 74 | int main(){ 75 | Queue q; 76 | q.push(1); 77 | q.push(2); 78 | q.push(3); 79 | q.push(4); 80 | q.push(5); 81 | q.push(3); 82 | q.push(4); 83 | 84 | while(!q.empty()){ 85 | cout< 2 | #include 3 | using namespace std; 4 | int partionhelper(int*arr,int s,int e){ 5 | int i=s-1; 6 | int pivot=arr[e]; 7 | for(int j=s;j<=e-1;j++){ 8 | if(arr[j]<=pivot){ 9 | i++; 10 | swap(arr[i],arr[j]); 11 | } 12 | } 13 | swap(arr[i+1],arr[e]); 14 | return i+1; 15 | 16 | } 17 | int partion(int *arr,int s,int e){ 18 | srand(time(0)); 19 | int r=s+rand()%(e-s); 20 | swap(arr[r],arr[e]); 21 | return partionhelper(arr,s,e); 22 | } 23 | void quicksort(int *arr,int s,int e){ 24 | // base case 25 | if(s>=e){ 26 | return; 27 | } 28 | 29 | 30 | // recursive case 31 | int p=partion(arr,s,e); //i+1 32 | quicksort(arr,s,p-1); 33 | quicksort(arr,p+1,e); //tail recursion-->without return 34 | 35 | // return quicksort(arr,p+1,e); //non tail 36 | 37 | 38 | } 39 | 40 | int main(){ 41 | 42 | int n,arr[1000]; 43 | cin>>n; 44 | for (int i = 0; i < n; ++i) 45 | { 46 | cin>>arr[i]; 47 | /* code */ 48 | } 49 | cout<<"Before sorting"< 2 | #include 3 | using namespace std; 4 | 5 | class Stack{ 6 | queueq1; 7 | queueq2; 8 | 9 | public: 10 | 11 | void push(int d){ 12 | if(q1.empty()&&q2.empty()){ 13 | q1.push(d); 14 | 15 | } 16 | else if(!q1.empty()&&q2.empty()){ 17 | q1.push(d); 18 | 19 | } 20 | else{ 21 | q2.push(d); 22 | 23 | } 24 | 25 | } 26 | void pop(){ 27 | if(!q1.empty()&&q2.empty()){ 28 | while(q1.size()>1){ 29 | q2.push(q1.front()); 30 | q1.pop(); 31 | } 32 | q1.pop(); 33 | 34 | } 35 | else if(q1.empty()&&!q2.empty()){ 36 | while(q2.size()>1){ 37 | q1.push(q2.front()); 38 | q2.pop(); 39 | } 40 | q2.pop(); 41 | } 42 | else{ 43 | cout<<"underflow -->there is no element to be popped"<1){ 50 | q2.push(q1.front()); 51 | q1.pop(); 52 | } 53 | int ans=q1.front(); //4 54 | q2.push(ans); 55 | q1.pop(); 56 | return ans; 57 | 58 | } 59 | else if(q1.empty()&&!q2.empty()){ 60 | while(q2.size()>1){ 61 | q1.push(q2.front()); 62 | q2.pop(); 63 | } 64 | int ans=q2.front(); //4 65 | q1.push(ans); 66 | q2.pop(); 67 | return ans; 68 | } 69 | else{ 70 | cout<<"underflow "< 2 | using namespace std; 3 | int main(){ 4 | if(0){ 5 | cout<<"i am a student"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | // int a=10; 6 | 7 | 8 | // cout<<"before inc"<0&&a++){ 25 | // a=a+1; 26 | // } 27 | // else{ 28 | // b=b+1; 29 | // } 30 | 31 | // cout<<"value of a is : "<0){ 37 | // a=a+1; 38 | // } 39 | // else{ 40 | // b=b+1; 41 | // } 42 | 43 | // cout<<"value of a is : "<0||a++){ 50 | // a=a+1; 51 | // } 52 | // else{ 53 | // b=b+1; 54 | // } 55 | 56 | // cout<<"value of a is : "<0){ 63 | // a=a+1; 64 | // } 65 | // else{ 66 | // b=b+1; 67 | // } 68 | 69 | // cout<<"value of a is : "<0){ 77 | a=a+1; 78 | } 79 | else{ 80 | b=b+1; 81 | } 82 | 83 | cout<<"value of a is : "< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int ffn=0,sfn=1; 7 | // if(n==ffn||n==sfn){ 8 | // cout<<"yes it is part of fibonacci series"< 2 | using namespace std; 3 | int main(){ 4 | int n,val; 5 | cin>>n; 6 | int rowno=1; 7 | while(rowno<=n){ 8 | 9 | if(rowno%2!=0){ 10 | val=1; 11 | } 12 | else{ 13 | val=0; 14 | } 15 | 16 | 17 | int count=1; 18 | while(count<=rowno){ 19 | cout< 2 | using namespace std; 3 | int main(){ 4 | int a=10; 5 | char ch='A'; 6 | bool israining='5'; 7 | float f=10.4; 8 | double x=10.4; 9 | 10 | // sizeof operator mai hum ya tou datatype desakte hai ,ya tou variable name, 11 | // ya tou direct value 12 | 13 | 14 | // cout< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int ival,fval,step; 6 | cin>>ival>>fval>>step; 7 | 8 | 9 | int f=ival; 10 | 11 | while(f<=fval){ 12 | 13 | 14 | // int cel=(5/9.0)*(f-32); //explicit type conversion 15 | 16 | // int cel=float(5)/9*(f-32);//explicit type conversion 17 | 18 | // int cel=((5)*(f-32))/9; 19 | 20 | int cel=(5*(f-32))/9; 21 | 22 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | // Minheap h; 8 | // priority_queue h; //by default ->max heap 9 | priority_queue ,greater > h; //min heap 10 | h.push(1); 11 | h.push(4); 12 | h.push(14); 13 | h.push(24); 14 | h.push(40); 15 | h.push(-4); 16 | h.push(3); 17 | h.push(20); 18 | h.push(13); 19 | h.push(5); 20 | while(!h.empty()){ 21 | cout< 2 | #include 3 | using namespace std; 4 | class Minheap{ 5 | private: 6 | void heapify(int i){ 7 | int min_index=i; 8 | int left=2*i; 9 | int right=2*i+1; //left +1 10 | if(leftv[left]){ 11 | min_index=left; 12 | } 13 | if(rightv[right]){ 14 | min_index=right; 15 | } 16 | if(min_index!=i){ 17 | swap(v[i],v[min_index]); 18 | heapify(min_index); 19 | } 20 | } 21 | 22 | 23 | public: 24 | vector v; 25 | Minheap(){ 26 | v.push_back(-1); 27 | } 28 | void push(int data){ 29 | v.push_back(data); 30 | int c=v.size()-1; 31 | int p=c/2; 32 | while(c>1&&v[p]>v[c]){ 33 | swap(v[p],v[c]); 34 | c=p; 35 | p=p/2; 36 | } 37 | } 38 | int top(){ 39 | return v[1]; 40 | } 41 | void pop(){ 42 | swap(v[1],v[v.size()-1]); 43 | v.pop_back(); 44 | heapify(1); 45 | } 46 | bool empty(){ 47 | return v.size()==1; 48 | } 49 | 50 | }; 51 | int main(){ 52 | Minheap h; 53 | h.push(1); 54 | h.push(4); 55 | h.push(14); 56 | h.push(24); 57 | h.push(40); 58 | h.push(-4); 59 | h.push(3); 60 | h.push(20); 61 | h.push(13); 62 | h.push(5); 63 | while(!h.empty()){ 64 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | #define Minheap priority_queue ,greater > 6 | void printheap(Minheap h){ 7 | while(!h.empty()){ 8 | cout<>n; 25 | if(n==-1){ 26 | printheap(h); 27 | 28 | 29 | } 30 | else{ 31 | if(count 2 | using namespace std; 3 | class node{ 4 | public: 5 | int val; 6 | string key; 7 | node* next; 8 | // constructor 9 | node(string k,int v){ 10 | val=v; 11 | key=k; 12 | next=NULL; 13 | 14 | 15 | } 16 | }; 17 | class hashmap{ 18 | int ts; 19 | int cs; 20 | node**arr; 21 | 22 | 23 | 24 | int hashfunction(string key){ 25 | int ans=0; 26 | int mul=1; 27 | for(int i=0;ikey,head->val); 52 | head=head->next; 53 | 54 | } 55 | } 56 | delete[]oldarr; 57 | } 58 | 59 | 60 | public: 61 | // constructor 62 | hashmap(int size=7){ 63 | cs=0; 64 | ts=size; 65 | arr=new node*[ts]; 66 | for(int i=0;inext=arr[indx]; 76 | arr[indx]=n; 77 | cs++; 78 | // if((cs/(ts*1.0))>0.6){ 79 | // rehashing(); 80 | // } 81 | 82 | } 83 | 84 | void print(){ 85 | for (int i = 0; i < ts; ++i) 86 | { 87 | cout<"; 88 | node*head=arr[i]; 89 | while(head){ 90 | cout<key<<" "; 91 | head=head->next; 92 | } 93 | cout<key==key){ 103 | return head; 104 | } 105 | head=head->next; 106 | } 107 | return NULL; 108 | } 109 | 110 | 111 | 112 | 113 | }; 114 | int main(){ 115 | hashmap h; 116 | h.insert("mango",100); 117 | h.insert("kiwi",150); 118 | h.insert("apple",60); 119 | h.insert("banana",180); 120 | h.insert("abc",1030); 121 | h.insert("def",1540); 122 | h.insert("aghi",660); 123 | // h.insert("banana",180); 124 | 125 | h.print(); 126 | cout< 2 | using namespace std; 3 | int fibo(int n){ 4 | 5 | 6 | // base case 7 | if(n==0||n==1){ 8 | return n; 9 | } 10 | 11 | // recursive case 12 | return fibo(n-1)+fibo(n-2); 13 | } 14 | 15 | int topdownfibo(int n,int *dp){ 16 | 17 | 18 | // base case 19 | if(n==0||n==1){ 20 | return dp[n]=n; 21 | } 22 | // calculate karne se pehle check 23 | if(dp[n]!=-1){ 24 | return dp[n]; 25 | 26 | } 27 | 28 | // recursive case 29 | // return karne se pehle store 30 | // arr[n]=fibodp(n-1,arr)+fibodp(n-2,arr); 31 | return dp[n]=topdownfibo(n-1,dp)+topdownfibo(n-2,dp); 32 | } 33 | 34 | int bottomupfibo(int n){ 35 | int*arr=new int[n+1]; 36 | //initilization 37 | arr[0]=0; 38 | arr[1]=1; 39 | // loop 40 | 41 | for(int i=2;i<=n;i++){ 42 | arr[i]=arr[i-1]+arr[i-2]; 43 | } 44 | 45 | for(int i=0;i<=n;i++){ 46 | cout<>n; 60 | int *arr=new int[n+1]; 61 | for (int i = 0; i <=n; ++i) 62 | { 63 | arr[i]=-1; 64 | } 65 | cout<<"topdownfibo"< 2 | using namespace std; 3 | class node{ 4 | public: 5 | int val; 6 | string key; 7 | node* next; 8 | // constructor 9 | node(string k,int v){ 10 | val=v; 11 | key=k; 12 | next=NULL; 13 | 14 | 15 | } 16 | }; 17 | class hashmap{ 18 | int ts; 19 | int cs; 20 | node**arr; 21 | 22 | 23 | 24 | int hashfunction(string key){ 25 | int ans=0; 26 | int mul=1; 27 | for(int i=0;ikey,head->val); 52 | head=head->next; 53 | 54 | } 55 | } 56 | delete[]oldarr; 57 | } 58 | 59 | 60 | public: 61 | // constructor 62 | hashmap(int size=7){ 63 | cs=0; 64 | ts=size; 65 | arr=new node*[ts]; 66 | for(int i=0;inext=arr[indx]; 76 | arr[indx]=n; 77 | cs++; 78 | // if((cs/(ts*1.0))>0.6){ 79 | // rehashing(); 80 | // } 81 | 82 | } 83 | 84 | void print(){ 85 | for (int i = 0; i < ts; ++i) 86 | { 87 | cout<"; 88 | node*head=arr[i]; 89 | while(head){ 90 | cout<key<<" "; 91 | head=head->next; 92 | } 93 | cout<key==key){ 103 | return head; 104 | } 105 | head=head->next; 106 | } 107 | return NULL; 108 | } 109 | 110 | 111 | int& operator[](string key){ 112 | node*ans=search(key); 113 | // key is not present -->we have to insert this key in hashmap 114 | if(ans==NULL){ 115 | int g; //value garbage 116 | insert(key,g); 117 | ans=search(key); 118 | return ans->val; 119 | 120 | 121 | } 122 | 123 | // key is present 124 | else{ 125 | return ans->val; 126 | } 127 | } 128 | 129 | 130 | 131 | 132 | }; 133 | int main(){ 134 | hashmap h; 135 | h.insert("mango",100); 136 | h.insert("kiwi",150); 137 | h.insert("apple",60); 138 | h.insert("banana",180); 139 | h.insert("abc",1030); 140 | h.insert("def",1540); 141 | h.insert("aghi",660); 142 | // h.insert("banana",180); 143 | h.print(); 144 | 145 | cout<<"after inserting papaya"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | // unordered_map h; 6 | 7 | unordered_map h; 8 | // 1st way to insert 9 | h["Apple"]=100; 10 | h["banana"]=150; 11 | 12 | // 2nd way to insert 13 | // h.insert({"papaya",160}); 14 | 15 | //3rd way to insert 16 | pairp("mango",80); 17 | h.insert(p); 18 | 19 | 20 | //4th way to insert 21 | h.insert(make_pair("grapes",500)); 22 | 23 | 24 | // print the hashmap //for each pair of fruit in the hashmap h 25 | for(pair fruit:h){ 26 | cout<"<"<"<first<<" "<second< 2 | using namespace std; 3 | int minden(int *coins,int amount,int n){ 4 | // base case 5 | if(amount==0){ 6 | return 0; 7 | } 8 | 9 | 10 | // recursive case 11 | int ans=INT_MAX; 12 | for(int i=0;i=0){ 14 | int chotaamount=amount-coins[i]; 15 | int chotaans=minden(coins,chotaamount,n); 16 | if(chotaans!=INT_MAX&&chotaans=0){ 45 | int chotaamount=amount-coins[i]; 46 | int chotaans=mindentopdown(coins,chotaamount,n,dp); 47 | if(chotaans!=INT_MAX&&chotaans=0){ 69 | int chotaamt=rup-coins[i]; 70 | dp[rup]=min(dp[chotaamt]+1,dp[rup]); 71 | 72 | } 73 | } 74 | } 75 | for(int i=0;i<=amount;i++){ 76 | cout<>amount; 89 | 90 | 91 | int dp[1000]; 92 | for (int i = 0; i < 1000; ++i) 93 | { 94 | dp[i]=-1; 95 | } 96 | cout< 2 | using namespace std; 3 | int minto1(int n){ 4 | // base case 5 | if(n==1){ 6 | return 0; 7 | } 8 | 9 | 10 | 11 | // recursive case 12 | int op1,op2,op3; 13 | op1=op2=op3=INT_MAX; 14 | op3=minto1(n-1); 15 | 16 | if(n%3==0){ 17 | op1=minto1(n/3); 18 | } 19 | if(n%2==0){ 20 | op2=minto1(n/2); 21 | 22 | } 23 | 24 | 25 | return min(op1,min(op2,op3))+1; //min(2 arguments) 26 | 27 | } 28 | 29 | //topdown 30 | int topdownminto1(int n,int*dp){ 31 | // base case 32 | if(n==1){ 33 | return dp[n]=0; 34 | } 35 | 36 | 37 | // calculate karne se pehle check 38 | if(dp[n]!=-1){ 39 | return dp[n]; 40 | } 41 | 42 | // recursive case 43 | int op1,op2,op3; 44 | op1=op2=op3=INT_MAX; 45 | op3=topdownminto1(n-1,dp); 46 | 47 | if(n%3==0){ 48 | op1=topdownminto1(n/3,dp); 49 | } 50 | if(n%2==0){ 51 | op2=topdownminto1(n/2,dp); 52 | 53 | } 54 | //RETURN SE PEHLE STORE 55 | 56 | return dp[n]=min(op1,min(op2,op3))+1; //min(2 arguments) 57 | 58 | } 59 | int bottonup(int n){ 60 | int *dp=new int[n+1]; 61 | // base case of td id initilization condition here 62 | dp[1]=0; 63 | int op1,op2,op3; 64 | for(int i=2;i<=n;i++){ 65 | op1=op2=op3=INT_MAX; 66 | op1=dp[i-1]; 67 | if(i%2==0){ 68 | op2=dp[i/2]; 69 | } 70 | if(i%3==0){ 71 | op3=dp[i/3]; 72 | } 73 | dp[i]=min(op1,min(op2,op3))+1; 74 | } 75 | for(int i=1;i<=n;i++){ 76 | cout<>n; 87 | 88 | int *dp=new int[n+1]; 89 | for (int i = 0; i <=n; ++i) 90 | { 91 | dp[i]=-1; 92 | } 93 | cout< 2 | using namespace std; 3 | int solve(int cost[][4],int i,int j){ 4 | // base case 5 | if(i==0&&j==0){ 6 | return cost[i][j]; 7 | } 8 | if(i<0||j<0){ 9 | return INT_MAX; 10 | } 11 | 12 | 13 | 14 | // recursive case 15 | int op2=solve(cost,i-1,j); 16 | int op1=solve(cost,i,j-1); 17 | return min(op1,op2)+cost[i][j]; 18 | 19 | } 20 | int topdown(int cost[][4],int i,int j,int dp[][100]){ 21 | // base case 22 | if(i==0&&j==0){ 23 | return dp[i][j]=cost[i][j]; 24 | } 25 | if(i<0||j<0){ 26 | return INT_MAX; 27 | } 28 | if(dp[i][j]!=-1){ 29 | return dp[i][j]; 30 | } 31 | // recursive case 32 | int op2=topdown(cost,i-1,j,dp); 33 | int op1=topdown(cost,i,j-1,dp); 34 | 35 | return dp[i][j]=min(op1,op2)+cost[i][j]; 36 | 37 | } 38 | int bottomup(int a,int b,int cost[][4]){ 39 | int dp[100][100]; 40 | // dp[0][0]=cost[0][0]; 41 | for(int i=0;i<4;i++){ 42 | for(int j=0;j<4;j++){ 43 | if(i==0&&j==0){ 44 | dp[i][j]=cost[i][j]; 45 | } 46 | // ist row 47 | else if(i==0){ 48 | dp[i][j]=cost[i][j]+dp[i][j-1]; 49 | } 50 | // ist cols 51 | else if(j==0){ 52 | dp[i][j]=cost[i][j]+dp[i-1][j]; 53 | } 54 | // baki ki array 55 | else{ 56 | dp[i][j]=min(dp[i-1][j],dp[i][j-1])+cost[i][j]; 57 | } 58 | 59 | 60 | } 61 | } 62 | return dp[a][b]; 63 | } 64 | 65 | 66 | 67 | 68 | 69 | int main(){ 70 | int cost[][4]={ 71 | {2,1,3,4}, 72 | {3,1,1,10}, 73 | {1,6,1,1}, 74 | {2,7,4,3} 75 | }; 76 | int dp[100][100]; 77 | // for (int i = 0; i < 100; ++i) 78 | // { 79 | // for(int j=0;j<100;j++){ 80 | // dp[i][j]=-1; 81 | // } 82 | // } 83 | memset(dp,-1,sizeof(dp)); 84 | int i=3,j=2; 85 | cout< 2 | using namespace std; 3 | int wineproblem(int l,int r,int *price,int day){ 4 | // base case 5 | if(l>r){ 6 | return 0; 7 | } 8 | 9 | 10 | // recursive case 11 | int op1=(price[l]*day+wineproblem(l+1,r,price,day+1)); 12 | int op2=(price[r]*day+wineproblem(l,r-1,price,day+1)); 13 | return max(op1,op2); 14 | 15 | } 16 | 17 | 18 | int topdown(int l,int r,int *price,int day,int dp[][100]){ 19 | // base case 20 | if(l>r){ 21 | // store 22 | return dp[l][r]=0; 23 | } 24 | //calculate karne se pehle check 25 | if(dp[l][r]!=-1){ 26 | return dp[l][r]; 27 | } 28 | 29 | 30 | // recursive case 31 | int op1=(price[l]*day+topdown(l+1,r,price,day+1,dp)); 32 | int op2=(price[r]*day+topdown(l,r-1,price,day+1,dp)); 33 | // store 34 | return dp[l][r]=max(op1,op2); 35 | 36 | } 37 | 38 | int bottomup(int *price ,int n){ 39 | int dp[100][100]={0}; 40 | // diagnols pe kaam kiya 41 | for(int i=0;i=0;i--){ 45 | for(int j=0;j 2 | using namespace std; 3 | char s1[100],s2[100]; //i is iterator to s1 and j is iterator to s2 4 | int lcs(int i,int j){ 5 | // base case 6 | if(s1[i]=='\0'||s2[j]=='\0'){ 7 | return 0; 8 | } 9 | 10 | 11 | // recursive case 12 | if(s1[i]==s2[j]){ 13 | return 1+lcs(i+1,j+1); 14 | } 15 | else{ 16 | // agar unequal hogi tou donu mai se max 17 | return max(lcs(i+1,j),lcs(i,j+1)); 18 | 19 | } 20 | 21 | } 22 | 23 | 24 | int bottomup(){ 25 | int dp[100][100]={0}; 26 | int n=strlen(s1); 27 | int m=strlen(s2); 28 | for(int i=1;i<=n;i++){ 29 | for(int j=1;j<=m;j++){ 30 | if(s1[i-1]==s2[j-1]){ 31 | dp[i][j]=1+dp[i-1][j-1]; 32 | } 33 | else{ 34 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 35 | } 36 | } 37 | } 38 | return dp[n][m]; 39 | } 40 | 41 | int main(){ 42 | cin>>s1>>s2; 43 | cout< 2 | using namespace std; 3 | int nstaircase(int n,int k){ 4 | // base case 5 | if(n==0){ 6 | return 1; 7 | } 8 | if(n<0){ 9 | return 0; 10 | } 11 | 12 | 13 | 14 | // recursive case 15 | int ans=0; 16 | for(int i=1;i<=k;i++){ 17 | ans+=nstaircase(n-i,k); 18 | 19 | } 20 | return ans; 21 | } 22 | int nstaircasetopdown(int n,int k,int *dp){ 23 | // base case 24 | if(n==0){ 25 | return dp[n]=1; 26 | } 27 | if(n<0){ 28 | return 0; 29 | } 30 | 31 | 32 | 33 | // recursive case 34 | // calculate se pehle check 35 | if(dp[n]!=-1){ 36 | return dp[n]; 37 | 38 | } 39 | 40 | int ans=0; 41 | for(int i=1;i<=k;i++){ 42 | ans+=nstaircase(n-i,k); 43 | 44 | } 45 | return dp[n]=ans; 46 | } 47 | int bottomup(int n,int k){ 48 | int dp[1000]; 49 | // initilization condition 50 | dp[0]=1; 51 | for(int i=1;i<=n;i++){ 52 | int ans=0; 53 | for(int j=1;j<=k;j++){ 54 | if(i>=j){ 55 | ans+=dp[i-j]; 56 | } 57 | } 58 | dp[i]=ans; 59 | } 60 | for(int i=0;i<=n;i++){ 61 | cout<=0){ 73 | dp[i]=2*dp[i-1]-dp[i-1-k]; 74 | } 75 | else{ 76 | dp[i]=2*dp[i-1]; 77 | 78 | } 79 | } 80 | return dp[n]; 81 | } 82 | 83 | int main(){ 84 | int dp[1000]; 85 | memset(dp,-1,sizeof(dp)); 86 | int n,k; 87 | cin>>n>>k; 88 | cout< 2 | using namespace std; 3 | int elephantways(int i,int j){ 4 | // base case 5 | if(i==0&&j==0){ 6 | return 1; 7 | } 8 | 9 | 10 | // recursive case 11 | int ans=0; 12 | // for col sum 13 | for(int k=0;k>i>>j; 88 | int dp[100][100]; 89 | memset(dp,-1,sizeof(dp));//puri array will get initilized with -1 90 | cout< 2 | using namespace std; 3 | int main(){ 4 | 5 | cout<<"value of (5&7) is :"<<(5&7)<>2 is :"<<(a>>2)< 2 | using namespace std; 3 | //macros 4 | #define pi 3.14 5 | #define print cout<< 6 | #define semicolon ; 7 | // 8 | 9 | #define f(n) for(int i=0;iyou have to initialize it at the time of declearrion 26 | // const int a=10; 27 | 28 | // a=60; 29 | 30 | print 2*pi*1< 2 | using namespace std; 3 | int main(){ 4 | int x=2; 5 | int total = ++x + ++x + ++x; 6 | cout<<"value of x is : "< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int ival,fval,step; 6 | cin>>ival>>fval>>step; 7 | 8 | 9 | // int f=ival; //initialization condition 10 | 11 | // while(f<=fval){ //condition check 12 | 13 | 14 | // int cel=(5/9.0)*(f-32); 15 | 16 | // // int cel=((5)*(f-32))/9; 17 | 18 | // cout< 2 | using namespace std; 3 | int main(){ 4 | 5 | // for(int i=1;i<=10;i++){ 6 | // cout<>n; 12 | for (int i = 1; i <=n; ++i) 13 | { 14 | cout< 2 | using namespace std; 3 | int main(){ 4 | 5 | // int a=10; 6 | 7 | // a=a++; 8 | 9 | // a=a--; 10 | 11 | // cout< 2 | using namespace std; 3 | int main(){ 4 | 5 | 6 | 7 | int n; 8 | cin>>n; 9 | for (int i = 0; i <=n; i=i+2) 10 | { 11 | cout< 2 | #include 3 | using namespace std; 4 | int editdistance(char *s1,char *s2,int i,int j,int dp[][100]){ 5 | // base case 6 | // i is length of s1 7 | if(i==0){ 8 | return dp[i][j]=j; 9 | 10 | } 11 | if(j==0){ 12 | return dp[i][j]=i; 13 | } 14 | 15 | 16 | // recursive case 17 | if(dp[i][j]!=-1){ 18 | return dp[i][j]; 19 | } 20 | if(s1[i-1]==s2[j-1]){ 21 | //ignore 22 | return dp[i][j]=editdistance(s1,s2,i-1,j-1,dp); 23 | } 24 | else{ 25 | int op1=editdistance(s1,s2,i-1,j-1,dp); //replace //3 26 | int op2=editdistance(s1,s2,i,j-1,dp);//insert //4 27 | int op3=editdistance(s1,s2,i-1,j,dp);//delete //5 28 | return dp[i][j]=min(op1,min(op2,op3))+1; 29 | } 30 | 31 | } 32 | int main(){ 33 | char s1[1000],s2[1000]; 34 | int dp[100][100]; 35 | memset(dp,-1,sizeof(dp)); 36 | cin>>s1>>s2; 37 | int i=strlen(s1); 38 | int j=strlen(s2); 39 | cout< 2 | #include 3 | using namespace std; 4 | int lis(int *a,int n){ 5 | int dp[1000]; 6 | // because increasing subsequence will have atleat length =1 7 | for(int i=0;i<1000;i++){ 8 | dp[i]=1; 9 | } 10 | for(int i=1;ia[j]){ 13 | dp[i]=max(dp[i],dp[j]+1); 14 | } 15 | } 16 | } 17 | int ans=1; 18 | int i=0; 19 | for(int j=0;jv; 30 | int len=ans; 31 | v.push_back(a[i]); 32 | len--; 33 | for(int j=i-1;j>=0;j--){ 34 | if(dp[j]==len&&v[v.size()-1]>a[j]){ 35 | v.push_back(a[j]); 36 | len--; 37 | if(len==0){ 38 | break; 39 | } 40 | } 41 | } 42 | cout<<"lis will be : "; 43 | for(int i=v.size()-1;i>=0;i--){ 44 | cout< 2 | #include //cpp refrence list 3 | using namespace std; 4 | class Graph{ 5 | public: 6 | list *l; //pointer address of array -->list 7 | int n; 8 | // constructor 9 | Graph(int N){ 10 | n=N; 11 | l=new list[N]; 12 | } 13 | 14 | void addedge(int u,int v,int bidir=true){ 15 | l[u].push_back(v); 16 | if(bidir){ 17 | l[v].push_back(u); 18 | } 19 | } 20 | 21 | 22 | void print(){ 23 | for(int i=0;i"; 25 | for(auto n:l[i]){ 26 | cout<>nn>>ne; 40 | Graph g(nn); 41 | for(int i=0;i>u>>v; 44 | g.addedge(u,v); 45 | 46 | } 47 | g.print(); 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /lecture41/knapsackproblem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/lecture41/knapsackproblem -------------------------------------------------------------------------------- /lecture41/knapsackproblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int knapsack(int *price,int *weight,int capacity,int n,int dp[][100]){ 4 | // base case 5 | if(n==0||capacity==0){ 6 | return dp[n][capacity]=0; 7 | } 8 | // recursive case 9 | if(dp[n][capacity]!=-1){ 10 | return dp[n][capacity]; 11 | } 12 | int op1=INT_MIN,op2=INT_MIN; 13 | // if you are including 14 | if(capacity>=weight[n-1]){ 15 | op1=price[n-1]+knapsack(price,weight,capacity-weight[n-1],n-1,dp); 16 | 17 | } 18 | 19 | 20 | // if you are excluding 21 | op2=0+knapsack(price,weight,capacity,n-1,dp); 22 | 23 | 24 | return dp[n][capacity]=max(op1,op2); 25 | 26 | 27 | } 28 | int bottomup(int *price,int *weight,int tcapacity,int tn){ 29 | int dp[100][100]={0}; 30 | for(int n=1;n<=tn;n++){ 31 | for(int ccap=1;ccap<=tcapacity;ccap++){ 32 | int op1=INT_MIN,op2=INT_MIN; 33 | if(ccap>=weight[n-1]){ 34 | op1=price[n-1]+dp[n-1][ccap-weight[n-1]]; 35 | } 36 | op2=0+dp[n-1][ccap]; 37 | dp[n][ccap]=max(op1,op2); 38 | 39 | 40 | } 41 | } 42 | return dp[tn][tcapacity]; 43 | 44 | } 45 | int main(){ 46 | int price[]={13,13,16}; 47 | int weight[]={7,7,8}; 48 | int n=sizeof(price)/sizeof(int); 49 | int dp[100][100]; 50 | memset(dp,-1,sizeof(dp)); 51 | int capacity=14; 52 | cout< 2 | #include //cpp refrence list 3 | using namespace std; 4 | 5 | int main(){ 6 | list l; 7 | l.push_back(1); 8 | l.push_back(2); 9 | l.push_back(3); 10 | l.push_back(4); 11 | l.push_back(5); 12 | for(auto node: l){ 13 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | #define pb push_back 8 | template 9 | // generic 10 | class Graph{ 11 | // unordered_map >h; 12 | unordered_map >h; 13 | void dfshelper(T src,unordered_map &visited){ 14 | 15 | 16 | visited[src]=true; 17 | cout<key value 37 | cout< visited; 53 | dfshelper(src,visited); 54 | for(auto node :h){ 55 | if(!visited[node.first]){ 56 | dfshelper(node.first,visited); 57 | comp++; 58 | } 59 | } 60 | cout< g; 68 | 69 | 70 | // int ne,nn; 71 | // cin>>nn>>ne; 72 | 73 | // for(int i=0;i>u>>v; 76 | // g.addEdge(u,v); 77 | 78 | // } 79 | 80 | g.addEdge(0, 1); 81 | g.addEdge(0, 4); 82 | g.addEdge(2, 1); 83 | g.addEdge(2, 4); 84 | g.addEdge(3, 4); 85 | g.addEdge(3, 2); 86 | g.addEdge(3, 5); 87 | 88 | g.addEdge(11, 12); 89 | g.addEdge(13, 12); 90 | g.addEdge(14, 12); 91 | // g.printlist(); 92 | 93 | 94 | g.addEdge(114, 115); 95 | g.addEdge(114, 116); 96 | g.addEdge(114, 117); 97 | 98 | g.addEdge(1140, 1150); 99 | 100 | 101 | 102 | 103 | 104 | // g.dfs(0,visited); 105 | g.dfs(0); 106 | 107 | 108 | // cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | #define pb push_back 9 | #define mp make_pair 10 | template 11 | class Graph{ 12 | unordered_map > > h; 13 | public: 14 | void addEdge(T src,T des,int dist,bool bidir=true){ 15 | h[src].pb(mp(des,dist)); 16 | if(bidir){ 17 | h[des].pb(mp(src,dist)); 18 | 19 | } 20 | 21 | } 22 | void printlist(){ 23 | for(auto node:h){ 24 | cout<distance; 34 | for(auto node:h){ 35 | distance[node.first]=INT_MAX; 36 | } 37 | 38 | distance[src]=0; 39 | unordered_map p; 40 | p[src]=src; 41 | set >s; 42 | s.insert(mp(0,src)); 43 | while(!s.empty()){ 44 | 45 | auto node=*(s.begin()); //6,d 46 | s.erase(s.begin()); 47 | int parentDist=node.first; //6 48 | T parent=node.second; //d 49 | for(auto children:h[parent]){ //(,3) 50 | int edgeDist=children.second;//3 51 | if(distance[children.first]>parentDist+edgeDist){ 52 | auto f=s.find(mp(distance[children.first],children.first)); 53 | 54 | if(f!=s.end()){ 55 | s.erase(f); 56 | 57 | } 58 | p[children.first]=parent; 59 | 60 | distance[children.first]=parentDist+edgeDist;// D[C]=3; 61 | s.insert(mp(distance[children.first],children.first));//3,C 62 | 63 | 64 | } 65 | 66 | } 67 | 68 | } 69 | // for(auto node: distance){ 70 | // cout<<"minimum distance of "< g; 94 | g.addEdge("Amritsar", "Agra", 1); 95 | g.addEdge("Amritsar", "Jaipur", 4); 96 | g.addEdge("Delhi", "Jaipur", 2); 97 | g.addEdge("Delhi", "Agra", 1); 98 | g.addEdge("Bhopal", "Agra", 2); 99 | g.addEdge("Bhopal", "Mumbai", 3); 100 | g.addEdge("Jaipur", "Mumbai", 8); 101 | g.printlist(); 102 | cout< 2 | #include 3 | using namespace std; 4 | class node{ 5 | public: 6 | char data; 7 | bool isterminal; 8 | unordered_map h; 9 | // constructor 10 | node(char ch){ 11 | data=ch; 12 | isterminal=false;//by default false kardiya 13 | 14 | } 15 | }; 16 | class Trie{ 17 | node*root; 18 | public: 19 | Trie(){ 20 | root=new node('\0'); 21 | 22 | } 23 | void insert(char *word){ 24 | node*temp=root; 25 | for(int i=0;word[i];i++){ 26 | char ch=word[i]; 27 | // agar exist karta hai -->result 1 28 | //h.count(ch)-->check whether ch currently i is at is present in the hashmap or not 29 | if(temp->h.count(ch)){ 30 | temp=temp->h[ch]; 31 | 32 | } 33 | // agar exist karta hai -->result 0 34 | else{ 35 | temp->h[ch]=new node(ch); 36 | temp=temp->h[ch]; 37 | 38 | } 39 | 40 | } 41 | temp->isterminal=true; 42 | 43 | } 44 | bool search(char *key){ 45 | node*temp=root; 46 | for(int i=0;key[i];i++){ 47 | char ch=key[i]; 48 | if(temp->h.count(ch)){ 49 | temp=temp->h[ch]; 50 | 51 | } 52 | else{ 53 | return false; 54 | 55 | } 56 | 57 | } 58 | return temp->isterminal; 59 | // if(temp->isterminal==true){ 60 | // return true; 61 | // } 62 | // return false; 63 | 64 | } 65 | 66 | 67 | }; 68 | int main(){ 69 | Trie t; 70 | char arr[100]="hello"; 71 | t.insert(arr); 72 | t.insert("hello world"); 73 | t.insert("coder"); 74 | t.insert("coding"); 75 | t.insert("hell"); 76 | while(1){ 77 | char key[100]; 78 | cin.getline(key,100); 79 | // cin>>key; 80 | 81 | 82 | if(t.search(key)){ 83 | cout<<"word is present"< 2 | using namespace std; 3 | int main(){ 4 | // int arr[5]; 5 | // arr[0]=40; 6 | // arr[1]=50; 7 | // arr[2]=60; 8 | // arr[3]=70; 9 | // arr[4]=10; 10 | 11 | // cout<>n; 39 | // //input 40 | // for(int i=0;i>arr[i]; 42 | 43 | // } 44 | // //output 45 | // cout<<"after printing array : "; 46 | // for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | 5 | // int count=0; 6 | // char ch; 7 | // cin>>ch; 8 | // while(ch!='$'){ 9 | // count++; 10 | // cin>>ch; 11 | 12 | // } 13 | // cout<<"The count of characters till $ is "< 2 | using namespace std; 3 | int main(){ 4 | 5 | char ch; 6 | int x=0,y=0; 7 | ch=cin.get(); 8 | while(ch!='\n'){ 9 | if(ch=='N'){ 10 | y++; 11 | 12 | } 13 | else if(ch=='E'){ 14 | x++; 15 | } 16 | else if(ch=='S'){ 17 | y--; 18 | } 19 | else{ 20 | x--; 21 | 22 | } 23 | 24 | ch=cin.get(); 25 | 26 | } 27 | //1st quadrant 28 | if(x>=0&&y>=0){ 29 | //printing east 30 | for(int i=0;i=0&&x<=0){ 44 | for (int i = 0; i < y; ++i) 45 | { 46 | cout<<'N'; 47 | } 48 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | 5 | int n; 6 | cin>>n; 7 | for(int row=1;row<=n;row++){ 8 | char ch='A'; 9 | //charcters in increasing order 10 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | 5 | int numbers=0,alphabets=0,whitespaces=0,specialsymbols=0; 6 | char ch; 7 | ch=cin.get(); 8 | while(ch!='$'){ 9 | if(ch>='0'&&ch<='9'){ 10 | numbers++; 11 | 12 | } 13 | else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){ 14 | alphabets++; 15 | } 16 | else if(ch==' '||ch=='\n'||ch=='\t'){ 17 | whitespaces++; 18 | } 19 | else{ 20 | specialsymbols++; 21 | 22 | } 23 | ch=cin.get(); 24 | } 25 | cout<<"total number of numbers are : "< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int arr[100]; 6 | cout<<"enter the number of elements:"<>n; 11 | for(int i=0;i>arr[i]; 13 | } 14 | cout<<"Array Before sorting"<arr[j+1]){ 25 | //swap; 26 | // with another variable temp 27 | 28 | // int temp=arr[j]; 29 | // arr[j]=arr[j+1]; 30 | // arr[j+1]=temp; 31 | 32 | // without temp 33 | // arr[j]=arr[j]+arr[j+1]; 34 | // arr[j+1]=arr[j]-arr[j+1]; 35 | // arr[j]=arr[j]-arr[j+1]; 36 | 37 | // inbuild swap 38 | 39 | swap(arr[j],arr[j+1]); 40 | 41 | // one line swap 42 | 43 | // b=(a+b)-(a=b); 44 | 45 | } 46 | } 47 | } 48 | cout<<"Array after sorting"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int arr[100]; 6 | cout<<"enter the number of elements:"<>n; 11 | for(int i=0;i>arr[i]; 13 | } 14 | for(int i=0;imax){ 16 | max=arr[i]; 17 | } 18 | 19 | } 20 | cout< 2 | using namespace std; 3 | int main(){ 4 | int arr[100]; 5 | cout<<"enter the number of elements:"<>n; 10 | for(int i=0;i>arr[i]; 12 | } 13 | cout<<"enter key"<>key; 15 | 16 | for(i=0;i 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int arr[100]; 6 | cout<<"enter the number of elements:"<>n; 11 | for(int i=0;i>arr[i]; 13 | } 14 | cout<<"Array Before sorting"<arr[j+1]){ 25 | 26 | 27 | swap(arr[j],arr[j+1]); 28 | cnt++; 29 | 30 | 31 | } 32 | } 33 | if(cnt==0){ 34 | break; 35 | } 36 | } 37 | cout<<"Array after sorting"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int arr[100],min; 6 | cout<<"enter the number of elements:"<>n; 11 | for(int i=0;i>arr[i]; 13 | } 14 | cout<<"Array Before sorting"<arr[j]){ 31 | min=j; 32 | } 33 | 34 | } 35 | swap(arr[min],arr[i]); 36 | } 37 | cout<<"Array after sorting"< 2 | using namespace std; 3 | 4 | void printingdetails(){ 5 | cout<<"hello world"< 2 | using namespace std; 3 | int main(){ 4 | int n,i,j; 5 | cin>>n; 6 | int arr[100]; 7 | for(int i=0;i>arr[i]; 9 | } 10 | cout<<"array Before insertion sort:"<=0&&arr[j]>current;j--){ 21 | arr[j+1]=arr[j]; 22 | } 23 | arr[j+1]=current; 24 | } 25 | //after insertion sort 26 | 27 | cout<<"array after insertion sort:"< 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int n,i,j,maximumsum=INT_MIN; 7 | cin>>n; 8 | int arr[100]; 9 | for(int i=0;i>arr[i]; 11 | } 12 | cout<<"printing array"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n,i,j,maximumsum=INT_MIN; 6 | cin>>n; 7 | int arr[100]; 8 | for(int i=0;i>arr[i]; 10 | } 11 | cout<<"printing array"<maximumsum){ 29 | maximumsum=sum; 30 | 31 | } 32 | // cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | void printingtables(int ival,int fval,int step){ 7 | for(int f=ival;f<=fval;f=f+step){ 8 | 9 | int cel=(5/9.0)*(f-32); 10 | 11 | // int cel=((5)*(f-32))/9; 12 | 13 | cout<>ival>>fval>>step; 25 | printingtables(ival,fval,step); 26 | 27 | // cout<<"enter ival, fval, step"<>ival>>fval>>step; 30 | printingtables(ival,fval,step); 31 | 32 | // cout<<"enter ival, fval, step"<>ival>>fval>>step; 34 | printingtables(ival,fval,step); 35 | 36 | 37 | 38 | 39 | 40 | 41 | return 0; 42 | 43 | 44 | 45 | 46 | } -------------------------------------------------------------------------------- /lecture9/functionfactorial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/lecture9/functionfactorial -------------------------------------------------------------------------------- /lecture9/functionfactorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void factorial(int no); //forward decleration -->used when we write our functions below main 4 | 5 | int main(){ 6 | 7 | 8 | int no; 9 | cin>>no; 10 | 11 | factorial(no); 12 | 13 | 14 | 15 | return 0; 16 | } 17 | 18 | 19 | void factorial(int no){ 20 | int ans=1; 21 | for(int i=1;i<=no;i++){ 22 | ans=ans*i; 23 | } 24 | cout<<"factorial is "< 2 | using namespace std; 3 | 4 | //function 5 | void printingdetails(){ //this is function with 0 arguments 6 | cout<<"hello world"< 2 | using namespace std; 3 | 4 | 5 | int sumoftwonumbers(int a,int b){ 6 | int sum=a+b; 7 | // cout<<"sum is : "<>x>>y; 20 | 21 | int val=sumoftwonumbers(x,y); 22 | 23 | // cout<<"sum is : "< 2 | using namespace std; 3 | 4 | //function decleration 5 | void isevenorodd(int n){ 6 | if(n%2==0){ //function definition 7 | cout<>no; 26 | 27 | // isevenorodd(n); 28 | isevenorodd(no); //function calling/invoking 29 | 30 | // cin>>n; 31 | // isevenorodd(n); 32 | 33 | // cin>>n; 34 | // isevenorodd(n); 35 | 36 | 37 | // cin>>n; 38 | // if(n%2==0){ 39 | // cout<>n; 47 | // if(n%2==0){ 48 | // cout< 2 | using namespace std; 3 | 4 | bool isprimeornot(int n){ 5 | int i=2; 6 | while(i<=n-1){ 7 | if(n%i==0){ 8 | return false; //the number is not prime 9 | 10 | } 11 | i++; 12 | } 13 | 14 | 15 | return true; 16 | 17 | 18 | 19 | // if(i==n){ 20 | // return true; 21 | // } 22 | 23 | 24 | } 25 | int main(){ 26 | 27 | 28 | int n; 29 | cin>>n; 30 | 31 | if(isprimeornot(n)){ 32 | cout<<"number is prime"< 2 | using namespace std; 3 | 4 | // int factorial(int no); 5 | int ncr(int n,int r); 6 | 7 | 8 | 9 | 10 | 11 | int factorial(int no){ 12 | int ans=1; 13 | for(int i=1;i<=no;i++){ 14 | ans=ans*i; 15 | } 16 | // cout<<"factorial is "<>n>>r; 29 | if(n>r){ 30 | int x=ncr(5,2); 31 | 32 | cout<<"value of ncr is "< 2 | using namespace std; 3 | 4 | void generateprimes(int no){ 5 | for(int n=2;n<=no;n++){ 6 | 7 | 8 | int i=2; 9 | while(i<=n-1){ 10 | if(n%i==0){ 11 | // cout<<"not prime"<>no; 36 | generateprimes(no); 37 | 38 | cin>>no; 39 | generateprimes(no); 40 | 41 | 42 | cin>>no; 43 | generateprimes(no); 44 | 45 | 46 | 47 | 48 | cout< 2 | using namespace std; 3 | 4 | 5 | void sumoftwonumbers(int a,int b){ 6 | int sum=a+b; 7 | cout<<"sum is : "<>x>>y; 17 | 18 | sumoftwonumbers(x,y); //it is not necessary to have the same names in the function declartion as they are in function calling 19 | // cout<<"enter two numbers"<>x>>y; 21 | 22 | // sumoftwonumbers(x,y); 23 | 24 | 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /lecture9/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mosina916/CPP-LV-VMC-DEC-2020/f30d1ffc619a5d5ed7f81bb187109ab49a129d72/lecture9/test --------------------------------------------------------------------------------