Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,45 @@ bin-release/
# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
# should NOT be excluded as they contain compiler settings and other important
# information for Eclipse / Flash Builder.

# Competitive Programming specific
# Compiled binaries
*.exe
*.o
*.out
a.out

# IDE files
.vscode/
.idea/
*.iml

# Language specific
# Python
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
*.so

# Java
*.class
*.jar

# C++
*.gch
*.pch

# System files
.DS_Store
Thumbs.db

# Temporary files
*.tmp
*.temp
*~

# Test files (optional - remove if you want to commit test outputs)
test_output.txt
debug.txt
108 changes: 108 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Contributing to Competitive Programming and Algorithms

Thank you for your interest in contributing to this repository! This guide will help you understand the project structure and contribution guidelines.

## Repository Structure

```
├── algorithms/ # Algorithm implementations
│ ├── graph/ # Graph algorithms
│ ├── dynamic-programming/ # DP solutions
│ ├── greedy/ # Greedy algorithms
│ ├── string/ # String algorithms
│ ├── sorting/ # Sorting algorithms
│ ├── searching/ # Search algorithms
│ └── math/ # Mathematical algorithms
├── data-structures/ # Data structure implementations
│ ├── linear/ # Arrays, lists, stacks, queues
│ ├── trees/ # Trees and tree-based structures
│ ├── graphs/ # Graph representations
│ ├── heaps/ # Heap implementations
│ └── hash-tables/ # Hash-based structures
├── problems/ # Problem solutions by platform
│ ├── leetcode/ # LeetCode solutions
│ ├── codeforces/ # Codeforces solutions
│ ├── atcoder/ # AtCoder solutions
│ ├── hackerrank/ # HackerRank solutions
│ ├── codechef/ # CodeChef solutions
│ └── spoj/ # SPOJ solutions
└── templates/ # Code templates and boilerplate
```

## Contribution Guidelines

### Adding Algorithm Implementations

1. Place the algorithm in the appropriate subdirectory under `algorithms/`
2. Include comprehensive documentation:
- Brief description of the algorithm
- Time and space complexity analysis
- When to use this algorithm
- Example usage

### Adding Data Structure Implementations

1. Place implementations in the appropriate subdirectory under `data-structures/`
2. Include:
- Clear interface documentation
- Complexity analysis for all operations
- Example usage
- Test cases demonstrating functionality

### Adding Problem Solutions

1. Choose the appropriate platform directory under `problems/`
2. Use descriptive file names: `problem_id_brief_description.ext`
3. Include at the top of each solution:
```
Problem: [Problem Title]
Link: [URL to problem]
Difficulty: [Easy/Medium/Hard]

Approach: [Brief explanation of your approach]
Time Complexity: O(...)
Space Complexity: O(...)
```

### Code Quality Standards

- **Readability**: Write clean, well-commented code
- **Efficiency**: Focus on optimal time and space complexity
- **Testing**: Include test cases when applicable
- **Documentation**: Explain complex logic and edge cases

### File Naming Conventions

- Use lowercase with underscores: `binary_search.cpp`
- Include relevant keywords: `dijkstra_shortest_path.py`
- For problems, include platform identifier: `lc_1480_two_sum.cpp`

### Language Support

We accept solutions in:
- C++ (preferred for competitive programming)
- Python
- Java
- Other languages are welcome

### Submitting Changes

1. Fork the repository
2. Create a feature branch for your changes
3. Follow the structure and naming conventions
4. Add appropriate documentation
5. Test your implementations
6. Submit a pull request with a clear description

## Getting Started

1. Use the templates in `templates/` for consistent code structure
2. Check existing implementations for reference
3. Read the README files in each directory for specific guidelines
4. Start with simpler algorithms or problems if you're new to the repository

## Questions?

If you have questions about contributing, feel free to open an issue or reach out to the maintainers.

Happy coding! 🚀
74 changes: 72 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
# competitive-programming-and-algorithms
Solutions to world renowed problems and explanations of legacy algorithms. This is not a research, but my personal findings and understanding
# Competitive Programming and Algorithms

A comprehensive collection of algorithm implementations, data structures, and solutions to competitive programming problems. This repository serves as a learning resource and reference for competitive programmers and algorithm enthusiasts.

## Repository Structure

### 📚 **Algorithms**
Fundamental algorithms organized by category:
- **Graph Algorithms**: DFS, BFS, Dijkstra, Floyd-Warshall, MST
- **Dynamic Programming**: Classic DP patterns and optimizations
- **Greedy Algorithms**: Optimization and scheduling problems
- **String Algorithms**: Pattern matching, string processing
- **Sorting & Searching**: Various implementations and applications
- **Mathematical Algorithms**: Number theory, combinatorics

### 🏗️ **Data Structures**
Essential data structures for competitive programming:
- **Linear**: Arrays, linked lists, stacks, queues
- **Trees**: Binary trees, segment trees, Fenwick trees, tries
- **Graphs**: Adjacency lists, matrices, specialized structures
- **Heaps**: Priority queues, min/max heaps
- **Hash Tables**: Efficient lookup structures

