Posts

Flatten BST to sorted list

  // Flatten BST to Sortlisted #include <bits/stdc++.h> using namespace std ; struct Node {     int data ;     struct Node * left ;     struct Node * right ;     Node ( int val ){         data = val ;         left = NULL ;         right = NULL ;     } }; void fun ( Node * parent ){     Node * curr = parent ;     while ( curr != NULL )         cout << curr -> data << " " , curr = curr -> right ; } void inorder ( Node * curr , Node * & prev ){     if ( ! curr ) return ;     inorder ( curr -> left , prev );     prev -> left = NULL ;     prev -> right = curr ;     prev = curr ;     inorder ( curr -> right , prev ); } Node * flatten ( Node * root ){     Node * dummy = new Node ( - 1 );     Node * prev = dummy ;     inorder ( root , prev );     prev -> left = NULL ;     prev -> right = NULL ;     Node * ret = dummy -> right ;     return ret ; } int main (){         #ifndef ONLINE_JUDGE freopen ( &q

Merge Two Balanced Binary Search Trees

  #include <bits/stdc++.h> using namespace std ; #define shailesh ios :: sync_with_stdio (false); cin . tie ( 0 ); cout . tie ( 0 ); #define int long long int #define vi vector < int > #define pii pair < int, int > #define vii vector < pii > #define rep ( i , a , b ) for(int i = a; i < b; i ++ ) #define ff first #define ss second const int N = 1e5 + 10 ; bool isPowerOfTwo ( int x ){       return x && ( ! ( x & ( x - 1 ))); } bool isPrime ( int n ) {     if ( n <= 1 )     return false ;     if ( n <= 3 )     return true ;     if ( n % 2 == 0 || n % 3 == 0 )     return false ;     for ( int i = 5 ; i * i <= n ; i = i + 6 )     if ( n % i == 0 || n % ( i + 2 ) == 0 )     return false ;     return true ; } bool isPalindrome ( string S ) {     for ( int i = 0 ; i < S . length () / 2 ; i ++ ) {         if ( S [ i ] != S [ S . length () - i - 1 ] ) {        

Normal BST to Balanced BST

  #include <bits/stdc++.h> using namespace std ; #define shailesh ios :: sync_with_stdio (false); cin . tie ( 0 ); cout . tie ( 0 ); #define int long long int #define vi vector < int > #define pii pair < int, int > #define vii vector < pii > #define rep ( i , a , b ) for(int i = a; i < b; i ++ ) #define ff first #define ss second const int N = 1e5 + 10 ; bool isPowerOfTwo ( int x ){       return x && ( ! ( x & ( x - 1 ))); } bool isPrime ( int n ) {     if ( n <= 1 )     return false ;     if ( n <= 3 )     return true ;     if ( n % 2 == 0 || n % 3 == 0 )     return false ;     for ( int i = 5 ; i * i <= n ; i = i + 6 )     if ( n % i == 0 || n % ( i + 2 ) == 0 )     return false ;     return true ; } bool isPalindrome ( string S ) {     for ( int i = 0 ; i < S . length () / 2 ; i ++ ) {         if ( S [ i ] != S [ S . length () - i - 1 ] ) {        

all_operation_on_singly_linked_list

  // all basic operation retleted to singly linked list #include <bits/stdc++.h> using namespace std ; class node {     public:     int data;     node * next;     node ( int val ){         data = val ;         next = NULL ;     } }; void insertAtTail ( node * & head , int val ){     node * n = new node ( val );     node * temp = head ;     if ( head == NULL ){         head = n;         return ;     }     while (temp->next != NULL ){         temp = temp->next;     }     temp->next = n; } void display ( node * head ){     node * temp = head ;     while (temp != NULL ){         cout << temp->data << "->" ;         temp = temp->next;     }     cout << "NULL" << endl ; } void insertAtHead ( node * & head , int val ){     node * n = new node ( val );     node * temp = head ;     if (temp == NULL ){         head = n;         return ;     }     n->next = head ;     head = n; } void insertAtAnyw

Binary_Search

  #include <bits/stdc++.h> using namespace std ; // A recursive binary search function. It returns // location of x in given array arr[l..r] is present, // otherwise -1 void sorting ( int arr [], int n ) {     int temp = 0 ;     for ( int i = 0 ; i < n ; i ++ )     {         for ( int j = i + 1 ; j < n ; j ++ )         {             if ( arr [i] > arr [j])             {                 temp = arr [i];                 arr [i] = arr [j];                 arr [j] = temp;             }         }     } } int binarySearch ( int arr [], int l , int r , int x ) {     if ( r >= l ) {         int mid = l + ( r - l ) / 2 ;         // If the element is present at the middle         // itself         if ( arr [mid] == x )             return mid;         // If element is smaller than mid, then         // it can only be present in left subarray         if ( arr [mid] > x )             return binarySearch ( arr , l , mid - 1 , x );         //

nested map and imp ques

  #include <bits/stdc++.h> using namespace std ; #define shailesh ios :: sync_with_stdio ( false ); cin. tie ( 0 ); cout. tie ( 0 ); #define ll long long int #define vi vector < int > #define pii pair < int , int > #define vii vector < pii > #define rep ( i , a , b ) for ( int i = a; i < b; ++ i) #define ff first #define ss second int main (){     shailesh         map < pair < string , string > , vector < int >> m;     int n;    cin >> n;     rep (i, 0 , n){         string fn, ln;         int ct;        cin >> fn >> ln >> ct;         rep (j, 0 , ct){             int x;            cin >> x;            m [ {fn, ln} ] . push_back (x);        }    }     for ( auto & pr: m){         auto & full_name = pr.first;         auto & list = pr.second;        cout << full_name.first << " " << full_name.second << endl ;        cout << list. size (

vector of vector again stl

  #include <bits/stdc++.h> using namespace std ; #define shailesh ios :: sync_with_stdio ( false ); cin. tie ( 0 ); cout. tie ( 0 ); #define ll long long int #define vi vector < int > #define pii pair < int , int > #define vii vector < pii > #define rep ( i , a , b ) for ( int i = a; i < b; i ++ ) #define ff first #define ss second void printVec ( vector < int > & v ){     cout << "size : " << v . size () << " " << endl ;     for ( int i = 0 ; i < v . size (); i ++ )     {         cout << v [ i ] << " " ;     }     cout << endl ;     } int main (){     shailesh     vector < vector < int >> v;     int N;    cin >> N;     for ( int i = 0 ; i < N; i ++ )    {         int n;        cin >> n;         vector < int > temp;         for ( int j = 0 ; j < n; j ++ )        {             int x;            cin >