Skip to content

Devansh-Seth-DEV/DSA-Playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ DSA-Playground

Repo Name Β  C++17 Β  Optimized Engine Β  Architecture Β  vEB Layout Β  Status Β  Platforms

A high-performance laboratory for mastering Data Structures and Algorithms. This repository features industrial-grade templates, optimized competitive programming solutions, and structured implementations.

πŸ—ΊοΈ Repository Map

Tip

Click on the icons or headers to navigate to the specific modules.

🎯 CP31-Sheet

Progressive problem-solving through the CP-31 curated list.

  • D800 - D1200: Foundation
  • D1300 - D1900: Intermediate/Advanced

βš™οΈ Implementations

Modular, reusable templates for core DSA.

  • Graphs: Shortest Paths, MST, Flow
  • Trees: Segment Trees, Splay Trees

Collection of solutions from various OJs.

  • LeetCode / SPOJ / Timus
  • Archived: Legacy/Offline problems

The "Must-Do" classics categorized by topic.

  • Dynamic Programming patterns
  • Array manipulation & Logic

πŸ“œ Competitive Programming Template

To maintain high performance and code clarity, all competitive programming solutions in this repository utilize a Triple-Layer Architecture. This decouples I/O stream synchronization from the core algorithmic logic.

#include <bits/stdc++.h>
using namespace std;

/**
 * @brief Optimizes C++ I/O operations by desyncing with 
 *        stdio and untying cin from cout.
 */
inline void fastIO() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

/**
 * @brief Generic output utility for competitive programming.
 *
 * The Printer class provides a unified interface for printing
 * various data types to standard output. It supports:
 *  - Primitive types (int, double, etc.)
 *  - Boolean values (printed as YES / NO)
 *  - C-style strings and std::string
 *  - std::vector<T> (including nested vectors)
 *  - std::vector<bool> (specialized handling)
 *  - Built-in arrays of any dimension
 *
 * All print operations are recursive and automatically dispatch
 * to the correct overload using C++ overload resolution.
 *
 * @note Designed for competitive programming environments.
 * @note Trailing spaces are avoided in container outputs.
 */
class Printer {
public:
    /**
     * @brief Prints a generic value followed by a terminator.
     *
     * This overload is selected for any type supporting
     * the stream insertion operator (operator<<).
     *
     * @tparam T Type of the value to print
     * @param val Value to print
     * @param end Output terminator (default: newline)
     */
    template <typename T>
    void print(const T& val, const char* end="\n") {
        cout << val << end;
    }
    
    /**
     * @brief Prints a boolean value as "YES" or "NO".
     *
     * @param val Boolean value
     * @param end Output terminator (default: newline)
     */
    void print(bool val, const char* end="\n") {
        cout << (val ? "YES" : "NO") << end;
    }
    
    /**
     * @brief Prints a null-terminated C-style string.
     *
     * @param s Pointer to null-terminated string
     * @param end Output terminator (default: newline)
     */
    void print(const char* s, const char* end="\n") {
        cout << s << end;
    }
    
    /**
     * @brief Prints a std::string.
     *
     * @param s String to print
     * @param end Output terminator (default: newline)
     */
    void print(const string& s, const char* end="\n") {
        cout << s << end;
    }
    
    /**
     * @brief Prints elements of a std::vector.
     *
     * Elements are printed recursively and separated by spaces.
     * Trailing spaces are avoided.
     *
     * @tparam T Element type of the vector
     * @param vec Vector to print
     * @param end Output terminator (default: newline)
     */
    template <typename T>
    void print(const vector<T>& vec, const char* end="\n") {
        for (size_t i = 0; i < vec.size(); ++i)
            print(vec[i], 
                  i+1 < vec.size() ? " " : "");

        cout << end;
    }
    
    /**
     * @brief Prints a std::vector<bool>.
     *
     * This overload exists due to std::vector<bool>'s
     * bit-proxy specialization, ensuring correct boolean output.
     *
     * @param vec Vector of booleans
     * @param end Output terminator (default: newline)
     */
    void print(const vector<bool>& vec,
               const char* end="\n") {
        for (bool b : vec) print(b, " ");
        cout << end;
    }
    
