Retrieval of items from a knapsack using dynamic programming - c++
I'm new to dynamic programming and have tried my first DP problem. The problem statement is
Given a knapsack of size C, and n items of sizes s[] with values v[], maximize the capacity of the items which can be put in the knapsack. An item may be repeated any number of times. (Duplicate items are allowed).
Although I was able to formulate the recurrence relation and create the DP table, and eventually get the maximum value that can be put in the knapsack, I am not able to device a method to retrieve which values have to be selected to get the required sum.
Here is my solution:
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int s[] = { 1, 3, 4, 5, 2, 7, 8 , 10};
int v[] = { 34, 45, 23, 78, 33, 5, 7 , 1};
int n = ( (sizeof(s)) / (sizeof(s[0])) );
vector<int> backtrack;
int C = 15;
int pos;
int m[20];
m[0] = 0;
int mx = 0;
for ( int j = 1; j <= C; j++) {
mx = 0;
m[j] = m[j-1];
pos = j-1;
for ( int i = 0; i < n; i++) {
mx = m[i-s[i]] + v[i];
if ( mx > m[i] ) {
m[i] = mx;
pos = i - s[j];
}
}
backtrack.push_back(pos);
}
cout << m[C] << endl<<endl;
for ( int i = 0; i < backtrack.size(); i++) {
cout << s[backtrack[i]] <<endl;
}
return 0;
}
In my solution, I've attempted to store the positions of the maximum value item selcted in a vector, and eventually print them. However this does not seem to give me the correct solution.
Running the program produces:
79
2
3
0
5
2
7
8
10
34
45
23
78
33
5
7
It is obvious from the output that the numbers in the output cant be the sizes of the items selected as there there no item of size 0 as shown in the output.
I hope that you will help me find the error in my logic or implementation. Thanks.
You are following the greedy approach. It's pretty clever , but it is heuristic. I will not give you the correct code, as it might be a homework, but the recursive function knapsack would look like this:
knapsack(C): maximum profit achivable using a knapsack of Capacity C
knapsack(C) = max { knapsack(C-w[i]) + v[i] } for all w[i] <= C
knapsack(0) = 0
In code:
dp(0) = 0;
for i = 1 to C
dp(i) = -INF;
for k = i-1 downto 0
if w[k] < i then
dp(i) = max{dp(i-w[k]) + v[k], dp(i)};
print dp(Capacity);
Related
Minimum difference between the highest and the smallest value of mines distributed
Given Question: Given n companies and m oil mines with values, design an algorithm to distribute the sites among the companies in a fair manner, where the company getting the highest total value of its assigned sites and the one getting the lowest total value is minimal. Your algorithm should output this minimum difference. Note that oil mines sites assigned to each company should be adjacent to each other, and that the number of mines m is always bigger than or equal to the number of companies n Sample Input: Input : n = 3, site values = [6, 10, 13, 2] Output : 9 → for the assignment of [6] to company #1, [10] to company #2, and [13, 2] to company #3, making the minimum difference (13+2) - 6 = 9 My attempt at solving: Add all previous elements in array, so the new array becomes [6, 16, 29, 31]. Then, I form all possible solution arrays which are: note: 31 stays constant because it is the largest and I need to subtract the largest from the smallest 31, 29, 16 31, 29, 6 31, 16, 6 Then, I subtract all previous elements in array, so the new arrays become 2, 13, 16 2, 23, 6 15, 10, 6 Then I subtract the highest number from the lowest number in each array which would be: 16 - 2 = 14 23 - 2 = 21 15 - 6 = 9 // **answer** I would pick the smallest difference which is 9 My question: (1) Is there an easier way to solve this? As this seems like it a bit too complex and I'm just overthinking things. (2) How would I go about implementing generating all the possible combinations to the array? Should I use permutations? Recursion? This is the part that I'm stuck on the most, generating all possible solutions where 31 (in the example stays the same) and I select only 2 out of the 3 other elements in the array. void Func (int n, int m, int prosValues[], int solArray[], bool summed) { if (!summed) { for (int i = 1; i < m; i++) { prosValues[i] = prosValues[i] + prosValues[i-1]; } solArray[0] = prosValues[m-1]; summed = true; } // generate all possible combinations as shown in example }
Interesting problem! I chose a different approach to solve this. I don't know, if it is more elegant, but I spend some time thinking about it and wanted to share my solution at least. I make the following convention for the site assignment: The indices in the site array describe the start index for site values, from which the site values are assigned to that site. Let's make it clearer using two examples. values = {2,6,3,8,2,1} sites1 = {0,1,2} sites2 = {0,2,5} The two site distributions site1 and site2 will assign the site values as follows: distribution for site1: 0: 2 1: 6 2: 3, 8, 2, 1 distribution for site2: 0: 2, 6 1: 3, 8 2: 2, 1 This way we can iterate through all combinations by shifting the indices in sites up. In the example for 6 site values, we would get: {0,1,2} {0,1,3} {0,1,4} {0,1,5} {0,2,3} {0,2,4} {0,2,5} {0,3,4} {0,3,5} {0,4,5} {1,2,3} {1,3,4} {1,3,5} . . . {3,4,5} For all these site distributions we sum up the site values for each site, calculate the maximum difference and pick the distribution with the smallest difference. Putting it all together, here is the code I came up with: #include <iostream> #include <algorithm> int calc_diff(int m, int n, int* values, int* sites) { // stores the sum of values for each site int *result = new int[n] {0}; // iterate through all site values for (int j = 0; j < m; j++) { // find the correct site index to which the value is assigned int index = 0; for (int i = n-1; i >= 0; i--) { if (j >= sites[i]) { index = i; break; } } // sum up the site vaues result[index] += values[j]; // debug print // std::cout << index << ":\t" << result[index] << std::endl; } // print the site results std::cout << "result:\t"; for (int i = 0; i < n; i++) std::cout << result[i] << "\t"; // get the highest difference auto min = std::min_element(result, result+n); auto max = std::max_element(result, result+n); int diff = *max - *min; delete[] result; return diff; } int main() { int n = 3; int m = 6; auto values = new int[m] {2,6,3,8,2,1}; auto sites = new int[n] {0,1,2}; // start index of the first site int start_index = 0; // current best difference (some really high number) int max = 100000000; // the current best solution auto best = new int[n]; bool end = false; while(!end) { std::cout << "sites:\t"; for (int i = 0; i < n; i++) std::cout << sites[i] << "\t"; std::cout << std::endl; // calculate the maximal difference of the current site distribution auto diff = calc_diff(m, n, values, sites); std::cout << "\nmax diff:\t" << diff << std::endl << std::endl; // if we find a better solution than the current best, we store it as new best solution if (diff < max) { max = diff; memcpy(best, sites, n*sizeof(int)); } // calculate new site distribution int index = 0; for (int i = n-1; i >= 0; i--) { // get the current index index = sites[i]; // can we still move the index one position up? // the index of the last site should not exceed m-1 // the index of all other sites should be less than the index of the next site if ((i == n-1 && index < m-1) || (i < n-1 && i > 0 && index < sites[i+1]-1)) { // increase the index of the current site sites[i]++; break; } // all site index have moved to maximum position? // (we iterated through all indices (so i=0) and moved none of them) if (i == 0) { // increase the start index of the first site start_index++; // reset the indices by starting from the current start_index for (int j = 1; j < n; j++) { sites[j] = start_index + j; // if we exceed the numbers of site values, we can stop the loop if (sites[j] >= m) end = true; } } } } // print best soluition std::cout << "Best distribution: "; for (int i = 0; i < n; i++) std::cout << best[i] << " "; delete[] sites; delete[] values; delete[] best; }
How to find all possible combinations of adding two variables, each attached to a multiplier, summing up to a given number (cin)?
In my situation, a lorry has a capacity of 30, while a van has a capacity of 10. I need to find the number of vans/lorries needed to transport a given amount of cargo, say 100. I need to find all possible combinations of lorries + vans that will add up to 100. The basic math calculation would be: (30*lorrycount) + (10*vancount) = n, where n is number of cargo. Output Example Cargo to be transported: 100 Number of Lorry: 0 3 2 1 Number of Van: 10 1 4 7 For example, the 2nd combination is 3 lorries, 1 van. Considering that lorries have capacity = 30 and van capacity = 10, (30*3)+(10*1) = 100 = n. For now, we only have this code, which finds literally all combinations of numbers that add up to given number n, without considering the formula given above. #include <iostream> #include <vector> using namespace std; void findCombinationsUtil(int arr[], int index, int num, int reducedNum) { int lorry_capacity = 30; int van_capacity = 10; // Base condition if (reducedNum < 0) return; // If combination is found, print it if (reducedNum == 0) { for (int i = 0; i < index; i++) cout << arr[i] << " "; cout << endl; return; } // Find the previous number stored in arr[] // It helps in maintaining increasing order int prev = (index == 0) ? 1 : arr[index - 1]; // note loop starts from previous number // i.e. at array location index - 1 for (int k = prev; k <= num; k++) { // next element of array is k arr[index] = k; // call recursively with reduced number findCombinationsUtil(arr, index + 1, num, reducedNum - k); } } void findCombinations(int n) { // array to store the combinations // It can contain max n elements std::vector<int> arr(n); // allocate n elements //find all combinations findCombinationsUtil(&*arr.begin(), 0, n, n); } int main() { int n; cout << "Enter the amount of cargo you want to transport: "; cin >> n; cout << endl; //const int n = 10; findCombinations(n); return 0; } Do let me know if you have any solution to this, thank you.
An iterative way of finding all possible combinations #include <iostream> #include <vector> int main() { int cw = 100; int lw = 30, vw = 10; int maxl = cw/lw; // maximum no. of lorries that can be there std::vector<std::pair<int,int>> solutions; // for the inclusive range of 0 to maxl, find the corresponding no. of vans for each variant of no of lorries for(int l = 0; l<= maxl; ++l){ bool is_integer = (cw - l*lw)%vw == 0; // only if this is true, then there is an integer which satisfies for given l if(is_integer){ int v = (cw-l*lw)/vw; // no of vans solutions.push_back(std::make_pair(l,v)); } } for( auto& solution : solutions){ std::cout<<solution.first<<" lorries and "<< solution.second<<" vans" <<std::endl; } return 0; }
We will create a recursive function that walks a global capacities array left to right and tries to load cargo into the various vehicle types. We keep track of how much we still have to load and pass that on to any recursive call. If we reach the end of the array, we produce a solution only if the remaining cargo is zero. std::vector<int> capacities = { 30, 10 }; using Solution = std::vector<int>; using Solutions = std::vector<Solution>; void tryLoad(int remaining_cargo, int vehicle_index, Solution so_far, std::back_insert_iterator<Solutions>& solutions) { if (vehicle_index == capacities.size()) { if (remaining_cargo == 0) // we have a solution *solutions++ = so_far; return; } int capacity = capacities[vehicle_index]; for (int vehicles = 0; vehicles <= remaining_cargo / capacity; vehicles++) { Solution new_solution = so_far; new_solution.push_back(vehicles); tryLoad(remaining_cargo - vehicles * capacity, vehicle_index + 1, new_solution, solutions); } } Calling this as follows should produce the desired output in all_solutions: Solutions all_solutions; auto inserter = std::back_inserter(all_solutions) tryLoad(100, 0, Solution{}, inserter);
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.
Distinct numbers in c++
I want to start by saying I am new to programming. I have a problem with writing a list of distinct numbers from another list in c++. Let's say I have a list l1 = {1, 12, 2, 4, 1, 3, 2} and I want to create a new list that looks like this l2 = {1, 12, 2, 4, 3}... This is what I wrote: #include <iostream> using namespace std; int main() { int l1[100], l2[100], length, length1 = 0, i, j, a = 0; cin >> length; //set the length for (i = 0; i < length; i++) { cin >> l1[i]; //add numbers to the list } l2[0] = l1[0]; //added the first number manually for (i = 0; i < length; i++) { length1++; a = 0; for (j = 0; j < length1; j++) { if (l1[i] != l2[j]) //this checks numbers in the second list a = 1; // and if they aren't found a gets the value } //1 so after it's done checking if a is 1 it if (a == 1) //will add the number to the list, but if the l2[j] = l1[i]; //number is found then a is 0 and nothing happens, } // SUPPOSEDLY for (j = 0; j < length1; j++) { cout << l2[j] << " "; } } The output of this is 1 -858993460 12 2 4 1 3 so obviously I did something very wrong. I'd welcome any suggestion you might have, I don't necessarily need a solution to this, I just want to get unstuck. Thanks a lot for taking time to reply to this.
std::sort(l1, l1 + 100); int* end_uniques = std::unique(l1, l1 + 100); std::copy(l1, end_uniques, l2); size_t num_uniques = end_uniques - l1; This is O(N log N) instead of your O(N^2) solution, so theoretically faster. It requires first sorting the array l1 (in-place) to let std::unique work. Then you get a pointer to the end of the unique elements, which you can use to copy to l2 and of course get the count (because it may be less than the full size of 100 of course).
Most Important : This solution assumes that we've to preserve order Well.... try out this one.... I've changed identifiers a bit ( of course that's not gonna affect the execution ) It'll just help us to identify what is the sake of that variable. Here's the code #include <iostream> using namespace std; int main() { int Input[100], Unique[100], InSize, UniLength = 0; cin >> InSize; for (int ii = 0 ; ii < InSize ; ii++ ) { cin >> Input[ii]; } Unique[0] = Input[0]; UniLength++; bool IsUnique; for ( int ii = 1 ; ii < InSize ; ii++ ) { IsUnique=true; for (int jj = 0 ; jj < UniLength ; jj++ ) { if ( Input[ii] == Unique[jj] ) { IsUnique=false; break; } } if ( IsUnique ) { Unique[UniLength] = Input[ii]; UniLength++; } } for ( int jj = 0 ; jj < UniLength ; jj++ ) { cout << Unique[jj] << " "; } } You were inserting Unique element at it's original index in new array..... and in place of those elements which were duplicate.... you was not doing any kind of shifting.... i.e. they were uninitialized..... and were giving something weird like -858993460 I appreciate above mentioned two answers but again..... I think this question was placed on hackerrank.... and unqiue_array() doesn't work there..... Added Of course we can only add Unique elements to our Input array..... but... this solution works..... Moreover we have 2 seconds of execution time limit.... and just 100 elements..... Keeping in mind.... that Big Oh Notation works good for really large Inputs .... Which is not case here....So there's really no point looking at time complexity....... What I'll choose is the algorithm which is easy to understand. I hope this is what you were looking for... Have a nice day.