Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Meet in the middle #3032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Ridham-sharma19 wants to merge 2 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from Ridham-sharma19:meet-in-the-middle
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
16 changes: 16 additions & 0 deletions MeetInTheMiddle/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Meet in the Middle

## 📘 Overview
**Meet in the Middle** is an optimization technique used for solving problems
that would otherwise take **exponential time (O(2^N))**.
It divides the problem into two halves and combines the results,
reducing complexity to **O(2^(N/2))**, which is much faster.

---

## ⚙️ How It Works
1. **Divide** the input set or problem into two halves.
2. **Generate all possible results** (like subset sums) for each half.
3. **Sort one list** and use **binary search or two-pointer** techniques
to efficiently combine results from both halves.

90 changes: 90 additions & 0 deletions MeetInTheMiddle/subset_sum_meet_in_middle.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @file subset_sum_meet_in_middle.cpp
* @brief Count subsets with sum <= target using the Meet in the Middle
* technique.
* @details
* The Meet in the Middle algorithm splits the array into two halves,
* generates all subset sums for each half, then efficiently counts
* valid combinations using binary search.
* @see https://cp-algorithms.com/combinatorics/meet_in_the_middle.html
* @author [Your Name](https://github.com/your-github-handle)
*/

#include <algorithm> ///< for std::sort, std::upper_bound
#include <cassert> ///< for assert()
#include <iostream> ///< for std::cout
#include <vector> ///< for std::vector

/**
* @brief Generates all subset sums for the range arr[start..end]
* @param arr Input array
* @param sums Vector to store generated sums
* @param start Start index
* @param end End index
* @param curr Current sum (default 0)
*/
void generateSubsetSums(const std::vector<int>& arr,
std::vector<long long>& sums, int start, int end,
long long curr = 0) {
if (start > end) {
sums.push_back(curr);
return;
}
generateSubsetSums(arr, sums, start + 1, end,
curr); // Exclude current element
generateSubsetSums(arr, sums, start + 1, end,
curr + arr[start]); // Include current element
}

/**
* @brief Counts subsets with sum <= target using Meet in the Middle
* @param arr Input array
* @param target Maximum allowed sum
* @return Number of subsets with sum <= target
*/
long long countSubsetsMeetInMiddle(const std::vector<int>& arr, int target) {
int n = arr.size();
std::vector<long long> left, right;

// Generate subset sums for left and right halves
generateSubsetSums(arr, left, 0, n / 2 - 1);
generateSubsetSums(arr, right, n / 2, n - 1);

std::sort(right.begin(), right.end());

long long count = 0;
for (auto s : left) {
// Count combinations with sum <= target using binary search
count += std::upper_bound(right.begin(), right.end(), target - s) -
right.begin();
}

return count;
}

/**
* @brief Self-test to verify the implementation
*/
static void tests() {
// Test case 1
std::vector<int> arr1 = {3, 5, 7, 9};
assert(countSubsetsMeetInMiddle(arr1, 12) == 9);

// Test case 2
std::vector<int> arr2 = {1, 2, 3};
assert(countSubsetsMeetInMiddle(arr2, 3) == 5);

// Test case 3
std::vector<int> arr3 = {4, 5, 6, 7};
assert(countSubsetsMeetInMiddle(arr3, 10) == 7);

std::cout << "All self-tests passed successfully!\n";
}

/**
* @brief Main function
*/
int main() {
tests();
return 0;
}

AltStyle によって変換されたページ (->オリジナル) /