### 💻 **Problems**
Solutions from popular competitive programming platforms:
- **LeetCode**: Interview preparation problems
- **Codeforces**: Contest problems and solutions
- **AtCoder**: Japanese competitive programming platform
- **HackerRank**: Coding challenges and algorithms
- **CodeChef**: Monthly contests and practice problems
- **SPOJ**: Classical programming problems

### 🛠️ **Templates**
Ready-to-use code templates for:
- C++ competitive programming setup
- Python algorithm implementations
- Java contest templates
- Fast I/O and common utilities

## Getting Started

1. **Browse** the directory structure to find relevant algorithms or problems
2. **Use** the templates for quick contest setup
3. **Refer** to individual README files in each directory for specific guidance
4. **Contribute** by adding new solutions or improving existing ones

## Contributing

We welcome contributions! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on:
- Code quality standards
- File naming conventions
- Documentation requirements
- Submission process

## Languages

Solutions are primarily in:
- **C++** (preferred for competitive programming)
- **Python** (for readability and quick prototyping)
- **Java** (for specific contest requirements)

## Philosophy

This repository focuses on:
- **Clarity**: Well-documented, readable code
- **Efficiency**: Optimal time and space complexity
- **Learning**: Comprehensive explanations and examples
- **Practice**: Real problem solutions from contests

---

*Happy coding and may your solutions be bug-free! 🚀*
20 changes: 20 additions & 0 deletions algorithms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Algorithms

This directory contains implementations of fundamental algorithms commonly used in competitive programming and software development.

## Directory Structure

- **graph/**: Graph algorithms (DFS, BFS, Dijkstra, Floyd-Warshall, MST, etc.)
- **dynamic-programming/**: Dynamic programming solutions and patterns
- **greedy/**: Greedy algorithms and optimization problems
- **string/**: String processing algorithms (KMP, Z-algorithm, suffix arrays, etc.)
- **sorting/**: Various sorting algorithms implementations
- **searching/**: Binary search and other searching techniques
- **math/**: Mathematical algorithms (number theory, combinatorics, etc.)

## Implementation Guidelines

- Each algorithm should be well-documented with complexity analysis
- Include example usage and test cases
- Prefer clear, readable code over micro-optimizations
- Add comments explaining the algorithm's key concepts
109 changes: 109 additions & 0 deletions algorithms/searching/binary_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Binary Search Algorithm
*
* Description: Efficiently searches for a target value in a sorted array
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Use Cases:
* - Finding element in sorted array
* - Lower/upper bound queries
* - Search space optimization problems
*/

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

/**
* Standard binary search - finds exact match
* @param arr: sorted array to search in
* @param target: value to find
* @return: index of target if found, -1 otherwise
*/
int binarySearch(const vector<int>& arr, int target) {
int left = 0, right = arr.size() - 1;

while (left <= right) {
int mid = left + (right - left) / 2; // Avoid overflow

if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return -1; // Not found
}

/**
* Lower bound - finds first position where element >= target
* @param arr: sorted array
* @param target: target value
* @return: index of first element >= target
*/
int lowerBound(const vector<int>& arr, int target) {
int left = 0, right = arr.size();

while (left < right) {
int mid = left + (right - left) / 2;

if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}

return left;
}

/**
* Upper bound - finds first position where element > target
* @param arr: sorted array
* @param target: target value
* @return: index of first element > target
*/
int upperBound(const vector<int>& arr, int target) {
int left = 0, right = arr.size();

while (left < right) {
int mid = left + (right - left) / 2;

if (arr[mid] <= target) {
left = mid + 1;
} else {
right = mid;
}
}

return left;
}

// Example usage and test cases
int main() {
vector<int> arr = {1, 3, 5, 7, 9, 11, 13, 15};

cout << "Array: ";
for (int x : arr) cout << x << " ";
cout << "\n\n";

// Test binary search
cout << "Binary Search Tests:\n";
cout << "Search for 7: " << binarySearch(arr, 7) << " (expected: 3)\n";
cout << "Search for 4: " << binarySearch(arr, 4) << " (expected: -1)\n";
cout << "Search for 1: " << binarySearch(arr, 1) << " (expected: 0)\n";
cout << "Search for 15: " << binarySearch(arr, 15) << " (expected: 7)\n\n";

// Test bounds
cout << "Bounds Tests:\n";
cout << "Lower bound of 7: " << lowerBound(arr, 7) << " (expected: 3)\n";
cout << "Lower bound of 6: " << lowerBound(arr, 6) << " (expected: 3)\n";
cout << "Upper bound of 7: " << upperBound(arr, 7) << " (expected: 4)\n";
cout << "Upper bound of 6: " << upperBound(arr, 6) << " (expected: 3)\n";

return 0;
}
18 changes: 18 additions & 0 deletions data-structures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Data Structures

This directory contains implementations of various data structures used in competitive programming.

## Directory Structure

- **linear/**: Arrays, linked lists, stacks, queues, deques
- **trees/**: Binary trees, BSTs, segment trees, Fenwick trees, tries
- **graphs/**: Graph representations and specialized graph data structures
- **heaps/**: Min/max heaps, priority queues
- **hash-tables/**: Hash map implementations and related structures

## Implementation Guidelines

- Focus on functionality needed for competitive programming
- Include common operations with time complexity analysis
- Provide clear examples of usage
- Test implementations thoroughly
Loading