-
-
Couldn't load subscription status.
- Fork 7.6k
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
from
Ridham-sharma19:meet-in-the-middle
Open
Meet in the middle #3032
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.