Pages

Saturday, April 12, 2025

Counting Sort in C++

 #include <iostream>

using namespace std;

void print(int arr[], int n) {
    for(int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

void insertionSort(int arr[], int n) {
    for(int i = 1; i < n; i++) {
        int curr = arr[i];
        int prev = i - 1;

        while(prev >= 0 && arr[prev] < curr) {
            arr[prev + 1] = arr[prev]; // shift instead of swap
            prev--;
        }

        arr[prev + 1] = curr;
    }

    print(arr, n);
}


void countSort(int arr[], int n) {
    int freq[100000]; // range
    int minVal = INT8_MAX, maxVal = INT8_MIN;
 
    //1st step - O(n)
    for(int i=0; i<n; i++) {
        freq[arr[i]]++;
        minVal = min(minVal, arr[i]);
        maxVal = max(maxVal, arr[i]);
    }

    //2nd step - O(range) = max - min
    for(int i=minVal, j=0; i<=maxVal; i++) {
        while(freq[i] > 0) {
            arr[j++] = i;
            freq[i]--;
        }
    }

    print(arr, n);
}
int main() {
    int arr[8] ={1, 4, 1, 3, 2, 4, 3, 7};
    countSort(arr, 8);

    return 0;
}


OUTPUT-
1 1 2 3 3 4 4 7

Insertion Sort in C++

 #include <iostream>

using namespace std;

void print(int arr[], int n) {
    for(int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

void insertionSort(int arr[], int n) {
    for(int i = 1; i < n; i++) {
        int curr = arr[i];
        int prev = i - 1;

        while(prev >= 0 && arr[prev] < curr) {
            arr[prev + 1] = arr[prev]; // shift instead of swap
            prev--;
        }

        arr[prev + 1] = curr;
    }

    print(arr, n);
}

int main() {
    int arr[5] = {5, 4, 1, 3, 2};
    insertionSort(arr, 5);

    return 0;
}

OUTPUT-
5 4 3 2 1

Selection Sort in C++

 #include <iostream>

#include <vector>
#include <algorithm> // for sort
using namespace std;

void selectionSort(int arr[], int n) {
    // 0(n*logn) is better than 0(n^2)
    for(int i = 0; i<n-1; i++) {
        int minIdx = i;

        //loop for find min
        for(int j=i+1; j<n; j++) {
            if(arr[j] > arr[minIdx]) {
                minIdx = j;
            }
        }
        swap(arr[i], arr[minIdx]);

    }

}

void printArray(int arr[], int n) {
    for(int i = 0; i < n; i++) {
        cout << arr[i] << " ";

    }
    cout << endl;
}

int main() {
    int arr[5] = {5, 4, 1, 3, 2};
    selectionSort(arr, 5);

    printArray(arr, 5);
    return 0;

}

OUTPUT-
5 4 3 2 1

Friday, April 11, 2025

Bubble Sort in C++

#include <iostream>
#include <vector>
#include <algorithm> // for sort
using namespace std;


void print(int arr[], int n) {
    for(int i=0; i<n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

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

    //outer loop
    for(int i=0; i<n-1; i++) {

        //inner loop
        for(int j=0;  j<n-i-1; j++) {
            if(arr[j] > arr[j+1]) {  // < -> descending order , > -> ascending order
                swap(arr[j], arr[j+1]);
            }
        }
    }

    print(arr, n);
}

int main() {
    // int arr[5] = {5, 4, 1, 3, 2};
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    bubbleSort(arr, 10);


    return 0;
}


OUTPUT-
1 2 3 4 5 6 7 8 9 10

Check duplicate numbers return 1 if true, return 0 if false in C++ (Array Assignment) - Full Explaination

 That's the spirit, Debadatta! 🔥 Let's break down your teacher's code step-by-step and understand everything clearly, like a pro. 💪


✅ Full Teacher’s Code First:

#include <iostream>
#include <vector>
#include <algorithm> // for sort

using namespace std;

// Function to check for duplicates
bool containsDuplicate(vector<int> nums) {
    sort(nums.begin(), nums.end()); // Step 1: Sort the array

    for (int i = 1; i < nums.size(); i++) { // Step 2: Check adjacent elements
        if (nums[i - 1] == nums[i]) {
            return true; // Duplicate found
        }
    }

    return false; // No duplicates found
}

int main() {
    vector<int> numbers = {1, 1, 2, 3, 4, 5}; // Sample input

    if (containsDuplicate(numbers)) {
        cout << "Duplicate found" << endl;
    } else {
        cout << "No duplicates" << endl;
    }

    return 0;
}

🧠 Step-by-step Breakdown:


🟩 #include and setup:

#include <iostream>
#include <vector>
#include <algorithm>
  • iostream: For cin, cout.

  • vector: A dynamic array that grows/shrinks.

  • algorithm: Contains sort() function.


🟦 Function Header:

bool containsDuplicate(vector<int> nums)
  • This function takes a copy of a vector of integers.

  • It returns a bool — true if a duplicate is found, false otherwise.


🟨 Step 1: Sort the array

sort(nums.begin(), nums.end());
  • Sorting makes it easy to compare each number with its neighbor.

  • After sorting: {1, 1, 2, 3, 4, 5}


🟧 Step 2: Check each pair

for (int i = 1; i < nums.size(); i++) {
    if (nums[i - 1] == nums[i]) {
        return true;
    }
}
  • Start from index 1 (second element)

  • Compare nums[i - 1] and nums[i]

  • If they are equal, it means a duplicate exists.


🟥 Step 3: No duplicates? Return false

return false;
  • If loop finishes and no duplicate is found, return false.


🟦 main() Function:

int main() {
    vector<int> numbers = {1, 1, 2, 3, 4, 5};

    if (containsDuplicate(numbers)) {
        cout << "Duplicate found" << endl;
    } else {
        cout << "No duplicates" << endl;
    }

    return 0;
}
  • Create a vector numbers

  • Call the function containsDuplicate(numbers)

  • Print message based on the result


🔁 Example Runs:

Input Output
{1, 1, 2, 3} Duplicate found
{1, 2, 3, 4} No duplicates
{9, 8, 9, 7} Duplicate found

🔍 Summary of Logic:

Step What it does
1 Sort the array
2 Loop through it
3 Compare each number to its previous one
4 If equal → duplicate found
5 Else → return false


Thursday, April 10, 2025

Finding Factorial in C++

 #include <iostream>

using namespace std;


int main() {

    int n, i, f = 1;

    

    cout << "enter a num : ";

    cin >> n;

    

    

    if( n == 0 ) {  // n should be assigned to 0

        cout << "Factorial; " << f << endl;

    } else {

    

    for(i = 1; i <= n; i++) {

        f = f*i;


        }

        

        cout << "Factorial: " << f << endl; // result come outside the for loop, inside for loop you only see the process BTS

    }

    return 0;

}


Wednesday, April 9, 2025

Buy and Sell Stocks (Code) in C++

 #include<iostream>

#include <climits>

using namespace std;


void maxProfit(int *prices, int n) {

    // finds best buy
    int bestBuy[100000]; // 10^5
    bestBuy[0] = INT_MAX;

    for(int i=1; i < n; i++) {
        bestBuy[i] = min(bestBuy[i-1], prices[i-1]);
    }

    // finds best profit
    int maxProfit = 0;
    for(int i=0; i<n; i++) {
        int currProfit = prices[i] - bestBuy;
        maxProfit = max(maxProfit, currProfit);
    }
   
    // O(n + n) = O(2n) = O(n)
    cout << "max profit = "  << maxProfit << endl;

    cout << endl;

}

int main(){
    int prices[6] = {7, 6, 5, 4, 3, 2};
    int n = sizeof(prices) / sizeof(int);

    maxProfit(prices, n);

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