How to get the equilibrium index of an array in O(n)? - c++
I have done a test in C++ asking for a function that returns one of the indices that splits the input vector in 2 parts having the same sum of the elements, for eg: for the vec = {1, 2, 3, 5, 4, -1, 1, 1, 2, -1}, it may return 3, because 1+2+3 = 6 = 4-1+1+1+2-1. So I have done the function that returns the correct answer:
int func(const std::vector< int >& vecIn)
{
for (std::size_t p = 0; p < vecin.size(); p++)
{
if (std::accumulator(vecIn.begin(), vecIn.begin() + p, 0) ==
std::accumulator(vecIn.begin() + p + 1, vecIn.end(), 0))
return p;
}
return -1;
}
My problem was when the input was a very long vector containing just 1 (or -1), the return of the function was slow. So I have thought of starting the search for the wanted index from middle, and then go left and right. But the best approach I suppose is the one where the index is in the merge-sort algorithm order, that means: n/2, n/4, 3n/4, n/8, 3n/8, 5n/8, 7n/8... where n is the size of the vector. Is there a way to write this order in a formula, so I can apply it in my function?
Thanks
EDIT
After some comments I have to mention that I had done the test a few days ago, so I have forgot to put and mention the part of no solution: it should return -1... I have updated also the question title.
Specifically for this problem, I would use the following algorithm:
Compute the total sum of the vector. This gives two sums (empty vector, and full vector)
for each element in order, move one element from full to empty, which means adding the value of next element from sum(full) to sum(empty). When the two sums are equal, you have found your index.
This give a o(n) algorithm instead of o(n2)
You can solve the problem much faster without calling std::accumulator at each step:
int func(const std::vector< int >& vecIn)
{
int s1 = 0;
int s2 = std::accumulator(vecIn.begin(), vecIn.end(), 0);
for (std::size_t p = 0; p < vecin.size(); p++)
{
if (s1 == s2)
return p;
s1 += vecIn[p];
s2 -= vecIn[p];
}
}
This is O(n). At each step, s1 will contain the sum of the first p elements, and s2 the sum of the rest. You can update both of them with an addition and a subtraction when moving to the next element.
Since std::accumulator needs to iterate over the range you give it, your algorithm was O(n^2), which is why it was so slow for many elements.
To answer the actual question: Your sequence n/2, n/4, 3n/5, n/8, 3n/8 can be rewritten as
1*n/2
1*n/4 3*n/4
1*n/8 3*n/8 5*n/8 7*n/8
...
that is to say, the denominator runs from i=2 up in powers of 2, and the nominator runs from j=1 to i-1 in steps of 2. However, this is not what you need for your actual problem, because the example you give has n=10. Clearly you don't want n/4 there - your indices have to be integer.
The best solution here is to recurse. Given a range [b,e], pick a value middle (b+e/2) and set the new ranges to [b, (b+e/2)-1] and [(b+e/2)=1, e]. Of course, specialize ranges with length 1 or 2.
Considering MSalters comments, I'm afraid another solution would be better. If you want to use less memory, maybe the selected answer is good enough, but to find the possibly multiple solutions you could use the following code:
static const int arr[] = {5,-10,10,-10,10,1,1,1,1,1};
std::vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
// compute cumulative sum
std::vector<int> cumulative_sum( vec.size() );
cumulative_sum[0] = vec[0];
for ( size_t i = 1; i < vec.size(); i++ )
{ cumulative_sum[i] = cumulative_sum[i-1] + vec[i]; }
const int complete_sum = cumulative_sum.back();
// find multiple solutions, if there are any
const int complete_sum_half = complete_sum / 2; // suggesting this is valid...
std::vector<int>::iterator it = cumulative_sum.begin();
std::vector<int> mid_indices;
do {
it = std::find( it, cumulative_sum.end(), complete_sum_half );
if ( it != cumulative_sum.end() )
{ mid_indices.push_back( it - cumulative_sum.begin() ); ++it; }
} while( it != cumulative_sum.end() );
for ( size_t i = 0; i < mid_indices.size(); i++ )
{ std::cout << mid_indices[i] << std::endl; }
std::cout << "Split behind these indices to obtain two equal halfs." << std::endl;
This way, you get all the possible solutions. If there is no solution to split the vector in two equal halfs, mid_indices will be left empty.
Again, you have to sum up each value only once.
My proposal is this:
static const int arr[] = {1,2,3,5,4,-1,1,1,2,-1};
std::vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
int idx1(0), idx2(vec.size()-1);
int sum1(0), sum2(0);
int idxMid = -1;
do {
// fast access without using the index each time.
const int& val1 = vec[idx1];
const int& val2 = vec[idx2];
// Precompute the next (possible) sum values.
const int nSum1 = sum1 + val1;
const int nSum2 = sum2 + val2;
// move the index considering the balanace between the
// left and right sum.
if ( sum1 - nSum2 < sum2 - nSum1 )
{ sum1 = nSum1; idx1++; }
else
{ sum2 = nSum2; idx2--; }
if ( idx1 >= idx2 ){ idxMid = idx2; }
} while( idxMid < 0 && idx2 >= 0 && idx1 < vec.size() );
std::cout << idxMid << std::endl;
It does add every value only once no matter how many values. Such that it's complexity is only O(n) and not O(n^2).
The code simply runs from left and right simultanuously and moves the indices further if it's side is lower than the other.
You want nth term of the series you mentioned. Then it would be:
numerator: (n - 2^((int)(log2 n)) ) *2 + 1
denominator: 2^((int)(log2 n) + 1)
I came across the same question in Codility tests. There is a similar looking answer above (didn't pass some of the unit tests), but below code segment was successful in tests.
#include <vector>
#include <numeric>
#include <iostream>
using namespace std;
// Returns -1 if equilibrium point is not found
// use long long to support bigger ranges
int FindEquilibriumPoint(vector<long> &values) {
long long lower = 0;
long long upper = std::accumulate(values.begin(), values.end(), 0);
for (std::size_t i = 0; i < values.size(); i++) {
upper -= values[i];
if (lower == upper) {
return i;
}
lower += values[i];
}
return -1;
}
int main() {
vector<long> v = {-1, 3, -4, 5, 1, -6, 2, 1};
cout << "Equilibrium Point:" << FindEquilibriumPoint(v) << endl;
return 0;
}
Output
Equilibrium Point:1
Here it is the algorithm in Javascript:
function equi(arr){
var N = arr.length;
if (N == 0){ return -1};
var suma = 0;
for (var i=0; i<N; i++){
suma += arr[i];
}
var suma_iz = 0;
for(i=0; i<N; i++){
var suma_de = suma - suma_iz - arr[i];
if (suma_iz == suma_de){
return i};
suma_iz += arr[i];
}
return -1;
}
As you see this code satisfy the condition of O(n)
Related
Count how many iterations of deletion until array is ordered
I'm trying to write a program whose input is an array of integers, and its size. This code has to delete each element which is smaller than the element to the left. We want to find number of times that we can process the array this way, until we can no longer delete any more elements. The contents of the array after we return are unimportant - only the return value is of interest. For example: given the array [10, 9, 7, 8, 6, 5, 3, 4, 2, 1], the function should return 2, because: [10,9,7,8,6,5,3,4,2,1] → [10,8,4] → [10] For example: given the array [1,2,3,4], the function should return 0, because No element is larger than the element to its right I want each element to remove the right element if it is more than its right element. We get a smaller array. Then we repeat this operation again. Until we get to an array in which no element can delete another element. I want to calculate the number of steps performed. int Mafia(int n, vector <int> input_array) { int ptr = n; int last_ptr = n; int night_Count = 0; do { last_ptr = ptr; ptr = 1; for (int i = 1; i < last_ptr; i++) { if (input_array[i] >= input_array[i - 1]) { input_array[ptr++] = input_array[i]; } } night_Count++; } while (last_ptr > ptr); return night_Count - 1; } My code works but I want it to be faster. Do you have any idea to make this code faster, or another way that is faster than this?
Here is a O(NlogN) solution. The idea is to iterate over the array and keep tracking candidateKillers which could kill unvisited numbers. Then we find the killer for the current number by using binary search and update the maximum iterations if needed. Since we iterate over the array which has N numbers and apply log(N) binary search for each number, the overall time complexity is O(NlogN). Alogrithm If the current number is greater or equal than the number before it, it could be a killer for numbers after it. For each killer, we keep tracking its index idx, the number of it num and the iterations needed to reach that killer iters. The numbers in the candidateKillers by its nature are non-increasing (see next point). Therefore we can apply binary search to find the killer of the current number, which is the one that is a) the closest to the current number b) greater than the current number. This is implemented in searchKiller. If the current number will be killed by a number in candidateKillers with killerPos, then all candidate killers after killerPos are outdated, because those outdated killers will be killed before the numbers after the current number reach them. If the current number is greater than all candidateKillers, then all the candidateKillers can be discarded. When we find the killer of the current number, we increase the iters of the killer by one. Because from now on, one more iteration is needed to reach that killer where the current number need to be killed first. class Solution { public: int countIterations(vector<int>& array) { if (array.size() <= 1) { return 0; } int ans = 0; vector<Killer> candidateKillers = {Killer(0, array[0], 1)}; for (auto i = 1; i < array.size(); i++) { int curNum = array[i]; int killerPos = searchKiller(candidateKillers, curNum); if (killerPos == -1) { // current one is the largest so far and all candidateKillers before are outdated candidateKillers = {Killer(i, curNum, 1)}; continue; } // get rid of outdated killers int popCount = candidateKillers.size() - 1 - killerPos; for (auto j = 0; j < popCount; j++) { candidateKillers.pop_back(); } Killer killer = candidateKillers[killerPos]; ans = max(killer.iters, ans); if (curNum < array[i-1]) { // since the killer of the current one may not even be in the list e.g., if current is 4 in [6,5,4] if (killer.idx == i - 1) { candidateKillers[killerPos].iters += 1; } } else { candidateKillers[killerPos].iters += 1; candidateKillers.push_back(Killer(i, curNum, 1)); } } return ans; } private: struct Killer { Killer(int idx, int num, int iters) : idx(idx), num(num), iters(iters) {}; int idx; int num; int iters; }; int searchKiller(vector<Killer>& candidateKillers, int n) { int lo = 0; int hi = candidateKillers.size() - 1; if (candidateKillers[0].num < n) { return -1; } int ans = -1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (candidateKillers[mid].num > n) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } }; int main() { vector<int> array1 = {10, 9, 7, 8, 6, 5, 3, 4, 2, 1}; vector<int> array2 = {1, 2, 3, 4}; vector<int> array3 = {4, 2, 1, 2, 3, 3}; cout << Solution().countIterations(array1) << endl; // 2 cout << Solution().countIterations(array2) << endl; // 0 cout << Solution().countIterations(array3) << endl; // 4 }
You can iterate in reverse, keeping two iterators or indices and moving elements in place. You don't need to allocate a new vector or even resize existing vector. Also a minor, but can replace recursion with loop or write the code the way compiler likely to do it. This approach is still O(n^2) worst case but it would be faster in run time.
given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A
I tried to solve this exercise I got 66 percent I can not understand why can you help? The exercise is: Write a function: int solution(vector &A); that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. Given A = [1, 2, 3], the function should return 4. Given A = [−1, −3], the function should return 1. The solution I wrote is: #include <algorithm> #include<cmath> using namespace std; int solution(vector<int> &A) { if (A.size() == 0 || (A.size() == 1 && A.at(0) <= 0)) return 1; if (A.size() == 1) return A.at(0) + 1; sort(A.begin(), A.end()); if (A.at(A.size() - 1) <= 0) return 1; auto ip = std::unique(A.begin(), A.end()); A.resize(distance(A.begin(), ip)); A.erase(remove_if(A.begin(), A.end(), [](const int i) { return i < 0; }),A.end()); if (A.at(0) != 1) return 1; if (A.size() == 1) return (A.at(0) != 1 ? 1 : 2); int i = 0; for (; i < A.size(); ++i) { if (A.at(i) != i + 1) return A.at(i - 1) + 1; } return A.at(A.size()) + 1; }
The following algorithm has a complexity O(n). No need to sort or to erase. We know that the first missing value is less or equal to n+1, if n is the array size. Then we simply have to use an array of size n+2, present[n+2], initialised to 0, and then to look at all values A[i]: if (A[i] <= 1+n && A[i] > 0) present[A[i]] = 1; Finally, in a second step we simply have to examine the array present[.], and search for the first index k such that present[k]==0. #include <iostream> #include <vector> int find_missing (const std::vector<int> &A) { int n = A.size(); std::vector<int> present (n+2, 0); int vmax = n+1; for (int i = 0; i < n; ++i) { if (A[i] <= vmax && A[i] > 0) { present[A[i]] = 1; } } for (int k = 1; k <= vmax; ++k) { if (present[k] == 0) return k; } return -1; } int main() { std::vector<int> A = {1, 2, 0, 3, -3, 5, 6, 8}; int missing = find_missing (A); std::cout << "First missing element = " << missing << std::endl; return 0; }
Well this is wrong if(A.size()==1) return A.at(0)+1; If A is {2} that code will return 3 when the correct answer is 1 Also A.erase(remove_if(A.begin(), A.end(),[](const int i) {return i < 0; }),A.end()); should be A.erase(remove_if(A.begin(), A.end(),[](const int i) {return i <= 0; }),A.end()); Also return A.at(A.size()) + 1; is a guaranteed vector out of bounds error. Even a small amount of testing and debugging would have caught these errors. It's a habit you should get into. I think there are far too many special cases in the code, which only serve to complicate the code and increase the chance of bugs.
This answer is the implementation of the proposal given in the comment by PaulMcKenzie. So, all credits go to PaulMcKenzie It is not the fastest solution, but compact. The idea is basically. Sort the data Then compare the adjacent values, if the next value is equal to the previous value+1. If not, then we found a gap. This can be implemented by using the function std::adjacent_find. Description can be found here. We put all the side conditions into the lambda. If std::adjacent_find cannot find a value, then we take the next possible positive value. I am not sure, what I could describe more. Please see the below example: #include <iostream> #include <vector> #include <algorithm> int solution(std::vector<int>& data) { // Sort std::sort(data.begin(), data.end()); // Check if there is a gap in the positive values const auto gap = std::adjacent_find(data.begin(), data.end(), [](const int p, const int n) { return (n !=p) && (n != (p + 1) && p>0); }); // If there is no gap, the take the next positive value return (gap == data.end()) ? (data.back() > 0 ? data.back() + 1 : 1) : *gap + 1; } int main() { //Some test cases std::vector<std::vector<int>> testCases{ {1,3,6,4,1,2}, {1,2,3}, {-1,-3} }; for (auto& testCase : testCases) std::cout << solution(testCase) << '\n'; return 0; }
others have already pointed out what are the main errors, but I would like to invite you to try a different solution instead of trying to fix all the bugs and spend much time on debugging, because your solution seems a little overcomplicated. Here I propose a way you can think about the problem: What is the minimum number the function can return? Since it returns a positive integer, it is 1, in the case 1 is not in the array. Since that we can use any number <=0 to see if we found our result scanning the vector (see next); In case one is not in the array, how do I find the wanted number? Your intuition is correct, if your vector is sorted it is easier: you can iterate over your data, and when you find an "hole" between two subsequent elements, then the value of the first element of the hole + 1 is you result What do I do if the array contains 1 and has no holes? Well, you return the smallest element that is not in the array, so the last element + 1. You may notice that by checking if your "candidate" value (that is a number that shouldn't be returned, so <=0) has changed during the scanning; Let's go to the code: int solution(std::vector<int>& v){ int retVal=0; std::sort(v.begin(), v.end()); for(int i=0; i<v.size()-1; i++){ if(v[i]>0 && v[i+1]>v[i]+1){ retVal=v[i]+1; break; } } if(retVal==0) { if (v.back() > 0) retVal = v.back() + 1; else retVal = 1; } return retVal; } As suggested you can use the standard library a little bit more, but I think this is reasonably simple and efficient. Other note: I think your assignment does not bother you with this, but I mention just for completeness. Most of the times you don't want a function to modify your parameters: you can pass the vector "by value" meaning that actually you pass a complete copy of your data, without touching the original one, or you can pass a const reference and create a copy inside the function.
Vector custom sum
I need to identify the position of a variable from an integer array who has the following properties: the sum of elements before this variable is equal with the sum of elements after this variable if the variable doesn't exist, i will show a message. For example, if x = {1,2,4,2,1}, the result is 4 with position 2, because 1 + 2 == 2 + 1. Any suggestions? In this example it's easy if((x[0]+x[1])==(x[3]+x[4])) print position 2 But for n variables?
There are several ways to do this: Brute force - n/2 passes: Loop through the array. For each element calculate the sum before and after that element. If they match you found the element. If the sum before becomes larger than the sum after, stop processing - no match found. This is not really efficient for larger arrays. 1.5 passes: Calculate the sum of all elements. Divide that sum by 2 (half_sum). Start summing the elements again from the beginning until you reach half_sum. Check if you found a valid element or not. Single pass (positive numbers only): Keep two running sums: one from the beginning (sum1) and one from the end (sum2). Set sum1 = first element and sum2 = last element. Check for the smallest of the two and add the next/previous element to that. Loop until the positions meet and check if the element is a valid result. For each method you'll have to do a litlle check first to see if the array is not too small. Special cases to consider: Empty array: return false Array with 1 element: return element Array with 2 nonzero elements: return false What with all zero's, or groups of zero's in the middle? (see Deduplicator's comment) Negative elements: single pass version will not work here (see Cris Luengo's comment) Negative elements in general: not reliable, consider +3 +1 -1 +1 -1 +3 +1 (see Deduplicator's comment)
Here is the O(n) solution. Keep summing in in one variable from array beginning(left_sum) and keep deducing from the sum of elements except the first one using another(right_sum). When both becomes equal break the loop and print. Otherwise, show your msg. #include <iostream> #include <vector> #include <numeric> #include <cstddef> int main() { std::vector<int> vec {1,2,4,2,1}; int left_sum = 0; int right_sum = std::accumulate(vec.cbegin()+1, vec.cend(), 0); bool Okay = false; std::size_t index = 1; // start from index 1 until n-1 for( ; index < vec.size() - 1; ++index) { left_sum += vec[index-1]; right_sum -= vec[index]; if(left_sum == right_sum) { Okay = true; break; } // in the case of array of positive integers // if(left_sum > right_sum) break; } (Okay) ? std::cout << vec[index] << " " << index << std::endl: std::cout << "No such case!\n"; return 0; }
Thanks for answers. I finally managed it. I used 3 for loops, and s0 is for sum before the element, and s1 is the sum after the element. for(i=0;i<n;i++) {s1=0; s0=0; for(int j=0;j<i-1;j++) s0=s0+v[j]; for(int k=i;k<n;k++) s1=s1+v[k]; if(s0==s1) {cout<<endl<<"Position i="<<i; x++;} } if(x==0) cout<<"doesnt exist";
Well, do it in two steps: Sum all elements. From first to last: If the sum equals the current element, success! Subtract it twice from the sum (once for no longer being on the right, once for being on the left). Use standard algorithms and range-for, and it's easily written: auto first_balanced(std::span<const int> x) noexcept { auto balance = std::accumulate(begin(x), end(x), 0LL); for (auto&& n : x) { if (balance == n) return &n; balance -= 2 * n; } return end(x); }
It's just looping. You need to sum the elements before and after each index and just compare these two sums: #include <iostream> #include <vector> #include <numeric> int main() { std::vector<int> x = {1, 2, 4, 2, 1}; for ( unsigned idx = 0; idx < x.size(); ++idx ) if ( std::accumulate(x.begin(), x.begin() + idx, 0) == std::accumulate(x.begin() + idx + 1, x.end(), 0) ) std::cout << idx << std::endl; return 0; }
Trying to build a solution out of std::algorithm, n+lg n instead of n+~n/2 Warning untested code. bool HasHalfSum(int& atIndex, const std::vector<int>& v) { std::vector<int> sum; sum.reserve(v.size); std::partial_sum(v.begin(), v.end(), std::back_iterator(sum)); // 1,3,7,9,10 int half = sum.back() / 2; // 5 auto found = std::lower_bound(sum.begin(), sum.end(), half); if (found != sum.begin() && std::prev(found) == sum.back() - *found) { index = std::distance(sum.begin(), found); return true; } return false; }
Maximum value of M digits out of N digits [duplicate]
This question already has answers here: How to get the least number after deleting k digits from the input number (11 answers) Closed 6 years ago. I am trying to code a program that can do something like this: in: 5 4 1 9 9 9 0 out: 9990 and i have a problem. It doesnt work on any set of numbers. For example it works for the one above, but it doesnt work for this one: in: 15 9 2 9 3 6 5 8 8 8 8 7 2 2 8 1 4 out: 988887814 2 9 3 6 5 8 8 8 8 7 2 2 8 1 4 I did this with a vector approach and it works for any set of numbers, but i'm trying to do it a stack for a better complexity. EDIT ---- MODIFIED FOR STD::STACK Code for method using stack: #include <iostream> #include <fstream> #include <stack> using namespace std; ifstream in("trompeta.in"); ofstream out("trompeta.out"); void reverseStack(stack<char> st) { if(!st.empty()) { char x = st.top(); st.pop(); reverseStack(st); out<<x; } return; } int main() { int n,m,count=1; stack <char> st; char x; in>>n>>m; in>>x; st.push(x); for(int i=1; i<n; i++) { in>>x; if(st.top()<x && count+n-i-1>=m) { st.pop(); st.push(x); } else { st.push(x); count++; if (count>m-1) break; } }; reverseStack(st); } Code for method using vectors: #include <iostream> #include <fstream> using namespace std; ifstream in ( "trompeta.in" ); ofstream out ( "trompeta.out" ); int main () { int i = 0, N, M, max, j, p = 0, var; in >> N >> M; char* v = new char[N]; char* a = new char[M]; in >> v; var = M; max = v[0]; for ( i = 0; i < M; i++ ) { for ( j = p ; j < N-var+1; j++ ) { if ( v[j] > max ) { max = v[j]; p = j; } } var--; a[i] = max; max = v[p+1]; p = p+1; } for ( i = 0; i < M; i++ ) out << a[i]-'0'; } Can any1 help me to get the STACK code working?
Using the fact that the most significant digit completely trumps all other digets except in place of a tie, I would look at the first (N-M+1) digits, find the largest single digit in that range. If it occurs once, the first digit is locked in. Discard the digits which occur prior to that position, and you repeat for "maximum value of M-1 numbers of out N-position" to find the remaining digits of the answer. (or N-position-1, if position is zero based) If it occurs multiple times, then recursively find "maximum value of M-1 numbers out of N-position" for each, then select the largest single result from these. There can be at most N such matches. I forgot to mention, if N==M, you are also done. proof of recursion: Computing the value of the sub-match will always select M-1 digits. When M is 1, you only need to select the largest of a few positions, and have no more recursion. This is true for both cases. Also the "select from" steps always contain no more than N choices, because they are always based on selecting one most significant digit. ------------------ how you might do it with a stack ---------------- An actual implementation using a stack would be based on an object which contains the entire state of the problem, at each step, like so: struct data { // require: n == digits.size() int n, m; std::string digits; bool operator<(const data &rhs){ return digits < rhs.digits; } }; The point of this is not just to store the original problem, but to have a way to represent any subproblem, which you can push and pop on a stack. The stack itself is not really important, here, because it is used to pick the one best result within a specific layer. Recursion handles most of the work. Here is the top level function which hides the data struct: std::string select_ordered_max(int n, int m, std::string digits) { if (n < m || (int)digits.size() != n) return "size wrong"; data d{ n, m, digits }; data answer = select_ordered_max(d); return answer.digits; } and a rough pseudocode of the recursive workhorse data select_ordered_max(data original){ // check trivial return conditions // determine char most_significant // push all subproblems that satisfy most_significant //(special case where m==1) // pop subproblems, remembering best return answer {original.m, original.m, std::string(1, most_significant) + best_submatch.digits }; } String comparison works on numbers when you only compare strings of the exact same length, which is the case here. Yes, I know having n and m is redundant with digits.size(), but I didn't want to work too hard. Including it twice simplified some recursion checks. The actual implementation only pushed a candidate to the stack if it passed the max digit check for that level of recursion. This allowed me to get the correct 9 digit answer from 15 digits of input with only 28 candidates pushed to the stack (and them popped during max-select).
Now your code has quite a few issues, but rather than focusing on those lets answer the question. Let's say that your code has been corrected to give us: const size_t M where M is the number of digits expected in our output const vector<int> v which is the input set of numbers of size N You just always want to pick the highest value most significant number remaining. So we'll keep an end iterator to prevent us from picking a digit that wouldn't leave us with enough digits to finish the number, and use max_element to select: const int pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; auto maximum = 0; auto end = prev(cend(v), M - 1); auto it = max_element(cbegin(v), end); for (auto i = M - 1; i > 0; --i) { maximum += *it * pow10[i]; advance(end, 1); it = max_element(next(it), end); } maximum += *it; Live Example This code depends upon M being greater than 0 and less than N and less than log10(numeric_limits<int>::max()) EDIT: Sad to say this solves the consecutive digits problem, after edits the question wants subsequent digits, but not necessarily consecutive So the little known numeric library provides inner_product which seems like just the tool for this job. Now your code has quite a few issues, but rather than focusing on those lets answer the question. Let's say that your code has been corrected to give us: vector<int> foo(M) where M is the number of digits expected in our output const vector<int> v which is the input set of numbers of size N We'll use foo in the inner_product, initializing it with decreasing powers of 10: generate(begin(foo), end(foo), [i=int{1}]() mutable { auto result = i; i *= 10; return result; }); We can then use this in a loop: auto maximum = 0; for (auto it = prev(rend(v), size(foo) + 1); it != rbegin(v); advance(it, -1)) { maximum = max<int>(inner_product(cbegin(foo), cend(foo), it, 0), maximum); } maximum = max<int>(inner_product(cbegin(foo), cend(foo), rbegin(v), 0), maximum); Live Example To use it's initialization requires that your initial M was smaller than N, so you may want to assert that or something.
--EDITED-- here's my suggestion with STACK based on my previous suggestion using vector findMaxValueOutOfNDigits(stackInput, M, N) { // stackInput = [2, 9, 3, 6, 5, 8, 8, 8, 8, 7, 2, 2, 8, 1, 4] // *where 4 was the first element to be inserted and 2 was the last to be inserted // if the sequence is inverted, you can quickly fix it by doing a "for x = 0; x < stack.length; x++ { newStack.push(stack.pop()) }" currentMaxValue = 0 for i = 0; i < (M - N + 1); i++ { tempValue = process(stackInput, M, N) stackInput.pop() if (tempValue > currentMaxValue) currentMaxValue = tempValue } return currentMaxValue } process(stackInput, M, N) { tempValue = stackInput.pop() * 10^(N - 1) *howManyItemsCanILook = (M - N + 1) for y = (N - 2); y == 0; y++ { currentHowManyItemsCanILook = *howManyItemsCanILook tempValue = tempValue + getValue(stackInput, *howManyItemsCanILook) * 10^(y) *howManyItemsCanILook = *howManyItemsCanILook - 1 for x = 0; x < (currentHowManyItemsCanILook - *howManyItemsCanILook); x++ { stackInput.pop() } } return tempValue } getValue(stackInput, *howManyItemsCanILook) { currentMaxValue = stackInput.pop() if (currentMaxValue == 9) return 9 else { goUntil = *howManyItemsCanILook for i = 0; i < goUntil; i++ { *howManyItemsCanILook = *howManyItemsCanILook - 1 tempValue = stackInput.pop() if (currentMaxValue < tempValue) { currentMaxValue = tempValue if (currentMaxValue == 9) return currentMaxValue } } return currentMaxValue } } note: where *howManyItemsCanILook is passed by reference I hope this helps
Power set of large set
I have to calculate power set of set which may have more elements upto 10^5. I tried an algo and the code below but it failed (I think cause large value of pow(2, size)). void printPowerSet(int *set, int set_size) { unsigned int pow_set_size = pow(2, set_size); int counter, j,sum=0; for(counter = 0; counter < pow_set_size; counter++) { for(j = 0; j < set_size; j++) { if(counter & (1<<j)) std::cout<<set[i]<<" "; } std::cout<<sum; sum=0; printf("\n"); } } Is there any other algorithm or how can I fix this one (if it is possible)?? OR Can you suggest me how to do it i.e. finding subset of large set. As pointed out in an answer it seems I'm stuck in X-Y problem. Basically, I need sum of all subsets of any set. Now if you can suggest me any other approach to solve the problem. Thank you.
Here is an algorithm which will print out the power set of any set that will fit in your computer's memory. Given enough time, it will print the power set of a set of length 10^5. However, "enough time" will be something like several trillion billion gazillion years. c++14 #include <iostream> #include <vector> #include <algorithm> using namespace std; #include <iostream> void printPowerset (const vector<int>& original_set) { auto print_set = [&original_set](auto first, auto last) -> ostream& { cout << '('; auto sep = ""; for ( ; first != last ; ++first, sep = ",") { cout << sep << original_set[(*first) - 1]; } return cout << ')'; }; const int n = original_set.size(); std::vector<int> index_stack(n + 1, 0); int k = 0; while(1){ if (index_stack[k]<n){ index_stack[k+1] = index_stack[k] + 1; k++; } else{ index_stack[k-1]++; k--; } if (k==0) break; print_set(begin(index_stack) + 1, begin(index_stack) + 1 + k); } print_set(begin(index_stack), begin(index_stack)) << endl; } int main(){ auto nums = vector<int> { 2, 4, 6, 8 }; printPowerset(nums); nums = vector<int> { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; printPowerset(nums); return 0; } expected results: first power set (4 items): (2)(2,4)(2,4,6)(2,4,6,8)(2,4,8)(2,6)(2,6,8)(2,8)(4)(4,6)(4,6,8)(4,8)(6)(6,8)(8)() second power set (10 items) (2)(2,4)(2,4,6)(2,4,6,8)(2,4,6,8,10)(2,4,6,8,10,12)(2,4,6,8,10,12,14)(2,4,6,8,10,12,14,16)(2,4,6,8,10,12,14,16,18)(2,4,6,8,10,12,14,16,18,20)(2,4,6,8,10,12,14,16,20)(2,4,6,8,10,12,14,18)(2,4,6,8,10,12,14,18,20)(2,4,6,8,10,12,14,20)(2,4,6,8,10,12,16)(2,4,6,8,10,12,16,18)(2,4,6,8,10,12,16,18,20)(2,4,6,8,10,12,16,20)(2,4,6,8,10,12,18)(2,4,6,8,10,12,18,20)(2,4,6,8,10,12,20)(2,4,6,8,10,14)(2,4,6,8,10,14,16)(2,4,6,8,10,14,16,18)(2,4,6,8,10,14,16,18,20)(2,4,6,8,10,14,16,20)(2,4,6,8,10,14,18)(2,4,6,8,10,14,18,20)(2,4,6,8,10,14,20)(2,4,6,8,10,16)(2,4,6,8,10,16,18)(2,4,6,8,10,16,18,20)(2,4,6,8,10,16,20)(2,4,6,8,10,18)(2,4,6,8,10,18,20)(2,4,6,8,10,20)(2,4,6,8,12)(2,4,6,8,12,14)(2,4,6,8,12,14,16)(2,4,6,8,12,14,16,18)(2,4,6,8,12,14,16,18,20)(2,4,6,8,12,14,16,20)(2,4,6,8,12,14,18)(2,4,6,8,12,14,18,20)(2,4,6,8,12,14,20)(2,4,6,8,12,16)(2,4,6,8,12,16,18)(2,4,6,8,12,16,18,20)(2,4,6,8,12,16,20)(2,4,6,8,12,18)(2,4,6,8,12,18,20)(2,4,6,8,12,20)(2,4,6,8,14)(2,4,6,8,14,16)(2,4,6,8,14,16,18)(2,4,6,8,14,16,18,20)(2,4,6,8,14,16,20)(2,4,6,8,14,18)(2,4,6,8,14,18,20)(2,4,6,8,14,20)(2,4,6,8,16)(2,4,6,8,16,18)(2,4,6,8,16,18,20)(2,4,6,8,16,20)(2,4,6,8,18)(2,4,6,8,18,20)(2,4,6,8,20)(2,4,6,10)(2,4,6,10,12)(2,4,6,10,12,14)(2,4,6,10,12,14,16)(2,4,6,10,12,14,16,18)(2,4,6,10,12,14,16,18,20)(2,4,6,10,12,14,16,20)(2,4,6,10,12,14,18)(2,4,6,10,12,14,18,20)(2,4,6,10,12,14,20)(2,4,6,10,12,16)(2,4,6,10,12,16,18)(2,4,6,10,12,16,18,20)(2,4,6,10,12,16,20)(2,4,6,10,12,18)(2,4,6,10,12,18,20)(2,4,6,10,12,20)(2,4,6,10,14)(2,4,6,10,14,16)(2,4,6,10,14,16,18)(2,4,6,10,14,16,18,20)(2,4,6,10,14,16,20)(2,4,6,10,14,18)(2,4,6,10,14,18,20)(2,4,6,10,14,20)(2,4,6,10,16)(2,4,6,10,16,18)(2,4,6,10,16,18,20)(2,4,6,10,16,20)(2,4,6,10,18)(2,4,6,10,18,20)(2,4,6,10,20)(2,4,6,12)(2,4,6,12,14)(2,4,6,12,14,16)(2,4,6,12,14,16,18)(2,4,6,12,14,16,18,20)(2,4,6,12,14,16,20)(2,4,6,12,14,18)(2,4,6,12,14,18,20)(2,4,6,12,14,20)(2,4,6,12,16)(2,4,6,12,16,18)(2,4,6,12,16,18,20)(2,4,6,12,16,20)(2,4,6,12,18)(2,4,6,12,18,20)(2,4,6,12,20)(2,4,6,14)(2,4,6,14,16)(2,4,6,14,16,18)(2,4,6,14,16,18,20)(2,4,6,14,16,20)(2,4,6,14,18)(2,4,6,14,18,20)(2,4,6,14,20)(2,4,6,16)(2,4,6,16,18)(2,4,6,16,18,20)(2,4,6,16,20)(2,4,6,18)(2,4,6,18,20)(2,4,6,20)(2,4,8)(2,4,8,10)(2,4,8,10,12)(2,4,8,10,12,14)(2,4,8,10,12,14,16)(2,4,8,10,12,14,16,18)(2,4,8,10,12,14,16,18,20)(2,4,8,10,12,14,16,20)(2,4,8,10,12,14,18)(2,4,8,10,12,14,18,20)(2,4,8,10,12,14,20)(2,4,8,10,12,16)(2,4,8,10,12,16,18)(2,4,8,10,12,16,18,20)(2,4,8,10,12,16,20)(2,4,8,10,12,18)(2,4,8,10,12,18,20)(2,4,8,10,12,20)(2,4,8,10,14)(2,4,8,10,14,16)(2,4,8,10,14,16,18)(2,4,8,10,14,16,18,20)(2,4,8,10,14,16,20)(2,4,8,10,14,18)(2,4,8,10,14,18,20)(2,4,8,10,14,20)(2,4,8,10,16)(2,4,8,10,16,18)(2,4,8,10,16,18,20)(2,4,8,10,16,20)(2,4,8,10,18)(2,4,8,10,18,20)(2,4,8,10,20)(2,4,8,12)(2,4,8,12,14)(2,4,8,12,14,16)(2,4,8,12,14,16,18)(2,4,8,12,14,16,18,20)(2,4,8,12,14,16,20)(2,4,8,12,14,18)(2,4,8,12,14,18,20)(2,4,8,12,14,20)(2,4,8,12,16)(2,4,8,12,16,18)(2,4,8,12,16,18,20)(2,4,8,12,16,20)(2,4,8,12,18)(2,4,8,12,18,20)(2,4,8,12,20)(2,4,8,14)(2,4,8,14,16)(2,4,8,14,16,18)(2,4,8,14,16,18,20)(2,4,8,14,16,20)(2,4,8,14,18)(2,4,8,14,18,20)(2,4,8,14,20)(2,4,8,16)(2,4,8,16,18)(2,4,8,16,18,20)(2,4,8,16,20)(2,4,8,18)(2,4,8,18,20)(2,4,8,20)(2,4,10)(2,4,10,12)(2,4,10,12,14)(2,4,10,12,14,16)(2,4,10,12,14,16,18)(2,4,10,12,14,16,18,20)(2,4,10,12,14,16,20)(2,4,10,12,14,18)(2,4,10,12,14,18,20)(2,4,10,12,14,20)(2,4,10,12,16)(2,4,10,12,16,18)(2,4,10,12,16,18,20)(2,4,10,12,16,20)(2,4,10,12,18)(2,4,10,12,18,20)(2,4,10,12,20)(2,4,10,14)(2,4,10,14,16)(2,4,10,14,16,18)(2,4,10,14,16,18,20)(2,4,10,14,16,20)(2,4,10,14,18)(2,4,10,14,18,20)(2,4,10,14,20)(2,4,10,16)(2,4,10,16,18)(2,4,10,16,18,20)(2,4,10,16,20)(2,4,10,18)(2,4,10,18,20)(2,4,10,20)(2,4,12)(2,4,12,14)(2,4,12,14,16)(2,4,12,14,16,18)(2,4,12,14,16,18,20)(2,4,12,14,16,20)(2,4,12,14,18)(2,4,12,14,18,20)(2,4,12,14,20)(2,4,12,16)(2,4,12,16,18)(2,4,12,16,18,20)(2,4,12,16,20)(2,4,12,18)(2,4,12,18,20)(2,4,12,20)(2,4,14)(2,4,14,16)(2,4,14,16,18)(2,4,14,16,18,20)(2,4,14,16,20)(2,4,14,18)(2,4,14,18,20)(2,4,14,20)(2,4,16)(2,4,16,18)(2,4,16,18,20)(2,4,16,20)(2,4,18)(2,4,18,20)(2,4,20)(2,6)(2,6,8)(2,6,8,10)(2,6,8,10,12)(2,6,8,10,12,14)(2,6,8,10,12,14,16)(2,6,8,10,12,14,16,18)(2,6,8,10,12,14,16,18,20)(2,6,8,10,12,14,16,20)(2,6,8,10,12,14,18)(2,6,8,10,12,14,18,20)(2,6,8,10,12,14,20)(2,6,8,10,12,16)(2,6,8,10,12,16,18)(2,6,8,10,12,16,18,20)(2,6,8,10,12,16,20)(2,6,8,10,12,18)(2,6,8,10,12,18,20)(2,6,8,10,12,20)(2,6,8,10,14)(2,6,8,10,14,16)(2,6,8,10,14,16,18)(2,6,8,10,14,16,18,20)(2,6,8,10,14,16,20)(2,6,8,10,14,18)(2,6,8,10,14,18,20)(2,6,8,10,14,20)(2,6,8,10,16)(2,6,8,10,16,18)(2,6,8,10,16,18,20)(2,6,8,10,16,20)(2,6,8,10,18)(2,6,8,10,18,20)(2,6,8,10,20)(2,6,8,12)(2,6,8,12,14)(2,6,8,12,14,16)(2,6,8,12,14,16,18)(2,6,8,12,14,16,18,20)(2,6,8,12,14,16,20)(2,6,8,12,14,18)(2,6,8,12,14,18,20)(2,6,8,12,14,20)(2,6,8,12,16)(2,6,8,12,16,18)(2,6,8,12,16,18,20)(2,6,8,12,16,20)(2,6,8,12,18)(2,6,8,12,18,20)(2,6,8,12,20)(2,6,8,14)(2,6,8,14,16)(2,6,8,14,16,18)(2,6,8,14,16,18,20)(2,6,8,14,16,20)(2,6,8,14,18)(2,6,8,14,18,20)(2,6,8,14,20)(2,6,8,16)(2,6,8,16,18)(2,6,8,16,18,20)(2,6,8,16,20)(2,6,8,18)(2,6,8,18,20)(2,6,8,20)(2,6,10)(2,6,10,12)(2,6,10,12,14)(2,6,10,12,14,16)(2,6,10,12,14,16,18)(2,6,10,12,14,16,18,20)(2,6,10,12,14,16,20)(2,6,10,12,14,18)(2,6,10,12,14,18,20)(2,6,10,12,14,20)(2,6,10,12,16)(2,6,10,12,16,18)(2,6,10,12,16,18,20)(2,6,10,12,16,20)(2,6,10,12,18)(2,6,10,12,18,20)(2,6,10,12,20)(2,6,10,14)(2,6,10,14,16)(2,6,10,14,16,18)(2,6,10,14,16,18,20)(2,6,10,14,16,20)(2,6,10,14,18)(2,6,10,14,18,20)(2,6,10,14,20)(2,6,10,16)(2,6,10,16,18)(2,6,10,16,18,20)(2,6,10,16,20)(2,6,10,18)(2,6,10,18,20)(2,6,10,20)(2,6,12)(2,6,12,14)(2,6,12,14,16)(2,6,12,14,16,18)(2,6,12,14,16,18,20)(2,6,12,14,16,20)(2,6,12,14,18)(2,6,12,14,18,20)(2,6,12,14,20)(2,6,12,16)(2,6,12,16,18)(2,6,12,16,18,20)(2,6,12,16,20)(2,6,12,18)(2,6,12,18,20)(2,6,12,20)(2,6,14)(2,6,14,16)(2,6,14,16,18)(2,6,14,16,18,20)(2,6,14,16,20)(2,6,14,18)(2,6,14,18,20)(2,6,14,20)(2,6,16)(2,6,16,18)(2,6,16,18,20)(2,6,16,20)(2,6,18)(2,6,18,20)(2,6,20)(2,8)(2,8,10)(2,8,10,12)(2,8,10,12,14)(2,8,10,12,14,16)(2,8,10,12,14,16,18)(2,8,10,12,14,16,18,20)(2,8,10,12,14,16,20)(2,8,10,12,14,18)(2,8,10,12,14,18,20)(2,8,10,12,14,20)(2,8,10,12,16)(2,8,10,12,16,18)(2,8,10,12,16,18,20)(2,8,10,12,16,20)(2,8,10,12,18)(2,8,10,12,18,20)(2,8,10,12,20)(2,8,10,14)(2,8,10,14,16)(2,8,10,14,16,18)(2,8,10,14,16,18,20)(2,8,10,14,16,20)(2,8,10,14,18)(2,8,10,14,18,20)(2,8,10,14,20)(2,8,10,16)(2,8,10,16,18)(2,8,10,16,18,20)(2,8,10,16,20)(2,8,10,18)(2,8,10,18,20)(2,8,10,20)(2,8,12)(2,8,12,14)(2,8,12,14,16)(2,8,12,14,16,18)(2,8,12,14,16,18,20)(2,8,12,14,16,20)(2,8,12,14,18)(2,8,12,14,18,20)(2,8,12,14,20)(2,8,12,16)(2,8,12,16,18)(2,8,12,16,18,20)(2,8,12,16,20)(2,8,12,18)(2,8,12,18,20)(2,8,12,20)(2,8,14)(2,8,14,16)(2,8,14,16,18)(2,8,14,16,18,20)(2,8,14,16,20)(2,8,14,18)(2,8,14,18,20)(2,8,14,20)(2,8,16)(2,8,16,18)(2,8,16,18,20)(2,8,16,20)(2,8,18)(2,8,18,20)(2,8,20)(2,10)(2,10,12)(2,10,12,14)(2,10,12,14,16)(2,10,12,14,16,18)(2,10,12,14,16,18,20)(2,10,12,14,16,20)(2,10,12,14,18)(2,10,12,14,18,20)(2,10,12,14,20)(2,10,12,16)(2,10,12,16,18)(2,10,12,16,18,20)(2,10,12,16,20)(2,10,12,18)(2,10,12,18,20)(2,10,12,20)(2,10,14)(2,10,14,16)(2,10,14,16,18)(2,10,14,16,18,20)(2,10,14,16,20)(2,10,14,18)(2,10,14,18,20)(2,10,14,20)(2,10,16)(2,10,16,18)(2,10,16,18,20)(2,10,16,20)(2,10,18)(2,10,18,20)(2,10,20)(2,12)(2,12,14)(2,12,14,16)(2,12,14,16,18)(2,12,14,16,18,20)(2,12,14,16,20)(2,12,14,18)(2,12,14,18,20)(2,12,14,20)(2,12,16)(2,12,16,18)(2,12,16,18,20)(2,12,16,20)(2,12,18)(2,12,18,20)(2,12,20)(2,14)(2,14,16)(2,14,16,18)(2,14,16,18,20)(2,14,16,20)(2,14,18)(2,14,18,20)(2,14,20)(2,16)(2,16,18)(2,16,18,20)(2,16,20)(2,18)(2,18,20)(2,20)(4)(4,6)(4,6,8)(4,6,8,10)(4,6,8,10,12)(4,6,8,10,12,14)(4,6,8,10,12,14,16)(4,6,8,10,12,14,16,18)(4,6,8,10,12,14,16,18,20)(4,6,8,10,12,14,16,20)(4,6,8,10,12,14,18)(4,6,8,10,12,14,18,20)(4,6,8,10,12,14,20)(4,6,8,10,12,16)(4,6,8,10,12,16,18)(4,6,8,10,12,16,18,20)(4,6,8,10,12,16,20)(4,6,8,10,12,18)(4,6,8,10,12,18,20)(4,6,8,10,12,20)(4,6,8,10,14)(4,6,8,10,14,16)(4,6,8,10,14,16,18)(4,6,8,10,14,16,18,20)(4,6,8,10,14,16,20)(4,6,8,10,14,18)(4,6,8,10,14,18,20)(4,6,8,10,14,20)(4,6,8,10,16)(4,6,8,10,16,18)(4,6,8,10,16,18,20)(4,6,8,10,16,20)(4,6,8,10,18)(4,6,8,10,18,20)(4,6,8,10,20)(4,6,8,12)(4,6,8,12,14)(4,6,8,12,14,16)(4,6,8,12,14,16,18)(4,6,8,12,14,16,18,20)(4,6,8,12,14,16,20)(4,6,8,12,14,18)(4,6,8,12,14,18,20)(4,6,8,12,14,20)(4,6,8,12,16)(4,6,8,12,16,18)(4,6,8,12,16,18,20)(4,6,8,12,16,20)(4,6,8,12,18)(4,6,8,12,18,20)(4,6,8,12,20)(4,6,8,14)(4,6,8,14,16)(4,6,8,14,16,18)(4,6,8,14,16,18,20)(4,6,8,14,16,20)(4,6,8,14,18)(4,6,8,14,18,20)(4,6,8,14,20)(4,6,8,16)(4,6,8,16,18)(4,6,8,16,18,20)(4,6,8,16,20)(4,6,8,18)(4,6,8,18,20)(4,6,8,20)(4,6,10)(4,6,10,12)(4,6,10,12,14)(4,6,10,12,14,16)(4,6,10,12,14,16,18)(4,6,10,12,14,16,18,20)(4,6,10,12,14,16,20)(4,6,10,12,14,18)(4,6,10,12,14,18,20)(4,6,10,12,14,20)(4,6,10,12,16)(4,6,10,12,16,18)(4,6,10,12,16,18,20)(4,6,10,12,16,20)(4,6,10,12,18)(4,6,10,12,18,20)(4,6,10,12,20)(4,6,10,14)(4,6,10,14,16)(4,6,10,14,16,18)(4,6,10,14,16,18,20)(4,6,10,14,16,20)(4,6,10,14,18)(4,6,10,14,18,20)(4,6,10,14,20)(4,6,10,16)(4,6,10,16,18)(4,6,10,16,18,20)(4,6,10,16,20)(4,6,10,18)(4,6,10,18,20)(4,6,10,20)(4,6,12)(4,6,12,14)(4,6,12,14,16)(4,6,12,14,16,18)(4,6,12,14,16,18,20)(4,6,12,14,16,20)(4,6,12,14,18)(4,6,12,14,18,20)(4,6,12,14,20)(4,6,12,16)(4,6,12,16,18)(4,6,12,16,18,20)(4,6,12,16,20)(4,6,12,18)(4,6,12,18,20)(4,6,12,20)(4,6,14)(4,6,14,16)(4,6,14,16,18)(4,6,14,16,18,20)(4,6,14,16,20)(4,6,14,18)(4,6,14,18,20)(4,6,14,20)(4,6,16)(4,6,16,18)(4,6,16,18,20)(4,6,16,20)(4,6,18)(4,6,18,20)(4,6,20)(4,8)(4,8,10)(4,8,10,12)(4,8,10,12,14)(4,8,10,12,14,16)(4,8,10,12,14,16,18)(4,8,10,12,14,16,18,20)(4,8,10,12,14,16,20)(4,8,10,12,14,18)(4,8,10,12,14,18,20)(4,8,10,12,14,20)(4,8,10,12,16)(4,8,10,12,16,18)(4,8,10,12,16,18,20)(4,8,10,12,16,20)(4,8,10,12,18)(4,8,10,12,18,20)(4,8,10,12,20)(4,8,10,14)(4,8,10,14,16)(4,8,10,14,16,18)(4,8,10,14,16,18,20)(4,8,10,14,16,20)(4,8,10,14,18)(4,8,10,14,18,20)(4,8,10,14,20)(4,8,10,16)(4,8,10,16,18)(4,8,10,16,18,20)(4,8,10,16,20)(4,8,10,18)(4,8,10,18,20)(4,8,10,20)(4,8,12)(4,8,12,14)(4,8,12,14,16)(4,8,12,14,16,18)(4,8,12,14,16,18,20)(4,8,12,14,16,20)(4,8,12,14,18)(4,8,12,14,18,20)(4,8,12,14,20)(4,8,12,16)(4,8,12,16,18)(4,8,12,16,18,20)(4,8,12,16,20)(4,8,12,18)(4,8,12,18,20)(4,8,12,20)(4,8,14)(4,8,14,16)(4,8,14,16,18)(4,8,14,16,18,20)(4,8,14,16,20)(4,8,14,18)(4,8,14,18,20)(4,8,14,20)(4,8,16)(4,8,16,18)(4,8,16,18,20)(4,8,16,20)(4,8,18)(4,8,18,20)(4,8,20)(4,10)(4,10,12)(4,10,12,14)(4,10,12,14,16)(4,10,12,14,16,18)(4,10,12,14,16,18,20)(4,10,12,14,16,20)(4,10,12,14,18)(4,10,12,14,18,20)(4,10,12,14,20)(4,10,12,16)(4,10,12,16,18)(4,10,12,16,18,20)(4,10,12,16,20)(4,10,12,18)(4,10,12,18,20)(4,10,12,20)(4,10,14)(4,10,14,16)(4,10,14,16,18)(4,10,14,16,18,20)(4,10,14,16,20)(4,10,14,18)(4,10,14,18,20)(4,10,14,20)(4,10,16)(4,10,16,18)(4,10,16,18,20)(4,10,16,20)(4,10,18)(4,10,18,20)(4,10,20)(4,12)(4,12,14)(4,12,14,16)(4,12,14,16,18)(4,12,14,16,18,20)(4,12,14,16,20)(4,12,14,18)(4,12,14,18,20)(4,12,14,20)(4,12,16)(4,12,16,18)(4,12,16,18,20)(4,12,16,20)(4,12,18)(4,12,18,20)(4,12,20)(4,14)(4,14,16)(4,14,16,18)(4,14,16,18,20)(4,14,16,20)(4,14,18)(4,14,18,20)(4,14,20)(4,16)(4,16,18)(4,16,18,20)(4,16,20)(4,18)(4,18,20)(4,20)(6)(6,8)(6,8,10)(6,8,10,12)(6,8,10,12,14)(6,8,10,12,14,16)(6,8,10,12,14,16,18)(6,8,10,12,14,16,18,20)(6,8,10,12,14,16,20)(6,8,10,12,14,18)(6,8,10,12,14,18,20)(6,8,10,12,14,20)(6,8,10,12,16)(6,8,10,12,16,18)(6,8,10,12,16,18,20)(6,8,10,12,16,20)(6,8,10,12,18)(6,8,10,12,18,20)(6,8,10,12,20)(6,8,10,14)(6,8,10,14,16)(6,8,10,14,16,18)(6,8,10,14,16,18,20)(6,8,10,14,16,20)(6,8,10,14,18)(6,8,10,14,18,20)(6,8,10,14,20)(6,8,10,16)(6,8,10,16,18)(6,8,10,16,18,20)(6,8,10,16,20)(6,8,10,18)(6,8,10,18,20)(6,8,10,20)(6,8,12)(6,8,12,14)(6,8,12,14,16)(6,8,12,14,16,18)(6,8,12,14,16,18,20)(6,8,12,14,16,20)(6,8,12,14,18)(6,8,12,14,18,20)(6,8,12,14,20)(6,8,12,16)(6,8,12,16,18)(6,8,12,16,18,20)(6,8,12,16,20)(6,8,12,18)(6,8,12,18,20)(6,8,12,20)(6,8,14)(6,8,14,16)(6,8,14,16,18)(6,8,14,16,18,20)(6,8,14,16,20)(6,8,14,18)(6,8,14,18,20)(6,8,14,20)(6,8,16)(6,8,16,18)(6,8,16,18,20)(6,8,16,20)(6,8,18)(6,8,18,20)(6,8,20)(6,10)(6,10,12)(6,10,12,14)(6,10,12,14,16)(6,10,12,14,16,18)(6,10,12,14,16,18,20)(6,10,12,14,16,20)(6,10,12,14,18)(6,10,12,14,18,20)(6,10,12,14,20)(6,10,12,16)(6,10,12,16,18)(6,10,12,16,18,20)(6,10,12,16,20)(6,10,12,18)(6,10,12,18,20)(6,10,12,20)(6,10,14)(6,10,14,16)(6,10,14,16,18)(6,10,14,16,18,20)(6,10,14,16,20)(6,10,14,18)(6,10,14,18,20)(6,10,14,20)(6,10,16)(6,10,16,18)(6,10,16,18,20)(6,10,16,20)(6,10,18)(6,10,18,20)(6,10,20)(6,12)(6,12,14)(6,12,14,16)(6,12,14,16,18)(6,12,14,16,18,20)(6,12,14,16,20)(6,12,14,18)(6,12,14,18,20)(6,12,14,20)(6,12,16)(6,12,16,18)(6,12,16,18,20)(6,12,16,20)(6,12,18)(6,12,18,20)(6,12,20)(6,14)(6,14,16)(6,14,16,18)(6,14,16,18,20)(6,14,16,20)(6,14,18)(6,14,18,20)(6,14,20)(6,16)(6,16,18)(6,16,18,20)(6,16,20)(6,18)(6,18,20)(6,20)(8)(8,10)(8,10,12)(8,10,12,14)(8,10,12,14,16)(8,10,12,14,16,18)(8,10,12,14,16,18,20)(8,10,12,14,16,20)(8,10,12,14,18)(8,10,12,14,18,20)(8,10,12,14,20)(8,10,12,16)(8,10,12,16,18)(8,10,12,16,18,20)(8,10,12,16,20)(8,10,12,18)(8,10,12,18,20)(8,10,12,20)(8,10,14)(8,10,14,16)(8,10,14,16,18)(8,10,14,16,18,20)(8,10,14,16,20)(8,10,14,18)(8,10,14,18,20)(8,10,14,20)(8,10,16)(8,10,16,18)(8,10,16,18,20)(8,10,16,20)(8,10,18)(8,10,18,20)(8,10,20)(8,12)(8,12,14)(8,12,14,16)(8,12,14,16,18)(8,12,14,16,18,20)(8,12,14,16,20)(8,12,14,18)(8,12,14,18,20)(8,12,14,20)(8,12,16)(8,12,16,18)(8,12,16,18,20)(8,12,16,20)(8,12,18)(8,12,18,20)(8,12,20)(8,14)(8,14,16)(8,14,16,18)(8,14,16,18,20)(8,14,16,20)(8,14,18)(8,14,18,20)(8,14,20)(8,16)(8,16,18)(8,16,18,20)(8,16,20)(8,18)(8,18,20)(8,20)(10)(10,12)(10,12,14)(10,12,14,16)(10,12,14,16,18)(10,12,14,16,18,20)(10,12,14,16,20)(10,12,14,18)(10,12,14,18,20)(10,12,14,20)(10,12,16)(10,12,16,18)(10,12,16,18,20)(10,12,16,20)(10,12,18)(10,12,18,20)(10,12,20)(10,14)(10,14,16)(10,14,16,18)(10,14,16,18,20)(10,14,16,20)(10,14,18)(10,14,18,20)(10,14,20)(10,16)(10,16,18)(10,16,18,20)(10,16,20)(10,18)(10,18,20)(10,20)(12)(12,14)(12,14,16)(12,14,16,18)(12,14,16,18,20)(12,14,16,20)(12,14,18)(12,14,18,20)(12,14,20)(12,16)(12,16,18)(12,16,18,20)(12,16,20)(12,18)(12,18,20)(12,20)(14)(14,16)(14,16,18)(14,16,18,20)(14,16,20)(14,18)(14,18,20)(14,20)(16)(16,18)(16,18,20)(16,20)(18)(18,20)(20)()
Since number of elements in the set can go upto 10^5 and hence the size of the set will go upto 2^(10^5) which is huge. You can just either print it or if you want to store it , store it in a file.
It is not possible, as you pointed out yourself power set contains pow(2, size) no of elements. You need to print the entire set that can only be done by generating the set using backtracking, there is nothing better. To find sum of all subsets of a given set is however a simpler problem. If the no of elements is n, then each element in the array occurs 2^(n-1) times in the power set. Pseudo code : int sum = 0; for(int i = 0;i < n;i++){ sum += ( set[i] * ( 1 << (n-1) ) ); //pow(2, n-1) == 1 << (n-1) } cout << sum << endl; You would need a Big Integer Library for larger values of n.