Pages

Saturday, May 31, 2025

Friends Pairing using Recursion in C++

 #include<iostream>

#include<string>
using namespace std;

int friendsPairing(int n) {
    if(n == 1 || n == 2) {
        return n;
    }

    // // single
    // friendsPairing(n-1);

    // // pair
    // (n-1) * friendsPairing(n-2)
    return friendsPairing(n-1) + (n-1) * friendsPairing(n-2);
}

int main() {
    cout << friendsPairing(4) << endl; // 10
return 0;

}

Remove Duplicates using Recursion in C++

 #include<iostream>

#include<vector>
#include<cmath>
using namespace std;

void removeDuplicates(string str, string ans, int i, int map[26]) {
    // Base case
    if(i == str.size()) {
        cout << "ans: " << ans << endl;
    return;
    }

    int mapIdx = (int)(str[i] - 'a');

    if(map[mapIdx]) { // duplicate
            removeDuplicates(str, ans, i+1, map);


    } else {// not duplicate
    map[mapIdx] = true;
    removeDuplicates(str, ans+str[i], i+1, map);
    }
}

int main() {
    string str = "appnnacollege";
    string ans = "";
    int map[26] = {false};



    removeDuplicates(str, ans,0, map);
return 0;

}

Friday, May 30, 2025

Book Library Management (University Question)

 #include <iostream>

#include <string>
#include <vector>
using namespace std;

// Library Book Management System

class Book {
private:
    string title;
    string author;

public:
    Book(string t, string a) {
        title = t;
        author = a;
    }
   

    // Constructor :
    string getTitle() {
        return title;
    }

    string getAuthor() {
        return author;
    }


    void showDetails() {
        cout << "Book: " << title << "\nAuthor: " << author << endl;
    }
};

class Library {
private:
    vector<Book> books;


public:


    // Add a book
    void addBook(string title, string author) {
        books.push_back(Book(title, author));
        cout << "Book added successfully.\n";
    }

    // Remove a book
    void removeBook(string title) {
        for(auto it = books.begin(); it <= books.end(); it++) {
            if(it->getTitle() == title) {
                books.erase(it);
                cout << "Book removed successfully.\n";
                return;
            }
        }

        cout << "Book not found.\n";
    }    

    // Show All Books
    void showAllBooks() {
        if(books.empty()){
            cout << "Library is empty.\n";
            return;
        }

        cout << "\nBooks in Library: \n";
        for(auto& book : books) {
            book.showDetails();

        }
    }


};

int main() {
    Library lib;

    lib.addBook("Book S1", "Deb");
    lib.addBook("Book S2", "Deb");
    lib.addBook("Book S3", "Deb");

    lib.showAllBooks();

    lib.removeBook("Book S2");

    lib.showAllBooks();


    return 0;

}

C. Create a C++ program that uses a function template to find the maximum value between two values of any data type.

#include <iostream>

using namespace std;


template<typename T>

T maxVal(T a, T b) {

    return (a>b) ? a : b;

    /*

    if (a > b) {

        return a;

    } else {

        return b;

    }

    */

    /*

    OR 

    return (a > b) ? a : b is same as if 

    (a > b)

    return a;

    else

    return b;

    */

}


int main() {

    int val1, val2;

    cout << "enter first value : ";

    cin >> val1;

    cout << "\nenter second value : ";

    cin >> val2;

    int findMax = maxVal(val1, val2);

    cout << findMax << " is the max value " << endl;

    return 0;

}

Thursday, May 29, 2025

A. Create a C++ program that demonstrates function overloading.

 #include <iostream>

#include <string>

#include <vector>

using namespace std;



// Function overloading - example



class Print {

public:

    void show(int x) {

        cout << "int : "<< x << endl;

    }





    void show(string str) {

        cout << "str: " << str << endl;

    }





    void show(string str2) { // <--- This is the problem ( can't put two functions
under a class when it comes to function overloading....

        cout << "str2: " << str2 << endl;

    }

};





int main() {

    Print ob1;



    ob1.show(45);

    ob1.show("apna college");

    ob1.show("Debadatta Patra");



    return 0;

}

D. Create a C++ class template named Pair that represents a pair of values. (University Topic of OOPs)

#include <iostream>

#include <string>

#include <vector>

using namespace std;


// D. Create a C++ class template named Pair that represents a pair of values. 


template <typename T>


class Pair {

private:

    T A1;

    T A2;

    

public:

    Pair(T val1,T val2) {

        A1 = val1;  // initialize first ehe private variable then the Public variable

        A2 = val2;


    }

    

    // display

    void display() {

        cout << "value A: " << A1 << " \nvalue B: " << A2 << endl;

    }

};





int main() {

    int x, y;

    cout << "enter first value : ";

    cin >> x;

        

    cout << "enter second value : ";

    cin >> y;

    

    Pair<int> class1(x, y);

    

    class1.display();

    

    return 0;

}

Merge Sort in C++ (university dsa topic)

 #include <iostream>

#include <vector>
using namespace std;

void merge(vector<int> &arr, int st, int mid , int end){  // O(n)
    vector<int> temp;
    int i = st, j = mid+1;


    // left to right half traversal
    while(i <= mid && j <= end) {
        if(arr[i] <= arr[j]) {
            temp.push_back(arr[i]);
            i++;a
        } else {
            temp.push_back(arr[j]);
            j++;
        }
    }


    while(i <= mid) {
        temp.push_back(arr[i]);
        i++;
    }


    while(j <= end) {
        temp.push_back(arr[j]);
        j++;
    }


   
    for(int idx=0; idx<temp.size(); idx++) {
        arr[idx+st] = temp[idx];
    }
}


void mergeSort(vector<int> &arr, int st, int end) {
    if(st < end) {
        int mid = st + (end-st)/2;

        mergeSort(arr, st,mid); // left half
        mergeSort(arr, mid+1, mid); // right half

        merge(arr, st, mid ,end);
    }
}

int main() {
    vector<int> arr = {12, 31, 35, 8, 32, 17};

    mergeSort(arr, 0, arr.size()-1); // arr.size()-1 => array ka last index)

    for(int val : arr) {
        cout << val << " ";
    }

    cout << endl;

    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...