    /**
     * @brief Prints a built-in array of fixed size.
     *
     * The array size is deduced at compile time.
     * Supports multi-dimensional arrays via recursion.
     *
     * @tparam T Element type
     * @tparam N Number of elements
     * @param arr Reference to the array
     * @param end Output terminator (default: newline)
     */
    template <typename T, size_t N>
    void print(const T (&arr)[N], const char* end = "\n") {
        for (size_t i = 0; i < N; ++i)
            print(arr[i],
                  i+1 < N ? " " : "");
        cout << end;
    }
    
    /**
     * @brief Prints a fixed-size character array safely.
     *
     * This overload is specifically designed to handle character arrays
     * that are NOT guaranteed to be null-terminated (i.e., not C-strings).
     *
     * Unlike `print(const char*)`, which assumes a null-terminated string
     * and may cause undefined behavior if none is present, this function
     * prints exactly N characters from the array.
     *
     * This prevents buffer over-reads when printing raw character buffers,
     * keys, grids, or fixed-length data.
     *
     * @tparam N Size of the character array
     * @param arr Reference to a character array of size N
     * @param end Output terminator (default: newline)
     *
     * @note String literals (e.g. "Hello") will still prefer the
     *       `print(const char*)` overload due to overload resolution,
     *       ensuring correct C-string behavior.
     *
     * @warning This function does NOT stop at '\\0'. All N characters
     *          are printed unconditionally.
     */
    template <size_t N>
    void print(const char (&arr)[N], const char* end = "\n") {
        for (size_t i = 0; i < N; ++i)
            print(arr[i],
                  i+1 < N ? " " : "");
        cout << end;
    }

};


class Solver {
private:
    Printer printer;
    
public:
    /** @brief Entry point for handling multiple test cases. */
    void execute() {
        int testCases = 1;
        // Comment this out for single test case problems
        cin >> testCases;
        
        while(testCases--) solve();
    }

    /**
     * @brief Interface Layer: Handles data ingestion and output dispatch.
     */
    void solve() {
        // 1. Ingest input parameters
        int n;
        cin >> n;
 
        // 2. Execute computational logic
        auto result = process(n);
 
        // 3. Dispatch result to output stream
        printer.print(result);
    }
 
    /**
     * @brief Engine Layer: Pure logic for a single test case unit.
     * @param n Input parameter (Update signature as per problem)
     * @return Processed result (Update return-type as per problem)
     */
    int process(int n) {
        int result = 0;
                
        // --- Implementation Logic Start ---
        
        // --- Implementation Logic End ---
        
        return result;
    }
};

int main() {
    fastIO();
    
    Solver solver;
    solver.execute();
    
    return 0;
}

πŸ› οΈ Implementation Deep-Dive

🌲 Trees & Advanced Data Structures

Our implementations focus on cache efficiency and generic usability.

  • Segment Tree: Supports vEB (van Emde Boas) layout for better CPU cache locality. Level-aware combine logic allows for complex range queries.

Note

Performance Optimization (vEB Layout): By arranging nodes in a van Emde Boas layout, this implementation significantly reduces CPU cache misses compared to the traditional $2i$ and $2i+1$ indexing. In large-scale benchmarks ($N &gt; 10^5$), this results in a 10–30% speedup for range queries.

  • Splay Tree: A self-adjusting BST that moves frequently accessed nodes to the root.

πŸ•ΈοΈ Graph Algorithms

  • Shortest Path: Dijkstra, Bellman-Ford
  • MST: Prim's, Kruskal's
  • Connectivity: Kosaraju (SCC)

πŸ“Š Performance & Complexity

Template Build Query Space
Segment Tree $O(n)$ $O(\log n)$ $O(n)$
Splay Tree $O(n \log n)$ $O(\log n)^*$ $O(n)$

* Denotes amortized time complexity.


πŸš€ Quick Start

1. Range Queries with Segment Tree

Our Segment Tree is highly flexible, allowing for custom combine logic and level-aware operations.

// Example: Range Sum
std::vector<int> arr = {1, 2, 3, 4, 5};
SegmentTree<int> st(arr); 

int sum = st.query(1, 3); // Returns 9 (2 + 3 + 4)
st.update(2, 10);         // Update index 2 to value 10

2. Dynamic Sets with Splay Tree

The Splay Tree is ideal for scenarios where recently accessed elements need to be retrieved quickly.

// Example: Self-adjusting BST
SplayTree tree = {10, 20, 30, 40};

tree.insert(25);      // Inserts 25 and splays it to the root
bool found = tree.contains(20); // Moves 20 to the root if found
tree.erase(30);       // Removes 30 and restructures optimally