Pages

Monday, July 28, 2025

Runtime Polymorphism – Employee and Manager : Salary Based Problem.

 #include <iostream>
using namespace std;

class Employee {
public:
virtual void computeSalary() {
cout << "Employee salary: 500rs" << endl; 
}
};


class Manager : public Employee {
public:
void computeSalary() override {
cout << "Manager Salary: 1500rs" << endl; } };

int main() {
Employee* = emp;
Manager mgr;
emp  = &mgr;

emp->computeSalary(); // Calls Manager's version...

return 0;

}


Necessity of Templates in C++: Examples : Explain?

-> Templates enables generic programming.
-> Allow functions and classes to operate on any data type without code duplication.
-> Promotes code reusability, efficiency, and type safety.
-> Instead of writing separate functions for int, float, double, char, etc,, you can write one generic template.


#Code Example to Find Max:

#include <iostream>

using namespace std;

template<typename T>

T max(T a, T b) {
return (a > b) ? a : b;
}


#How Compiler Generates Code:

When you call max() with specific types, like:

int a = max(3, 5); // T = int
double d = max(3.5, 2.1); // T = double
char c = max('a', 'b'); // T = char

-> The compiler generates a separate versions of max for each data type during compilation(which is called template instantiation). So, it seems like as if you had written separate versions like multiple overloaded versions of the function. 

Friday, July 25, 2025

Binary Search in a Sorted Array in C++

 #include <iostream>

using namespace std;




int binarySearch(int arr[], int key, int n) {

    int si = 0; 

    int ei = n-1;

    

    while(si <= ei) {

        int mid = si + (ei-si)/2;

        

        if(arr[mid] == key) {

            return mid;

            

        } else if(arr[mid] < key) {

            si = mid+1; // go to right

        }  else {

            ei = mid-1; // go to left

        }

        

    }

    

    return -1;

    

}


void printArr(int arr[], int n) {

    for(int i=0; i<n; i++) {

        cout << arr[i] << " ";

        

    }

    cout << endl;

}

int main() {

    // binarySearch works on sorted array:

    // O(logn) -> binary -> divide

    

    

    int arr[] = {1, 2, 3, 4, 5};

    int n = sizeof(arr)/sizeof(int);

    int key = 4; 

    cout << "Array : ";

    

    printArr(arr, n);

    

    cout << "Element: " << key << " \nFound at index: " <<  binarySearch(arr, key, n);

    


    return 0;

}

BST (Insertion + Inorder)

 #include <iostream>

using namespace std;


struct Node {

    int data;

    Node* left;

    Node* right;

    

    Node(int val) {

        data = val;

        left = right = NULL;

    }

};



Node* insert(Node* root, int value) {

    // Base Case

    if(root == NULL) {

        return new Node(value);

    }

    

    if(value < root->data) {

        root->left = insert(root->left, value);

    } else if(value > root->data) {

        root->right = insert(root->right, value);

    }

    

    return root;

    

}



void inorder(Node* root) {

    if(root == NULL) {

        return;

    }

    

    inorder(root->left);

    cout << root->data << " ";

    inorder(root->right);

}



int main() {

    Node* root = NULL;


    // Insert elements into BST

    root = insert(root, 50);

    insert(root, 30);

    insert(root, 60);

    

    cout << "Inorder Traversal OF BST: ";

    inorder(root);

    cout << endl;


    return 0;

}

Sunday, July 20, 2025

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream>

using namespace std;


struct Node {

    int data;

    Node* next;

};


class Stack {

    Node* top;


public:

    Stack() { top = NULL; }


    void push(int x) {

        Node* temp = new Node();

        temp->data = x;

        temp->next = top;

        top = temp;

    }


    void pop() {

        if (top == NULL) {

            cout << "Stack Underflow\n";

            return;

        }

        Node* temp = top;

        top = top->next;

        delete temp;

    }


    int peek() {

        if (top == NULL) return -1;

        return top->data;

    }


    bool isEmpty() {

        return top == NULL;

    }

};


Inorder Traversal (uni topic) in c++

 #include <iostream>
using namespace std;



struct Node {
int data;
Node* left;

Node* right;
};


void inorder(Node* root) {  // major mistake (Node* root) is correct)
if(root == NULL) {
return; }
inorder(root->left); 

cout << root->data << endl;

inorder(root->right);
}

Saturday, July 19, 2025

Binary Search in C++ (University Exam Topic) - Review Program

 #include <iostream>

using namespace std;




int binarySearch(int arr[], int size, int key) {

    int si = 0; 

    int ei = size-1;

    

    while(si <= ei) {

        int mid = si + (ei-si)/2;

        

        if(arr[mid] == key) {

            return mid;

        }

        

        else if(arr[mid] <= key) {

            si = mid+1; // go to right half

        } else {

            ei = mid-1; // go to left half

        }

    }

    

    return -1;

}




int main()

{

    int arr[] = {1, 4, 6, 7};

    int size = sizeof(arr)/sizeof(int);

    cout << binarySearch(arr, size, 6);


    return 0;

}

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...