Posts

Showing posts from June, 2021

Reversing stack Method 2 !! (One Helper Stack only)

#include   <bits/stdc++.h> using   namespace   std ; stack < int >  rev ( stack < int >  s ) {      int   size  =  s . size (),  x ,  y ,  t ;      stack < int >  s2 ;      size  -=  1 ;      while  ( size --)     {          t  =  size  +  1 ;          x  =  s . top ();          s . pop ();          while  ( t --)         {              s2 . push ( s . top ());              s . pop ();         }  ...

Square root using binary search !

Find the first Intersection point in Y Shaped Linked Lists !! using vector and stack

  /* Linked List Node struct Node {   int data;   struct Node *next;   Node(int x) {     data = x;     next = NULL;   } };  List_A      List_B 4              5 |              | 1              6  \             /   8   ------  1     |    4    |    5    |   NULL      */    int   intersectPoint ( Node *   head1 ,  Node *   head2 ) {   ...

No. of pairs with difference equal to (k) !! method 1.

  int   check ( int   k ,  vector < int >  arr ) {      int   i ,  j ,  sum  =  0 ,  x ;     set< int > s;     map< int ,  int > mp;      map < int ,  int >::iterator it;      for  ( auto   it  :  arr )     {          mp [ it ]++;          s . insert ( it );     }      for  (it =  mp . begin (); it !=  mp . end (); it++)     {          x  = ( it -> first ) +  k ;          if  ( s . count ( x ) >  0 )          ...

Subarray of maximum Sum of a given Array O(n)!!!! (kadane algorithm)

#include<bits/stdc++.h> using namespace std; int main() {     int n,i,j,k,sum,right,max_sum,s;     cout<<"enter the size of array : \n";     cin>>n;     int a[n];     for(i=0;i<n;i++)     cin>>a[i];          max_sum=-1;     right=-1;     sum=0;          for(i=0;i<n;i++)     {         sum+=a[i];         if(sum<0)         {             sum=0;         }         else if(sum>max_sum)         {             max_sum=sum;             right=i;         }     }     s=max_sum;          cout<<"The maximum sum will be: "<<max_sum<<endl; ...

Subarray of maximum Sum of a given Array O(n^2)!!!!

 #include<bits/stdc++.h> using namespace std; int main() {     int n,i,j,k,sum,c_sum,left,right,current_sum;     cout<<"enter the size of array : \n";     cin>>n;     int a[n],csum[n];     for(i=0;i<n;i++)     cin>>a[i];          csum[0]=a[0];     for(i=1;i<n;i++)     csum[i]=a[i]+csum[i-1];          /*for(i=0;i<n;i++)     cout<<csum[i]<<" ";*/          sum=-1;     for(i=0;i<n;i++)     {         for(j=i;j<n;j++)         {             current_sum=csum[j]-csum[i-1];                          if(current_sum>sum)             {                 sum=curr...

Subarray of maximum Sum of a given Array O(n^3)!!!!

 #include<bits/stdc++.h> using namespace std; int main() {     int n,i,j,k,sum,c_sum,left,right;     cout<<"enter the size of array : \n";     cin>>n;     int a[n];     for(i=0;i<n;i++)     cin>>a[i];     cout<<"All subarrays are: ";     sum=-1;     for(i=0;i<n;i++)     {         for(j=i;j<n;j++)         {             c_sum=0;             for(k=i;k<=j;k++)             c_sum+=a[k];                          if(c_sum>sum)             {                 sum=c_sum;                 left=i;                 ri...

To generate all the subarrays of a given array O(n^3) !!!!

#include<bits/stdc++.h> using namespace std; int main() {     int n,i,j,k;     cout<<"enter the size of array : \n";     cin>>n;     int a[n];     for(i=0;i<n;i++)     cin>>a[i];     cout<<"All subarrays are: ";     for(i=0;i<n;i++)     {         for(j=i;j<n;j++)         {             for(k=i;k<=j;k++)             cout<<a[k]<<" ";             cout<<endl;         }     } }

Calculate factorial of large numbers !! (Using Singly Linked list)

#include   <bits/stdc++.h> using   namespace   std ; struct   node {      int   data ;      struct   node   * next ; }; struct   node   * add ( struct   node   * tail ,  int   c ) {      struct   node   * newnode ;      newnode  = ( struct   node   * ) malloc ( sizeof (struct  node ));      newnode -> data  =  c ;      newnode -> next  =  0 ;      tail -> next  =  newnode ;      tail  =  newnode ;      return   tail ; } //Using recursion to print the list in reverse order... void   print ( struct   node   * head ) {      if  ( head  ==  0 )     ...

Calculate factorial of large numbers !! (Using Arrays)

#include   <bits/stdc++.h> using   namespace   std ; #define   max   10000 int   main () {      int   n ,  c ,  j ,  k ,  x ,  i ;      int   a [ max ];      cout   <<   "Enter the number: " ;      cin   >>   n ;      j  =  0 ;  // j will be pointing to new index created to stored carry      a [ 0 ] =  1 ;      for  ( i  =  2 ;  i  <=  n ;  i ++)     {          c  =  0 ;  //carry when each number of array is multiplied by i          //(i is a...

Insertion Sort

#include   <bits/stdc++.h> using   namespace   std ; int   main () {      int   n ,  i ,  j ,  value ;      cout   <<   "Enter the size of array : " ;      cin   >>   n ;      int   a [ n ];      cout   <<   " \n Enter the array : " ;      for  ( i  =  0 ;  i  <  n ;  i ++)          cin   >>   a [ i ];      for  ( i  =  1 ;  i  <  n ;  i ++)     {          value  =  a [ i ];          j  =  i  -  1 ;        ...