├── Buy_and_Sell_Stock.cpp ├── Kadane's_Algorithm.cpp ├── Minimize_the_Heights_II.cpp ├── Nodes_in_binary.cpp ├── README.md ├── Rat in a Maze Problem - I.cpp ├── VCS.cpp ├── balanced_parenthesis.cpp ├── binary.cpp ├── bits_prob.cpp ├── build_tree.cpp ├── chocolate_count.cpp ├── chocolate_distribution.cpp ├── circular_linklist.cpp ├── circular_queue.cpp ├── count ones.cpp ├── count_set_bit.cpp ├── dectobin.cpp ├── dnfsort.cpp ├── doubly_linklist.cpp ├── even_or_not.cpp ├── example of refrence.cpp ├── function_overloading.cpp ├── function_overloding.cpp ├── getbit.cpp ├── gld.cpp ├── height_of_binary.cpp ├── infinix_to_postfix.cpp ├── infinix_to_prefix.cpp ├── inheritance.cpp ├── inorder.cpp ├── inversions_problem.cpp ├── lagrange.c ├── length_of_sentance.cpp ├── linked_list.cpp ├── matrix_multp.cpp ├── maxima_minima.cpp ├── maximum_subarrays.cpp ├── mergesort.cpp ├── missing_number.cpp ├── num_in_decr_order.cpp ├── num_to_str.cpp ├── occurence.cpp ├── operator_overloading.cpp ├── opps.cpp ├── opps1.cpp ├── pi_to_3.14.cpp ├── pointer_function.cpp ├── postfixevaluation.cpp ├── postorder.cpp ├── postorder_to_tree.cpp ├── power using recursion.cpp ├── power.cpp ├── prefixevaluation.cpp ├── preorder.cpp ├── ptr_function.cpp ├── pythagorean triplet.cpp ├── queue.cpp ├── refrence.cpp ├── remove duplicates key.cpp ├── reverse_a_sentence.cpp ├── reverse_array.cpp ├── reverse_str.cpp ├── reverse_using_stack.cpp ├── rotated_sorted_array.cpp ├── setbit.cpp ├── size of int.cpp ├── sorting.cpp ├── stack.cpp ├── star printing.cpp ├── subset.cpp ├── substr.cpp ├── sum_of_nodes.cpp ├── tempCodeRunnerFile.cpp ├── tower_of_hanoi.cpp ├── transpose of matrix.cpp ├── vector.cpp ├── virtual_function.cpp ├── wavesor.cpp └── wavesort.cpp /Buy_and_Sell_Stock.cpp: -------------------------------------------------------------------------------- 1 | #include"bits/stdc++.h" 2 | using namespace std; 3 | /* 4 | Example 1: 5 | 6 | Input: prices = [7,1,5,3,6,4] 7 | Output: 5 8 | Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. 9 | Constraints: 10 | 1 <= prices.length <= 10^5 11 | 0 <= prices[i] <= 10^4*/ 12 | int buystock(int prices[],int n){ 13 | int buy=prices[0], max_profit=0; 14 | for(int i=0; iprices[i]){ 16 | buy = prices[i]; 17 | } 18 | max_profit= max((prices[i]-buy), max_profit); 19 | } 20 | return max_profit; 21 | } 22 | 23 | int main(){ 24 | int prices[] = {7,1,5,3,6,4,19 }; 25 | int n= sizeof(prices)/sizeof(prices[0]); 26 | int max_profit=buystock(prices, n); 27 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | class Solution{ 8 | public: 9 | // arr: input array 10 | // n: size of array 11 | //Function to find the sum of contiguous subarray with maximum sum. 12 | long long maxSubarraySum(int arr[], int n){ 13 | int mx=INT_MIN; 14 | int sum=0; 15 | for(int i=0;imx){ 18 | mx=sum; 19 | } 20 | if(sum<0){ 21 | sum=0; 22 | } 23 | } 24 | return mx; 25 | 26 | 27 | // Your code here 28 | 29 | } 30 | }; 31 | 32 | //{ Driver Code Starts. 33 | 34 | int main() 35 | { 36 | int t,n; 37 | 38 | cin>>t; //input testcases 39 | while(t--) //while testcases exist 40 | { 41 | 42 | cin>>n; //input size of array 43 | 44 | int a[n]; 45 | 46 | for(int i=0;i>a[i]; //inputting elements of array 48 | 49 | Solution ob; 50 | 51 | cout << ob.maxSubarraySum(a, n) << endl; 52 | } 53 | } 54 | 55 | // } Driver Code Ends -------------------------------------------------------------------------------- /Minimize_the_Heights_II.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | // User function template for C++ 9 | 10 | class Solution { 11 | public: 12 | int getMinDiff(int arr[], int n, int k) { 13 | sort(arr, arr+n); 14 | int mn=arr[0]; 15 | int mx= arr[n-1]; 16 | int ans= arr[n-1]-arr[0]; 17 | for(int i=1;i> t; 36 | while (t--) { 37 | int n, k; 38 | cin >> k; 39 | cin >> n; 40 | int arr[n]; 41 | for (int i = 0; i < n; i++) { 42 | cin >> arr[i]; 43 | } 44 | Solution ob; 45 | auto ans = ob.getMinDiff(arr, n, k); 46 | cout << ans << "\n"; 47 | } 48 | return 0; 49 | } 50 | // } Driver Code Ends -------------------------------------------------------------------------------- /Nodes_in_binary.cpp: -------------------------------------------------------------------------------- 1 | #include"bits/stdc++.h" 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node* left; 7 | struct Node* right; 8 | Node(int val){ 9 | data= val; 10 | left=NULL; 11 | right=NULL; 12 | } 13 | }; 14 | 15 | int countnodes(Node* root){ 16 | 17 | if(root ==NULL){ 18 | return 0; 19 | } 20 | return countnodes(root->left)+countnodes(root->right)+1; 21 | } 22 | int main(){ 23 | Node* node= new Node(1); 24 | node->left=new Node(2); 25 | node->right=new Node(3); 26 | node->right->left=new Node(3); 27 | node->right->right=new Node(3); 28 | node->left->left=new Node(2); 29 | node->left->right=new Node(2); 30 | node->left->left->left=new Node(2); 31 | node->left->left->right=new Node(2); 32 | cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | // User function template for C++ 10 | 11 | // User function template for C++ 12 | 13 | class Solution{ 14 | private: 15 | bool safe(int x, int y, int n,vector> v,vector>&m){ 16 | if((x>=0 &&x=0&&y>&m, int n ,vector&s,int x, int y, 22 | vector> v, string path){ 23 | if(x==n-1&&y==n-1) { 24 | s.push_back(path); 25 | return; 26 | } 27 | v[x][y]=1; 28 | int nx=x+1; 29 | int ny=y; 30 | if(safe(nx,ny,n,v,m)){ 31 | path.push_back('D'); 32 | solve(m,n,s,nx,ny,v,path); 33 | path.pop_back(); 34 | } 35 | nx=x; 36 | ny=y-1; 37 | if(safe(nx,ny,n,v,m)){ 38 | path.push_back('L'); 39 | solve(m,n,s,nx,ny,v,path); 40 | path.pop_back(); 41 | } 42 | nx=x; 43 | ny=y+1; 44 | if(safe(nx,ny,n,v,m)){ 45 | path.push_back('R'); 46 | solve(m,n,s,nx,ny,v,path); 47 | path.pop_back(); 48 | } 49 | nx=x-1; 50 | ny=y; 51 | if(safe(nx,ny,n,v,m)){ 52 | path.push_back('U'); 53 | solve(m,n,s,nx,ny,v,path); 54 | path.pop_back(); 55 | } 56 | v[x][y]=0; 57 | 58 | } 59 | public: 60 | vector findPath(vector>&m, int n ) { 61 | vectors; 62 | if(m[0][0]==0) return s; 63 | int x=0; 64 | int y=0; 65 | 66 | vector> v=m; 67 | for(int i=0;i> t; 88 | while (t--) { 89 | int n; 90 | cin >> n; 91 | vector> m(n, vector (n,0)); 92 | for (int i = 0; i < n; i++) { 93 | for (int j = 0; j < n; j++) { 94 | cin >> m[i][j]; 95 | } 96 | } 97 | Solution obj; 98 | vector result = obj.findPath(m, n); 99 | sort(result.begin(), result.end()); 100 | if (result.size() == 0) 101 | cout << -1; 102 | else 103 | for (int i = 0; i < result.size(); i++) cout << result[i] << " "; 104 | cout << endl; 105 | } 106 | return 0; 107 | } 108 | // } Driver Code Endss -------------------------------------------------------------------------------- /VCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"bits/stdc++.h" 3 | using namespace std; 4 | 5 | int main() { 6 | //int t; 7 | //cin>>t; 8 | //while(t--){ 9 | int n,m,k; 10 | cin>>n>>m>>k; 11 | int arr[m], ar[k]; 12 | for(int i=0;i>arr[i]; 13 | for(int i=0;i>ar[i]; 14 | int array[m+k]; 15 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | bool isvalid(string s){ 5 | int n= s.size(); 6 | stackst; 7 | bool ans = true; 8 | for(int i=0; ileft= new Node(2); 18 | root->right= new Node(3); 19 | root->left->left=new Node(4); 20 | root->left->right= new Node(5); 21 | root->right->left=new Node(6); 22 | root->right->right= new Node(7); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /bits_prob.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int unique(int arr[], int n){ 4 | int sum=0; 5 | for(int i=0; i>n; 15 | cout<end){ 25 | return NULL; 26 | } 27 | int val= preorder[idx]; 28 | idx++; 29 | 30 | Node* node= new Node(val); 31 | if(start==end){ 32 | return node; 33 | } 34 | int odr= search(inorder, start, end, val); 35 | node->left=builttree(preorder, inorder,start, odr-1); 36 | node->right= builttree(preorder, inorder, odr+1, end); 37 | } 38 | void preor(struct Node* root){ 39 | if(root==NULL){ 40 | return; 41 | } 42 | cout<data<<" "; 43 | preor(root->left); 44 | preor(root->right); 45 | } 46 | int main(){ 47 | int preorder[]= {1,2,4,3,5}; 48 | int inorder[]={4,2,1,5,3}; 49 | Node* root = builttree(preorder, inorder, 0, 4); 50 | preor(root); 51 | } -------------------------------------------------------------------------------- /chocolate_count.cpp: -------------------------------------------------------------------------------- 1 | //chocolate 2 | #include 3 | using namespace std; 4 | int main() 5 | { int n,i, chock=0, wrapp; 6 | cout<<"how much money u have"; 7 | cin>>n; 8 | while((n-1)>=0) 9 | { 10 | chock++; 11 | } 12 | wrapp= chock; 13 | 14 | while( wrapp>=3){ 15 | chock = chock+ wrapp/3; 16 | wrapp = wrapp/3+ wrapp%3; 17 | } 18 | cout<dif){ 15 | min=dif; 16 | } 17 | } 18 | return min; 19 | } 20 | int main(){ 21 | int m; 22 | cout<<"enter number of student : "; 23 | cin>>m; 24 | int arr[]={ 12, 4, 7, 9, 2, 23, 25, 41, 30, 40, 28, 42, 30, 44, 48, 43, 50}; 25 | //{5,3,8,1,5,2,7,2,8,3,7}; 26 | int n = sizeof(arr)/sizeof(arr[0]); 27 | cout< 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node* next; 7 | 8 | Node(int data){ 9 | this->data=data; 10 | this->next=NULL; 11 | } 12 | ~Node(){ 13 | if(this->next!=NULL){ 14 | delete next; 15 | next=NULL; 16 | } 17 | } 18 | 19 | }; 20 | void insertNode(Node* &tail,int element, int data){ 21 | if(tail==NULL){ 22 | Node* newNode=new Node(data); 23 | tail=newNode; 24 | newNode->next=newNode; 25 | } 26 | else{ 27 | Node* curr=tail; 28 | while(curr->data != element) curr=curr->next; 29 | Node* temp=new Node(data); 30 | temp->next=curr->next; 31 | curr->next=temp; 32 | } 33 | 34 | } 35 | void print(Node* &tail){ 36 | Node* curr=tail; 37 | do 38 | { 39 | cout<data<<" "; 40 | curr=curr->next; 41 | } while (curr->next != tail); 42 | cout< 2 | #include"bits/stdc++.h" 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main(){ 9 | 10 | } -------------------------------------------------------------------------------- /count ones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int countones(int n){ 4 | int count=0; 5 | while(n){ 6 | count++; 7 | n=n&(n-1); 8 | } 9 | return count; 10 | } 11 | int main(){ 12 | int n; 13 | cin>>n; 14 | cout< 3 | using namespace std; 4 | int dectobin(int n){ 5 | int count=0; 6 | int number =1; 7 | while (n>0) 8 | { 9 | int a = n%2; 10 | count= count+a; 11 | n= n/2; 12 | count *= 10; 13 | } 14 | return count/10; 15 | 16 | } 17 | int32_t main(){ 18 | int n; 19 | cin>>n; 20 | cout< 3 | using namespace std; 4 | void swap(int arr[], int a, int b){ 5 | int temp; 6 | temp = arr[a]; 7 | arr[a]=arr[b]; 8 | arr[b]= temp; 9 | 10 | } 11 | void dnfsort(int arr[], int n){ 12 | int i=0; 13 | int mid=0; 14 | int r=n-1; 15 | while(mid<=r){ 16 | if(arr[mid]==0){ 17 | swap(arr, i, mid); 18 | i++; mid++; 19 | } 20 | else if(arr[mid]==1){ 21 | mid++; 22 | } 23 | else{ 24 | swap(arr,mid,r); 25 | r--; 26 | } 27 | } 28 | } 29 | int main(){ 30 | cout<<"inter size of array: "; 31 | int n; 32 | cin>>n; 33 | int arr[n]; 34 | for(int i=0; i>arr[i]; 36 | 37 | //int arr[]= {1,1,0,0,1,2,2,0}; 38 | dnfsort(arr,n); 39 | for(int i=0; i<8;i++){ 40 | cout< 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node* prev; 7 | Node* next; 8 | Node(int data){ 9 | this->data=data; 10 | this->prev=NULL; 11 | this->next=NULL; 12 | } 13 | ~Node(){ 14 | if(next != NULL) { 15 | delete next; 16 | next=NULL; 17 | } 18 | } 19 | }; 20 | void print(Node* head){ 21 | Node* temp=head; 22 | while (temp!=NULL) 23 | { 24 | cout<data<<" "; 25 | temp=temp->next; 26 | } 27 | cout<next=head; 33 | head->prev=temp; 34 | head=temp; 35 | } 36 | void insertAtTail(Node* &tail, int data){ 37 | Node* temp=new Node(data); 38 | tail->next=temp; 39 | temp->prev=tail; 40 | tail=temp; 41 | } 42 | void insertAtPosition(Node* &tail,Node* &head, int position, int data){ 43 | if(position==1) { 44 | insertAtHead(head, data); 45 | return; 46 | } 47 | Node* temp=head; 48 | Node* insert=new Node(data); 49 | position--; 50 | while(position--){ 51 | temp=temp->next; 52 | } 53 | if(temp->next==NULL) 54 | { 55 | insertAtTail(tail, data); 56 | return; 57 | } 58 | insert->next=temp->next; 59 | temp->next->prev=insert; 60 | temp->next=insert; 61 | insert->prev=temp; 62 | 63 | 64 | } 65 | void DeleteatHead(Node* &head,int position){ 66 | if(position==1){ 67 | Node* temp=head; 68 | temp->next->prev=NULL; 69 | head=temp->next; 70 | temp->next=NULL; 71 | delete temp; 72 | } 73 | else 74 | { 75 | Node* curr=head; 76 | Node* prev=NULL; 77 | position--; 78 | while (position--) 79 | { 80 | prev=curr; 81 | curr=curr->next; 82 | } 83 | curr->prev=NULL; 84 | prev->next=curr->next; 85 | curr->next=NULL; 86 | delete curr; 87 | 88 | 89 | } 90 | 91 | 92 | } 93 | int main(){ 94 | Node* ans=new Node(10); 95 | Node* head=ans; 96 | Node* tail=ans; 97 | print(head); 98 | insertAtHead(head,20); 99 | print(head); 100 | insertAtTail(tail, 50); 101 | // print(tail); 102 | print(head); 103 | insertAtPosition(tail,head, 2,30); 104 | print(head); 105 | insertAtPosition(tail,head, 2,70); 106 | print(head); 107 | DeleteatHead(head,6); 108 | print(head); 109 | } -------------------------------------------------------------------------------- /even_or_not.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | float s,a,b,c; 9 | cin>>s>>a>>b>>c; 10 | float x= s + (s*c)/100; 11 | cout<=a && x<= b){ 13 | cout<<"YES"< 3 | using namespace std; 4 | int main() 5 | { 6 | char *p = "ram"; 7 | char *q = p; 8 | cout << p << endl << q << endl; 9 | q = "ytre"; 10 | cout << p << endl << q << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /function_overloading.cpp: -------------------------------------------------------------------------------- 1 | #include"bits/stdc++.h" 2 | using namespace std; 3 | class fun_overloading{ 4 | public: 5 | void fun(){ 6 | cout<<"i am function with no arguments"< 2 | using namespace std; 3 | class function_overloding{ 4 | public: 5 | void fun(){ 6 | cout<<"function with no arguments"< 2 | using namespace std; 3 | int getbit(int n, int position){ 4 | return ((n &(1<>n>>position; 9 | cout<>a>>b; 10 | cout<<__gcd(a,b)<left); 20 | int heightr= hightofnode(root->right); 21 | return max(heightl, heightr)+1; 22 | 23 | } 24 | int main(){ 25 | Node* node= new Node(1); 26 | node->left=new Node(2); 27 | node->right=new Node(3); 28 | node->right->left=new Node(3); 29 | node->right->right=new Node(3); 30 | node->left->left=new Node(2); 31 | node->left->right=new Node(2); 32 | node->left->left->left=new Node(2); 33 | node->left->left->right=new Node(2); 34 | cout< 2 | #include 3 | using namespace std; 4 | int prec(char c){ 5 | if(c=='^'){ 6 | return 3; 7 | } 8 | else if(c=='*'|| c=='/'){ 9 | return 2; 10 | } 11 | else if(c=='+'|| c=='-'){ 12 | return 1; 13 | } 14 | else 15 | return -1; 16 | } 17 | string infinixtopostfix(string s){ 18 | stackst; 19 | string res; 20 | for(int i=0; i='a'&&s[i]<='z')||( s[i]>='A'&& s[i]<='Z')){ 22 | res+=s[i]; 23 | } 24 | else if(s[i]=='('){ 25 | st.push(s[i]); 26 | } 27 | else if(s[i]==')'){ 28 | while(!st.empty()&& st.top()!='('){ 29 | res+=st.top(); 30 | st.pop(); 31 | 32 | } 33 | if(!st.empty()){ 34 | st.pop(); 35 | } 36 | } 37 | else{ 38 | while(!st.empty()&&prec(st.top())>prec(s[i])){ 39 | res+=st.top(); 40 | st.pop(); 41 | } 42 | st.push(s[i]); 43 | } 44 | } 45 | while(!st.empty()){ 46 | res+=st.top(); 47 | st.pop(); 48 | } 49 | return res; 50 | } 51 | int main(){ 52 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | int prec(char c){ 6 | if(c=='^'){ 7 | return 3; 8 | } 9 | else if(c=='*'|| c=='/'){ 10 | return 2; 11 | } 12 | else if(c=='+'|| c=='-'){ 13 | return 1; 14 | } 15 | else 16 | return -1; 17 | } 18 | string infinixtopostfix(string s){ 19 | reverse(s.begin(), s.end()); 20 | stackst; 21 | string res; 22 | for(int i=0; i='a'&&s[i]<='z')||( s[i]>='A'&& s[i]<='Z')){ 24 | res+=s[i]; 25 | } 26 | else if(s[i]==')'){ 27 | st.push(s[i]); 28 | } 29 | else if(s[i]=='('){ 30 | while(!st.empty()&& st.top()!=')'){ 31 | res+=st.top(); 32 | st.pop(); 33 | 34 | } 35 | if(!st.empty()){ 36 | st.pop(); 37 | } 38 | } 39 | else{ 40 | while(!st.empty()&&prec(st.top())>prec(s[i])){ 41 | res+=st.top(); 42 | st.pop(); 43 | } 44 | st.push(s[i]); 45 | } 46 | } 47 | while(!st.empty()){ 48 | res+=st.top(); 49 | st.pop(); 50 | } 51 | reverse(res.begin(), res.end()); 52 | return res; 53 | } 54 | int main(){ 55 | cout< 2 | using namespace std; 3 | class a{ 4 | public: 5 | void fun(){ 6 | cout<<"inheritance"; 7 | } 8 | 9 | }; 10 | class b: public a{ 11 | void fun(){ 12 | cout<<"single inheritance"; 13 | } 14 | }; 15 | int main(){ 16 | a op; 17 | op.fun(); 18 | 19 | 20 | } -------------------------------------------------------------------------------- /inorder.cpp: -------------------------------------------------------------------------------- 1 | #include"bits/stdc++.h" 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node* left; 7 | struct Node* right; 8 | Node(int val){ 9 | data= val; 10 | left = NULL; 11 | right= NULL; 12 | 13 | } 14 | }; 15 | void inorder(struct Node* root) 16 | { 17 | if(root==NULL){ 18 | return; 19 | } 20 | inorder(root->left); 21 | cout<data<<" "; 22 | inorder(root->right); 23 | } 24 | int main(){ 25 | struct Node* root = new Node(1); 26 | root->left= new Node(2); 27 | root->right= new Node(3); 28 | root->left->left=new Node(4); 29 | root->left->right= new Node(5); 30 | root->right->left=new Node(6); 31 | root->right->right= new Node(7); 32 | inorder(root); 33 | return 0; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /inversions_problem.cpp: -------------------------------------------------------------------------------- 1 | //function 2 | #include 3 | using namespace std; 4 | int dectobin(int n){ 5 | int count=0; 6 | int number =1; 7 | while (n>0) 8 | { 9 | int a = n%2; 10 | count= count+a; 11 | n= n/2; 12 | count *= 10; 13 | } 14 | return count/10; 15 | 16 | } 17 | int32_t main(){ 18 | int n; 19 | cin>>n; 20 | cout< 2 | #include 3 | 4 | void main() 5 | { 6 | float x[100], y[100], xp, yp=0, p; 7 | int i,j,n; 8 | clrscr(); 9 | /* Input Section */ 10 | printf("Enter number of data: "); 11 | scanf("%d", &n); 12 | printf("Enter data:\n"); 13 | for(i=1;i<=n;i++) 14 | { 15 | printf("x[%d] = ", i); 16 | scanf("%f", &x[i]); 17 | printf("y[%d] = ", i); 18 | scanf("%f", &y[i]); 19 | } 20 | printf("Enter interpolation point: "); 21 | scanf("%f", &xp); 22 | /* Implementing Lagrange Interpolation */ 23 | for(i=1;i<=n;i++) 24 | { 25 | p=1; 26 | for(j=1;j<=n;j++) 27 | { 28 | if(i!=j) 29 | { 30 | p = p* (xp - x[j])/(x[i] - x[j]); 31 | } 32 | } 33 | yp = yp + p * y[i]; 34 | } 35 | printf("Interpolated value at %.3f is %.3f.", xp, yp); 36 | getch(); 37 | } -------------------------------------------------------------------------------- /length_of_sentance.cpp: -------------------------------------------------------------------------------- 1 | //max length of any sentance 2 | #include"bits/stdc++.h" 3 | using namespace std; 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | cin.ignore(); 8 | char ar[n+1]; 9 | cin.getline(ar, n); 10 | cin.ignore(); 11 | int i=0, max=0, cl=0; 12 | int arr=0, st=0; 13 | while(1){ 14 | 15 | if(ar[i]==' ' || ar[i]=='\0') 16 | { 17 | if(cl>=max) 18 | { 19 | max= cl; 20 | st= arr; 21 | } 22 | cl=0; 23 | arr = i+1; 24 | } 25 | else 26 | { 27 | cl++; 28 | } 29 | if(ar[i]=='\0') 30 | break; 31 | i++; 32 | 33 | } 34 | cout< 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node* next; 7 | node(int val){ 8 | data = val; 9 | next=NULL; 10 | 11 | } 12 | 13 | }; 14 | void inserttolast(node* &head, int val){ 15 | node* n= new node(val); 16 | if(head==NULL){ 17 | head=n; 18 | return; 19 | } 20 | node* temp=head; 21 | while(temp->next!= NULL){ 22 | temp=temp->next; 23 | } 24 | temp->next=n; 25 | } 26 | void display(node* head){ 27 | node* temp=head; 28 | while(temp!=NULL){ 29 | cout<data<<" "; 30 | temp = temp->next; 31 | 32 | } 33 | cout<data==key){ 39 | return true; 40 | } 41 | temp = temp->next; 42 | 43 | } 44 | 45 | return false; 46 | } 47 | void inserttohead(node* &head, int val){ 48 | node* n= new node(val); 49 | n->next = head; 50 | head=n; 51 | 52 | } 53 | int main(){ 54 | node* head= NULL; 55 | inserttolast(head,2); 56 | inserttolast(head,7); 57 | inserttolast(head,9); 58 | display(head); 59 | cout< arr[1]) 23 | { 24 | minmax.max = arr[0]; 25 | minmax.min = arr[1]; 26 | } 27 | else 28 | { 29 | minmax.max = arr[1]; 30 | minmax.min = arr[0]; 31 | } 32 | 33 | for(i = 2; i < n; i++) 34 | { 35 | if (arr[i] > minmax.max) 36 | minmax.max = arr[i]; 37 | 38 | else if (arr[i] < minmax.min) 39 | minmax.min = arr[i]; 40 | } 41 | return minmax; 42 | } 43 | int main() 44 | { 45 | int arr[] = {1,2,3,4,5,6,7,8,9}; 46 | int arr_size = 9; 47 | 48 | struct Pair minmax = getMinMax(arr, arr_size); 49 | 50 | cout << "Minimum element is " 51 | << minmax.min << endl; 52 | cout << "Maximum element is " 53 | << minmax.max; 54 | 55 | return 0; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /maximum_subarrays.cpp: -------------------------------------------------------------------------------- 1 | #include"bits/stdc++.h" 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int arr[n]; 7 | for (int i = 0; i < n; i++) 8 | { 9 | cin>>arr[i]; 10 | } 11 | int curr=0, max1=0; 12 | for (int i = 0; i < n; i++) 13 | { 14 | curr=0; 15 | for (int j = i;j 2 | using namespace std; 3 | void display(int *array, int size) { 4 | for(int i = 0; i> n; 53 | int arr[n]; //create an array with given number of elements 54 | cout << "Enter elements:" << endl; 55 | for(int i = 0; i> arr[i]; 57 | } 58 | cout << "Array before Sorting: "; 59 | display(arr, n); 60 | mergeSort(arr, 0, n-1); //(n-1) for last index 61 | cout << "Array after Sorting: "; 62 | display(arr, n); 63 | } -------------------------------------------------------------------------------- /missing_number.cpp: -------------------------------------------------------------------------------- 1 | #include"bits/stdc++.h" 2 | using namespace std; 3 | int megasort(int arr[], int n){ 4 | 5 | } 6 | int missing(int arr[],int n, int k){ 7 | megasort(arr, n); 8 | return arr[n-k-1]; 9 | 10 | } 11 | int main(){ 12 | int n, k; 13 | cin>>n; 14 | cout<<"enter k: "; 15 | cin>>k; 16 | int arr[n]; 17 | for(int i=0;i>arr[i]; 19 | } 20 | missing(arr, n,k); 21 | } -------------------------------------------------------------------------------- /num_in_decr_order.cpp: -------------------------------------------------------------------------------- 1 | /* print number till n 2 | a. decrasing ORDER 3 | b. incresing order 4 | */ 5 | #include"bits/stdc++.h" 6 | using namespace std; 7 | int dodr(int n){ 8 | if(n==0) 9 | { 10 | return 1; 11 | } 12 | cout<< n; 13 | int x = dodr(n-1); 14 | // cout<< n; 15 | 16 | } 17 | int main(){ 18 | int n,m; 19 | return dodr(9); 20 | } -------------------------------------------------------------------------------- /num_to_str.cpp: -------------------------------------------------------------------------------- 1 | //biggest number from numeric string 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main(){ 7 | string s; 8 | cout<<"enter a numeric string"<>s; 10 | sort(s.begin(), s.end(), greater()); 11 | cout<<"greatest number is\t"; 12 | cout< 2 | using namespace std; 3 | class complex{ 4 | private : 5 | int imag, real; 6 | public: 7 | complex(int r=0, int i=0){ 8 | real =r; 9 | imag = i; 10 | } 11 | complex operator + (complex const &obj){ 12 | complex res; 13 | res.real = real + obj.real; 14 | res.imag = imag + obj.imag; 15 | return res; 16 | } 17 | void display(){ 18 | cout< 2 | using namespace std; 3 | class student 4 | { 5 | public: 6 | string name; 7 | int age; 8 | bool gender; 9 | void printinfo(){ 10 | cout<<"name: "; 11 | cout<>arr[i].name; 23 | cin>>arr[i].age; 24 | cin>>arr[i].gender; 25 | } 26 | for (int i=0; i<3;i++){ 27 | arr[i].printinfo(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /opps1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class student{ 4 | private: 5 | string id; 6 | public: 7 | string name; 8 | int age; 9 | bool gender; 10 | void setname(string s){ 11 | id =s; 12 | } 13 | }; 14 | int main(){ 15 | string s; 16 | student iem; 17 | 18 | iem.name="rocky"; 19 | iem.age =19; 20 | iem.gender=0; 21 | //iem.id='gstwh'; 22 | 23 | student arr[3]; 24 | for(int i=0;i<3;i++){ 25 | /*cin>>arr[i].age; 26 | cin>>arr[i].name; 27 | cin>>arr[i].gender;*/ 28 | cin>>s; 29 | arr[i].setname(s); 30 | 31 | } 32 | for(int i=0;i<3;i++){ 33 | cout< 3 | using namespace std; 4 | void replacei(string s){ 5 | if(s.length()==0){ 6 | return; 7 | } 8 | if(s[0]=='p' && s[1]=='i') 9 | { 10 | cout<<"3.14"; 11 | replacei(s.substr(2)); 12 | } 13 | else{ 14 | cout< 3 | using namespace std; 4 | void increment(int *x){ 5 | 6 | *x= *x+1; 7 | //cout< 2 | #include 3 | #include 4 | using namespace std; 5 | int postfix(string s){ 6 | stack st; 7 | for(int i=0;i='0'&&s[i]<='9'){ 9 | st.push(s[i]-'0'); 10 | } 11 | else{ 12 | int op2=st.top(); 13 | st.pop(); 14 | int op1=st.top(); 15 | st.pop(); 16 | switch (s[i]) 17 | { 18 | case '+': 19 | st.push(op1+op2); 20 | break; 21 | case '-': 22 | st.push(op1-op2); 23 | break; 24 | case '*': 25 | st.push(op1*op2); 26 | break; 27 | case '/': 28 | st.push(op1/op2); 29 | break; 30 | case '^': 31 | st.push(pow(op1,op2)); 32 | break; 33 | 34 | 35 | } 36 | } 37 | } 38 | return st.top(); 39 | } 40 | int main(){ 41 | cout<left); 20 | postorder(root->right); 21 | cout<data<<" "; 22 | } 23 | int main(){ 24 | struct Node* root = new Node(1); 25 | root->left= new Node(2); 26 | root->right= new Node(3); 27 | root->left->left=new Node(4); 28 | root->left->right= new Node(5); 29 | root->right->left=new Node(6); 30 | root->right->right= new Node(7); 31 | postorder(root); 32 | return 0; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /postorder_to_tree.cpp: -------------------------------------------------------------------------------- 1 | #include"bits/stdc++.h" 2 | using namespace std; 3 | /* 4 | struct Node 5 | { 6 | int data; 7 | struct Node* left; 8 | struct Node* right; 9 | Node(int val){ 10 | data= val; 11 | left= NULL; 12 | right=NULL; 13 | } 14 | };*/ 15 | using namespace std; 16 | struct Node 17 | { 18 | int data; 19 | struct Node* left; 20 | struct Node* right; 21 | Node(int val){ 22 | data = val; 23 | left = NULL; 24 | right= NULL; 25 | 26 | } 27 | }; 28 | 29 | int search(int inorder[], int start, int end, int val){ 30 | for(int i=0; i<=end; i++){ 31 | if(inorder[i]==val) 32 | return i; 33 | } 34 | return -1; 35 | } 36 | 37 | Node* builttree(int postorder[], int inorder[], int start,int end ){ 38 | static int idx=4; 39 | if(start>end){ 40 | return NULL; 41 | } 42 | int val= postorder[idx]; 43 | idx--; 44 | Node* curr= new Node(val); 45 | if(start==end){ 46 | return curr; 47 | } 48 | int pos= search(inorder, start, end, val); 49 | curr->right=builttree(postorder, inorder, pos+1, end); 50 | curr->left= builttree(postorder, inorder, start, pos-1); 51 | return curr; 52 | } 53 | void postprint( Node* root){ 54 | if(root==NULL){ 55 | return; 56 | } 57 | postprint(root->left); 58 | cout<data<<" "; 59 | 60 | postprint(root->right); 61 | 62 | } 63 | int main(){ 64 | int posorder[]= {4,2,5,3,1}; 65 | int inorder[]={4,2,1,5,3}; 66 | Node* root = builttree(posorder, inorder, 0, 4); 67 | postprint(root); 68 | } 69 | -------------------------------------------------------------------------------- /power using recursion.cpp: -------------------------------------------------------------------------------- 1 | #include"bits/stdc++.h" 2 | #include 3 | using namespace std; 4 | int power(int x,int n){ 5 | if(n==0) return 1; 6 | else x*=power(x,n-1); 7 | return x; 8 | } 9 | int main(){ 10 | int x,n; 11 | cin>>x>>n; 12 | cout< 2 | using namespace std; 3 | int pow2(int n){ 4 | return (n&&!(n&(n-1))); 5 | } 6 | int main(){ 7 | int n; 8 | cin>>n; 9 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | int prefixevalution(string s){ 6 | stack st; 7 | for(int i=s.length()-1;i>=0;i--){ 8 | if(s[i]>='0'&&s[i]<='9'){ 9 | st.push(s[i]-'0'); 10 | } 11 | else{ 12 | int op1=st.top(); 13 | st.pop(); 14 | int op2=st.top(); 15 | st.pop(); 16 | switch (s[i]) 17 | { 18 | case '+': 19 | st.push(op1+op2); 20 | break; 21 | case '-': 22 | st.push(op1-op2); 23 | break; 24 | case '*': 25 | st.push(op1*op2); 26 | break; 27 | case '/': 28 | st.push(op1/op2); 29 | break; 30 | case '^': 31 | st.push(pow(op1,op2)); 32 | break; 33 | 34 | 35 | } 36 | } 37 | } 38 | return st.top(); 39 | } 40 | int main(){ 41 | cout<data<<" "; 20 | preorder(root->left); 21 | preorder(root->right); 22 | } 23 | 24 | int main(){ 25 | struct Node* root = new Node(1); 26 | root->left= new Node(2); 27 | root->right= new Node(3); 28 | root->left->left=new Node(4); 29 | root->left->right= new Node(5); 30 | root->right->left=new Node(6); 31 | root->right->right= new Node(7); 32 | preorder(root); 33 | return 0; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /ptr_function.cpp: -------------------------------------------------------------------------------- 1 | //pointer function 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main(){ 8 | string s; 9 | cout <<"enter a word"; 10 | cin>>s; 11 | 12 | 13 | } -------------------------------------------------------------------------------- /pythagorean triplet.cpp: -------------------------------------------------------------------------------- 1 | //pythagorean triplet 2 | #include 3 | using namespace std; 4 | bool check(int x, int y, int z){ 5 | int b, c; 6 | int a= max(x, (max (y, z))); 7 | 8 | if(a==x){ 9 | b=y; 10 | c=z; 11 | } 12 | else if(a==y){ 13 | b=x; 14 | c=z; 15 | } 16 | else{ 17 | b=x; 18 | c=y; 19 | } 20 | if(a*a== b*b + c*c) 21 | return true; 22 | else 23 | return false; 24 | 25 | 26 | } 27 | int32_t main(){ 28 | int x, y, z; 29 | cin>> x >>y>>z; 30 | if(check(x, y,z)) 31 | cout<<"number is pythagorean triplet"; 32 | else 33 | cout<<"number is not pythagorean triplet "; 34 | } -------------------------------------------------------------------------------- /queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define n 100 4 | class queue{ 5 | int* arr; 6 | int front; 7 | int back; 8 | public: 9 | queue(){ 10 | arr= new int[n]; 11 | front =-1; 12 | back=-1; 13 | } 14 | void push(int x){ 15 | if(back==n-1){ 16 | cout<<"queue overflow"<back){ 28 | cout<<"empty queue"<back){ 35 | cout<<"empty queue"<back){ 43 | cout<<"empty queue"< 3 | using namespace std; 4 | int main() 5 | { 6 | int i = 5; 7 | int &j = i; 8 | int p = 10; 9 | j = p; 10 | cout << i << endl << j << endl; 11 | p = 20; 12 | cout << i << endl << j << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /remove duplicates key.cpp: -------------------------------------------------------------------------------- 1 | //remove duplicates key 2 | #include 3 | using namespace std; 4 | void removed(string s){ 5 | if(s.length()==0){ 6 | return; 7 | } 8 | if(s[0]==s[1]){ 9 | string p=s.substr(1); 10 | removed(p); 11 | 12 | } 13 | else{ 14 | cout< 2 | #include 3 | using namespace std; 4 | void reversesentence( string s){ 5 | stackst; 6 | for(int i=0; i 3 | using namespace std; 4 | void reverse(string s){ 5 | if(s.size()==0) 6 | return; 7 | string p= s.substr(1); 8 | cout< 2 | #include 3 | using namespace std; 4 | void insertatbottom(stack&st, int ele){ 5 | if(st.empty()){ 6 | st.push(ele); 7 | return; 8 | } 9 | int topele= st.top(); 10 | st.pop(); 11 | insertatbottom(st, ele); 12 | st.push(topele); 13 | 14 | } 15 | void reverse(stack&st){ 16 | if(st.empty()){ 17 | return; 18 | } 19 | int ele= st.top(); 20 | st.pop(); 21 | reverse(st); 22 | insertatbottom(st, ele); 23 | } 24 | int main(){ 25 | stack st; 26 | st.push(1); 27 | st.push(2); 28 | st.push(3); 29 | st.push(4); 30 | st.push(5); 31 | reverse(st); 32 | while(!st.empty()){ 33 | cout<arr[mid+1]){ 19 | return mid+1; 20 | } 21 | if(arr[mid]>arr[mid-1]&&arr[mid]>arr[0]){ 22 | return rotated(arr, n, mid); 23 | 24 | } 25 | return rotated(arr, mid, m); 26 | } 27 | 28 | 29 | int main(){ 30 | int arr[]={4,5,6,-9,-7,-5,-3,1,2}; 31 | int n=sizeof(arr)/sizeof(arr[0]); 32 | int m=0; 33 | cout< 2 | using namespace std; 3 | int setbit(int n, int pos){ 4 | return (n | (1<>n>>pos; 9 | cout< 3 | using namespace std; 4 | int main(){ 5 | int a; 6 | a=12; 7 | cout<<"size of int "< 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 | int subset(int arr[], int n){ 4 | for(int i=0; i<(1<>n; 16 | int arr[n]; 17 | for(int j=0; j 3 | using namespace std; 4 | int main(){ 5 | string s= "vinod"; 6 | string p =s.substr(1); 7 | cout<left)+countnodes(root->right)+root->data; 21 | } 22 | int main(){ 23 | Node* node= new Node(1); 24 | node->left=new Node(2); 25 | node->right=new Node(3); 26 | node->right->left=new Node(3); 27 | node->right->right=new Node(3); 28 | node->left->left=new Node(2); 29 | node->left->right=new Node(2); 30 | node->left->left->left=new Node(2); 31 | node->left->left->right=new Node(2); 32 | cout< -------------------------------------------------------------------------------- /tower_of_hanoi.cpp: -------------------------------------------------------------------------------- 1 | //tower of hanoi 2 | #include 3 | using namespace std; 4 | void towerofhanoi(int n, char src, char dest, char helper){ 5 | if(n==0){ 6 | return; 7 | } 8 | towerofhanoi(n-1, src, helper, dest); 9 | cout<<"move from "<>n>>m; 7 | int arr[n][m]; 8 | for(int i=0; i>arr[i][j]; 11 | } 12 | for(int i=0; i 2 | using namespace std; 3 | int main (){ 4 | vectorv; 5 | v.push_back(1); 6 | v.push_back(2); 7 | v.push_back(3); 8 | v.push_back(4); 9 | // 1st way to print elemen of vector 10 | for(int i=0; i::iterator it; 20 | for(it=v.begin();it!=v.end();it++){ 21 | cout<<"**"< print(); 27 | baseptr-> display(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /wavesor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int arr[], int a, int b){ 4 | int temp = arr[a]; 5 | arr[a]= arr[b]; 6 | arr[b]= temp; 7 | 8 | 9 | } 10 | void wavesort(int arr[], int n){ 11 | for(int i=1; iarr[i-1]) 13 | swap(arr, i, (i-1)); 14 | if(arr[i]>arr[i+1] && i<(n-2)){ 15 | swap(arr, i, i+1); 16 | } 17 | } 18 | 19 | } 20 | int main(){ 21 | 22 | int arr[]= {1,3,4,7,5,6,2}; 23 | 24 | wavesort(arr, 7); 25 | for(int i=0; i<7;i++){ 26 | cout< 2 | using namespace std; 3 | void swap(int arr[], int a, int b){ 4 | int temp = arr[a]; 5 | arr[a]= arr[b]; 6 | arr[b]= temp; 7 | 8 | 9 | } 10 | void wavesort(int arr[], int n){ 11 | for(int i=1; iarr[i-1]) 13 | swap(arr, i, (i-1)); 14 | if(arr[i]>arr[i+1] && i<(n-2)){ 15 | swap(arr, i, i+1); 16 | } 17 | } 18 | 19 | } 20 | int main(){ 21 | 22 | int arr[]= {1,3,4,7,5,6,2}; 23 | 24 | wavesort(arr, 7); 25 | for(int i=0; i<7;i++){ 26 | cout<