Posts

Showing posts from May, 2021

Convert Decimal to Binary using RECURSION !

#include   <bits/stdc++.h> using   namespace   std ; int   bin ( int   num ) {      static   int   a  =  0 ;      if  ( num  /  2  !=  0 )          bin ( num  /  2 );      a  =  a  *  10  +  num  %  2 ;      return   a ; } int   main () {      int   num ;      cout   <<   "Enter the Number : " ;      cin   >>   num ;      cout   <<   "The binary number will be : "   <<   bin ( num )  <<   endl ; }

Reverse Elements in Stack (ITERATIVE APPROACH, ONLY EXPLAINATION) !

Assume  the  stack to be reversed is S1 It can be done using two different stacks (helper stacks)   or by a single helper stack. A) TWO HELPER STACKS (Let them be S2 and S3) S1 (POP all)--> S2 (POP all)--> S3 (POP all)--> S1 (elements reversed) B)ONE HELPER STACK (let it be S2) -> i= 0 x= S1 . top () pop top element pop   remaining ( n - 1 - i ) elements and push in S2. Now push x in S1. and pop S2 completely and push it in S1 i++ ( Now   initial   top   most   element   is   at   bottom ) ------------------------------------------------- now store top most value in x= S1 . top () pop top element ...

Linked list creation, print, Reverse print, insertion at end(ALL by RECURSION)

#include   <bits/stdc++.h> using   namespace   std ; struct   node {      int   data ;      struct   node   * next ; }; struct   node   * create ( struct   node   * head ) {      struct   node   * newnode ;      int   x ;      cout   <<   "Enter the data" ;      cin   >>   x ;      if  ( x  == - 1 )          return   head ;      else     {          newnode  = ( struct   node   * ) malloc ( sizeof (struct  node ));          newnode -> data  =  x ;          newnode -> next  ...

Stack using Queue, making Push costly (via class and object)

#include   <bits/stdc++.h> using   namespace   std ; //making push operation costly class   stk { private:      queue < int >  q1 ,  q2 ; public:      void   psh ()     {          int   x ,  z ;          cout   <<   "Enter the value: " ;          cin   >>   x ;          q2 . push ( x );          while  (! q1 . empty ())         {              z  =  q1 . front ();              q2 . push ( z );         ...

calculate number of trailing zeros in n!

Image
#include   <bits/stdc++.h> using   namespace   std ; void   trailing_zeros ( long   long   int   x ) {      int   sum  =  0 ,  i  =  5 ;      while  ( x  /  i  >=  1 )     {          sum  +=  x  /  i ;          i  *=  5 ;     }      cout   <<   sum ; } int   main () {      long   long   int   n ;      cin   >>   n ;      trailing_zeros ( n ); }

Stack using Queue, making Pop costly (via class and object)

  #include   <bits/stdc++.h> using   namespace   std ; //making pop operation costly class   stk { private:      queue < int >  q1 ,  q2 ; public:      void   psh ()     {          int   x ;          cout   <<   "Enter the value: " ;          cin   >>   x ;          q1 . push ( x );     }      void   popp ()     {          int   x ;          if  ( q1 . empty ())         {              cout   <...

Queue using stack, making Enqueue costly (via class and object)

#include   <bits/stdc++.h> using   namespace   std ; class   que { private:      stack < int >  s1 ,  s2 ; public:      void   psh ()     {          int   x ,  y ;          cout   <<   "Enter the value: " ;          cin   >>   y ;          if  ( s1 . empty ())         {              s1 . push ( y );              return ;         }          else         {      ...

Queue using stack, making Dequeue costly (via class and object)

#include   <bits/stdc++.h> using   namespace   std ; class   que { private:      stack < int >  s1 ,  s2 ,  s3 ; public:      void   psh ()     {          int   x ;          cout   <<   "Enter the value: " ;          cin   >>   x ;          s1 . push ( x );     }      void   popp ()     {          if  ( s1 . empty ())         {              cout   <<   "Queue is empty! " ;           ...

Adjacency list representation of graph (nodes with string names, Hashing)

 #include<bits/stdc++.h> using namespace std; class graph{     /*creating a hash map,     <string><list of pair(<string,weight>)>     A->      B 20, C 30     B->      A 20, D 40     C->      D 10     D->      B 40, C 10     */     public:     unordered_map<string, list<pair<string,int>>>mp;     void addEdge(string x,string y, bool is_bidir, int wght)     {         mp[x].push_back(make_pair(y,wght));         if(is_bidir==true)         mp[y].push_back(make_pair(x,wght));     }     /*void print()     {         for(auto it=mp.begin();it!=mp.end();it++)         { //Here, there is a pointer which is pointing to every member index, //hence u...

Adjacency list representation of graph (nodes with numeric names)

 #include<bits/stdc++.h> using namespace std; class graph {     private:         int vertices;         list<int> *l;     public:         graph(int v)         {             this->vertices=v;             l=new list<int>[v];         }         void addEdge(int x,int y)         {             l[x].push_back(y);             l[y].push_back(x);         }         void print()         {             for(int i=0;i<vertices;i++)             {                 cout<<"Vertex "<<i<<"-> ";           ...

Sieve!

#include   <bits/stdc++.h> using   namespace   std ; #define   mod   1000000007 #define   size   100000000 typedef   long   long   int   ll ; ll   p [ size ]{ 0 }; void   sieve ( ll   p [ size ]) {      p [ 2 ] =  1 ;      ll   i ,  j ;      for  ( i  =  3 ;  i  < size;  i  +=  2 )          p [ i ] =  1 ;      for  ( i  =  3 ;  i  < size;  i  +=  2 )     {          for  ( j  =  i  *  i ;  j  < size;  j  +=  i )              p [ j ] =  0 ;     }    ...

Matrix Multiplication in Python

  n1, m1 =  map ( int ,  input ().split()) mat1 = [] for i in range(n1):     l = []     l = [int(input("enter the elements : ")) for i in range(m1)]     mat1.append(l) #---------------------------------------------------------------# n2, m2 = map(int, input().split()) mat2 = [] for i in range(n2):     l = []     l = [int(input("enter the elements : ")) for i in range(m2)]     mat2.append(l) #---------------------------------------------------------------# mat3 = [] if m1 == n2:      for i in range(n1):         l = []      ...

Merging two Singly linked lists in alternate order.

#include<bits/stdc++.h> using namespace std; struct node {     int data;     struct node * next; }; struct node * newnode, *temp, *temp1, *temp2; struct node * create() {     struct node *head=0;     int choice=1;     while(choice==1)     {         newnode=(struct node*)malloc(sizeof(struct node));         cout<<"Enter data ";         cin>>newnode->data;         newnode->next=0;         if(head==0)         head=temp=newnode;         else         {             temp->next=newnode;             temp=newnode;         }         cout<<"Want another node to be inserted : ";         cin>>choice;     }   ...