Fixing this recursive function

Hello, I am currently trying to solve a programming problem. However, I cannot get this recursive function to work:
 ``1234567891011121314151617`` ``````void mergeData(vector data) { for (int i = 0; i < data.size() - 1; i++) { for (int j = i + 1; j < data.size(); j++) { if (data.at(i) == data.at(j)) { data.at(i)++; data.erase(data.begin() + j); mergeData(data); } } } }``````

Can someone please tell me how to fix this recursive function? Thanks in advance.
Last edited on
More specifically, I am currently trying to solve this problem:
https://www.codechef.com/problems/WEAPONS

Here is my full code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include #include using namespace std; //Finds maximum element in array int findMax(vector data) { int max = data.at(0); for (int i = 1; i < data.size(); i++) { if (max < data.at(i)) { max = data.at(i); } } return max; } //Merges duplicates in array void mergeData(vector data) { for (int i = 0; i < data.size() - 1; i++) { for (int j = i + 1; j < data.size(); j++) { if (data.at(i) == data.at(j)) { data.at(i)++; data.erase(data.begin() + j); mergeData(data); } } } } //Main function int main() { int numSets; cin >> numSets; vector > data; for (int i = 0; i < numSets; i++) { int len; cin >> len; vector temp; for (int j = 0; j < len; j++) { int element; cin >> element; temp.push_back(element); } data.push_back(temp); temp.clear(); } for (int i = 0; i < numSets; i++) { mergeData(data.at(i)); cout << findMax(data.at(i)) << endl; } return 0; }``````
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include #include #ifndef NDEBUG // debugging scaffold (writing one tends to be much more productive in the long run than // unprincipled, non-repeatable hacking using a debugger) #include void show_sorted_levels( const std::vector& levels ) { std::cout << "[ " ; for( int v : levels ) std::cout << v << ' ' ; std::cout << "]\n" ; // assert the invariant assert( !levels.empty() && std::is_sorted( std::begin(levels), std::end(levels) ) ) ; } #else // #if defined NDEBUG void show( const std::vector& ) {} // do nothing; our code is already debugged #endif // NDEBUG std::vector& fuse( std::vector& levels ) // invariant: levels are sorted { show_sorted_levels(levels) ; // try to find the first two adjacent levels which are equal (in the reverse direction) // http://en.cppreference.com/w/cpp/algorithm/adjacent_find // note: fusing the first two adjacent values in the reverse direction maintains the sorted order // (because the fused value is just one greater than the original value) const auto rev_iter = std::adjacent_find( std::rbegin(levels), std::rend(levels) ) ; if( rev_iter == std::rend(levels) ) return levels ; // no equal levels, nothing more to fuse // fuse the two equal levels into a single weapon // get an iterator to the first weapon (the earlier one in the forward direction) // http://en.cppreference.com/w/cpp/iterator/reverse_iterator/base const auto iter = rev_iter.base() - 2 ; ++*iter ; // increment the level of that weapon levels.erase( iter+1 ) ; // and remove the next weapon return fuse(levels) ; // repeat the fuse operation } int max_fused_level( std::vector levels ) // return the max level after fusing weapons { if( levels.empty() ) return 0 ; // sanity check std::sort( std::begin(levels), std::end(levels) ) ; // sort the levels return fuse(levels).back() ; // the max fused level is at the back of the sorted vector } int main() // minimal test driver (the first two test cases are the ones from codechef) { std::cout << max_fused_level( { 3, 3, 1, 4 } ) << "\n-------------\n" << max_fused_level( { 2, 3, 3, 3, 1, 1 } ) << "\n-------------\n" << max_fused_level( { 2, 3, 3, 3, 1, 1, 2, 3, 3, 3, 1, 1, 4, 4, 5, 3, 3 } ) << '\n' ; }``````

http://coliru.stacked-crooked.com/a/269ff9ff4b7a1691
I'm sorry but your code doesn't seem to work on my computer.

So, I was wondering if I could modify the below code (which removes duplicates from a vector) to fuse the weapons?
 ``12`` ``````sort( vec.begin(), vec.end() ); vec.erase( unique( vec.begin(), vec.end() ), vec.end() );``````

Is this possible?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include #include #include using namespace std; vector > output; void sortOutput(vector &output) { while (unique(output.begin(), output.end()) != output.end()) { for (int i = 0; i < output.size() - 1; i++) { if (output.at(i) == output.at(i + 1)) { output.erase(output.begin() + i); output.at(i)++; } } sort(output.begin(), output.end()); } } //Finds maximum element in array int findMax(vector data) { int max = data.at(0); for (int i = 1; i < data.size(); i++) { if (max < data.at(i)) { max = data.at(i); } } return max; } //Main function int main() { int numSets; cin >> numSets; vector > data; for (int i = 0; i < numSets; i++) { int len; cin >> len; vector temp; for (int j = 0; j < len; j++) { int element; cin >> element; temp.push_back(element); } data.push_back(temp); temp.clear(); } for (int i = 0; i < numSets; i++) { sort(data.at(i).begin(), data.at(i).end()); sortOutput(data.at(i)); cout << findMax(data.at(i)) << endl; } return 0; }``````