Skip to content
Open
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
Empty file.
110 changes: 110 additions & 0 deletions Common/Coding Templates/Binary Search/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <bits/stdc++.h>
using namespace std;

/*
------------------------------------------
🔹 Approach 1: Classic Iterative Binary Search
- Uses low, high, and mid pointers
- Repeatedly halves the search space
- Returns index if found
------------------------------------------
*/
int binarySearch(int arr[], int n, int target) {
int low = 0;
int high = n - 1;

while (low <= high) {
int mid = low + (high - low) / 2;

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

return -1;
}

/*
------------------------------------------
🔹 Approach 2: Cleaner Iterative Version
- Same logic as Approach 1
- Direct return statements
- More concise code
------------------------------------------
*/
int binarySearch1(int arr[], int n, int target) {
int low = 0, high = n - 1;

while (low <= high) {
int mid = low + (high - low) / 2;

if (arr[mid] == target)
return mid;

if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}

return -1;
}

/*
------------------------------------------
🔹 Approach 3: Recursive Binary Search
- Solves problem recursively
- Divides search range each call
- Elegant and educational
------------------------------------------
*/
int binarySearchRecursive(int arr[], int low, int high, int target) {
if (low > high)
return -1;

int mid = low + (high - low) / 2;

if (arr[mid] == target)
return mid;

if (arr[mid] < target)
return binarySearchRecursive(arr, mid + 1, high, target);

return binarySearchRecursive(arr, low, mid - 1, target);
}

/*
------------------------------------------
🔹 Approach 4: STL binary_search()
- Uses built-in C++ algorithm
- Returns true if element exists
- Simplest implementation
------------------------------------------
*/
bool binarySearch2(vector<int>& arr, int target) {
return binary_search(arr.begin(), arr.end(), target);
}

/*
------------------------------------------
🔹 Approach 5: Using lower_bound()
- Finds first position where target
can be inserted
- Returns index if found
- Common in competitive programming
------------------------------------------
*/
int binarySearch3(vector<int>& arr, int target) {
auto it = lower_bound(arr.begin(), arr.end(), target);

if (it != arr.end() && *it == target)
return it - arr.begin();

return -1;
}
82 changes: 82 additions & 0 deletions Common/Coding Templates/Binary Search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 🔍 Binary Search – C++ Solutions

### 💡 What is Binary Search?

**Binary Search** is an efficient searching algorithm used to find an element in a **sorted array**.

Instead of checking elements one by one like Linear Search, Binary Search repeatedly divides the search space into half.

### Example

Consider the sorted array:

```text
[2, 5, 8, 12, 16, 23, 38]
```

Let's search for **16**.

* Middle element = 12
* Since 16 > 12, search the right half
* New middle = 23
* Since 16 < 23, search the left half
* Element found = 16 ✅

This divide-and-conquer strategy makes Binary Search extremely fast.

---

## 📘 What's Covered in This File?

This program demonstrates **five different ways** to perform Binary Search in C++:

| Approach | Description |
| -------- | -------------------------------- |
| 1 | Classic iterative Binary Search |
| 2 | Cleaner iterative implementation |
| 3 | Recursive Binary Search |
| 4 | STL `binary_search()` |
| 5 | STL `lower_bound()` |

---

## 🧠 Why So Many Approaches?

Each version introduces a different concept:

* 🔁 Iterative problem solving
* 🔙 Recursion
* 📚 C++ Standard Template Library (STL)
* ⚡ Efficient searching techniques
* 🏆 Competitive programming patterns

---

## 🗂 File Structure

* `BinarySearch.cpp`

---

## 🔍 Sample Usage

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

int main() {
vector<int> arr = {2, 5, 8, 12, 16, 23, 38};

int target = 16;

int index = binarySearch(arr, target);

if (index != -1)
cout << "Element found at index: " << index;
else
cout << "Element not found";

return 0;
}
```
43 changes: 43 additions & 0 deletions Common/Coding Templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 💻 Coding Templates in C++

This section contains **essential coding patterns and templates** that are frequently used in problem solving, competitive programming, and technical interviews.

These templates help you recognize patterns and solve problems efficiently instead of starting from scratch every time.

---

## 📘 What's Inside?

Each file provides a reusable template for a common algorithmic pattern.

We will cover:

* Binary Search patterns
* Two Pointers technique
* Sliding Window technique
* Optimized problem-solving approaches

---

## 📂 Problem Directories

Each problem will have its **own folder** with:
- Source code in C++
- Step-by-step explanation

### 🔗 Problem List

- [Binary Search](./Binary%20Search/BinarySearch.cpp)
- [Sliding Window](./Sliding%20Window/SlidingWindow.cpp)
- [Two Pointers](./Two%20Pointers/TwoPointers.cpp)

> ⚠️ Visit Each file and check the code

## 🚀 Getting Started

To run any C++ file:

```bash
g++ filename.cpp -o output
./output
```
Empty file.
86 changes: 86 additions & 0 deletions Common/Coding Templates/Sliding Window/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Sliding Window – C++ Solutions

### 💡 What is the Sliding Window Technique?

The **Sliding Window** technique is a common algorithmic approach used to solve problems involving **subarrays**, **substrings**, or **contiguous sequences** efficiently.

Instead of repeatedly processing the same elements, the window "slides" across the array or string while updating the result incrementally.

This often reduces the time complexity from **O(n²)** to **O(n)**.

### Example

Suppose we want to find the maximum sum of a subarray of size **3**:

```text
[2, 1, 5, 1, 3, 2]
```

Window size = 3

```text
[2,1,5] → Sum = 8
[1,5,1] → Sum = 7
[5,1,3] → Sum = 9
[1,3,2] → Sum = 6
```

Maximum sum = **9** ✅

Instead of calculating every window from scratch, we:

* Remove the outgoing element
* Add the incoming element
* Update the sum efficiently

---

## 📘 What's Covered in This File?

This program demonstrates **five common Sliding Window approaches** in C++:

| Approach | Description |
| -------- | ------------------------------------------------------- |
| 1 | Fixed-size window (Maximum Sum Subarray) |
| 2 | Fixed-size window (Average of Subarrays) |
| 3 | Variable-size window (Smallest Subarray with Given Sum) |
| 4 | Longest Substring Without Repeating Characters |
| 5 | Maximum Consecutive Ones |

---

## 🧠 Why Learn Sliding Window?

The Sliding Window technique is widely used for:

* 📊 Array problems
* 🔤 String problems
* ⚡ Optimization
* 🏆 Competitive programming
* 💼 Technical interviews

Many problems that seem O(n²) can be solved in O(n) using Sliding Window.

---

## 🗂 File Structure

* `SlidingWindow.cpp`

---

## 🔍 Sample Usage

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

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

cout << maxSumSubarray(arr, 3);

return 0;
}
```
Loading