Pages

Wednesday, June 4, 2025

Count all contiguous substrings of string S where the first and last characters are the same. (USING RECURSION in C++)

#include <iostream>

using namespace std;


// Count how many times it starts and ends with same string

int go(string str, int i, int j, int n) {

    if(n == 1) {

        return 1;

    }

    

    if(n<=0) {

        return 0;

    }

    

    int res = go(str, i+1, j, n-1)+go(str, i, j-1, n-1)-go(str, i+1, j-1, n-2);

    

    if(str[i] == str[j]) {

        res++;

    }

    

    return res;

}


int main() {

    string S = "abcab";

    int size = S.length();

    

    cout << go(S, 0, size-1, size);

    

    

    return 0;

}


OUTPUT:


7

Student Info Display (University OOPs Lab Topic)

 #include <iostream>

using namespace std;


// Base class

class Person {

protected: 

    string name;

    int age;


public:

    // Parameterized constructor

    Person(string n, int a) {

        name = n;

        age = a;

        cout << "Person constructor called with name = " << name << " and age = " << age << endl;

    }

};


// Derived class

class Student : public Person {

private: 

    string studentID;

    

public:

    // COnstructor for Student that intializes base class (Person) and studentID

    Student(string n, int a, string id) : Person(n, a), // Call the base class constructor with name and age

    studentID(id) {

        

        // Constructor body (can be empty if no additional initialization is needed)

    } 

    

    

    // Method to display student info

    void displayStudentInfo() {

        cout << "Name: " << name << endl;

        cout << "Age: " << age << endl;

        cout << "Student ID: " << studentID << endl;

    }

};



int main() {

    Student student("Rahul", 20, "50342324");

    student.displayStudentInfo();


    return 0;

}

Find all Occurences using Recursion in C++

 #include <iostream>

using namespace std;


void allOccurences(int arr[], int key, int i, int n) {

    if(i == n) {

        return;

    }

    

    if(arr[i] == key) {

        cout << i << " ";

    }

    

    allOccurences(arr, key, i+1, n);

}


int main() {

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

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

    int key = 2; 

    allOccurences(arr, key, 0, n);


    return 0;

}

Bank Account Question (University OOPs Lab Topic)

 #include <iostream>

using namespace std;

class BankAccount {
private:
    int accountNumber;
    double balance;
   
public:    
// Constructor to initialize account    
    BankAccount(int accNo, double initialBalance = 0.0) {
        accountNumber = accNo;
        balance = initialBalance;
    }
   
   
    void deposit(double amount) {
        if(amount > 0) {
            balance += amount;
             cout << "Deposited: " << amount << endl;
        } else {
            cout << "Invalid deposit amount!" << endl;
        }
    }
    void withdraw(double amount) {
        if(amount > 0 && amount <= balance) {
            balance -= amount;
            cout << "Withdrawn: " << amount<< endl;
        } else {
            cout << "Invalid or insufficient funds for withdrawal!" << endl;
        }
    }
   
    // method to get balance
    double getBalance() const {
        return balance;
    }
   
    // Optional: Show account number and balance
    void showAccountInfo() const {
        cout << "Account Number: " << accountNumber << endl;
        cout << "Current Balance: " << balance << endl;
    }
   
   
};

int main() {
    // Create a bank with account number 1001 and initial balance of 500
   
    BankAccount account(1001, 500.0);
   
    account.showAccountInfo();
   
    // Perform deposit and withdrawal operations
    account.deposit(250);
    account.withdraw(100);
   
    //Show final balance
    cout << "Final balance: " << account.getBalance() << endl;
   
    return 0;
}

Tuesday, June 3, 2025

Function Pointer (the TOPIC that I missed!) - Part 1

 #include <iostream>

using namespace std;



// Function declaration + definition (function to be pointed to..)

void greet() {

    cout << "Hello!\n";

}


int main() {

    

    // Function pointer

    void (*fp)();


    // Assign the address of greet() fnx to the fnx pointer..

    

    fp = greet;

    

    greet();

    

    return 0;

}




OR



with parameters 


#include <iostream>

using namespace std;


int add(int a, int b) {

    return a+b;

}



int main() {

    // Declare function pointer

    int (*operation)(int, int);

    

    

    // Assign fnx address

    operation = add;

    

    // Call the fnx using pointer

    cout << operation(4, 4);


    return 0;

}



Sunday, June 1, 2025

Watermelon in Codeforces(It accepted at 3rd attempt!!)

 #include <iostream>

using namespace std;




int main() {

    int watermelon;

    cin >> watermelon;


    

    if(watermelon == 2) {

        cout << "NO" << endl;

    }

    else if(watermelon%2==0) { // even no;

        cout << "YES" << endl;

    }else { // odd no.

        cout << "NO" << endl;

    }

    

    return 0;

}

Binary Strings using Recursion in C++

 #include <iostream>

#include <string>
using namespace std;

void binString(int n, int lastPlace, string ans) {
    if(n == 0) {
        cout << ans << endl;
        return;
    }
   
    if(lastPlace != 1) {
        binString(n-1, 0, ans+'0');
        binString(n-1, 1, ans+'1');
    } else {
        binString(n-1, 0, ans + '0');
    }
}

void binString(int n, string ans) {
    if(n == 0) {
        cout << ans << endl;
        return;
    }
   
    if(ans[ans.size()-1] != '1') {
        binString(n-1, ans+'0');
        binString(n-1, ans+'1');
    } else {
        binString(n-1, ans + '0');
    }
}

int main() {
    string ans = " ";
    binString(3, ans);

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