# Answer to Question #13584 in C++ for prakash

Question #13584

Adjacent repetitions - have a number 13368945501113. In this number there are some adjacent repetitions. Wherever we see a number being repeated side by side, we need to add them. So the number would look like 16689410033 after the first iteration. now we see that this resulting number has repetitions too. So we need to do the same operation until there are no more adjacent repetitions. The final output of this should be 4894106.

The program should accept any number like the above, and should return it after removing all the adjacent duplicates.

The program should accept any number like the above, and should return it after removing all the adjacent duplicates.

Expert's answer

#include <iostream>

using namespace std;

#include <vector>

long long addDuplicate(long long);

int main ()

{

cout<<"Enter number, please...\n";

int num;

cin>>num;

cout<<"Result = "<<addDuplicate(num)<<endl;

system("pause");

return 0;

}

long long addDuplicate(long long number)

{

long long result = 0;

vector<long long> operationWithIt;

while(number>0){

& long long numberUsingNow;

& numberUsingNow = number%10;

& operationWithIt.push_back(numberUsingNow);

& number -= operationWithIt[operationWithIt.size() - 1];

& number /= 10;

}

int check = 1;

while (check > 0){

& check = 0;

& int add = 0;

& for (int i = operationWithIt.size() - 1 ; i > 0 ; i--){

& add = 0;

& int k;

& if (operationWithIt[i] == operationWithIt[i-1]){

& k = i + 1;

& check++;

& add += operationWithIt[i] + operationWithIt[i-1];

& i--;

& while (i > 0 && (operationWithIt[i] == operationWithIt[i-1]) ){

& add += operationWithIt[i-1];

& i--;

& }

& if (add > 9){

& operationWithIt[i] = add%10;

& operationWithIt[i+1] = (add - add%10)/10;

& } else {

& operationWithIt[i] = add;

& operationWithIt.erase(operationWithIt.begin() + i + 1, operationWithIt.begin() + k);

& }

& }

& }

}

for (int i = operationWithIt.size() - 1 ; i >= 0 ; i--)

{

& result += operationWithIt[i];

& result *= 10;

}

result /= 10;

return result;

}

using namespace std;

#include <vector>

long long addDuplicate(long long);

int main ()

{

cout<<"Enter number, please...\n";

int num;

cin>>num;

cout<<"Result = "<<addDuplicate(num)<<endl;

system("pause");

return 0;

}

long long addDuplicate(long long number)

{

long long result = 0;

vector<long long> operationWithIt;

while(number>0){

& long long numberUsingNow;

& numberUsingNow = number%10;

& operationWithIt.push_back(numberUsingNow);

& number -= operationWithIt[operationWithIt.size() - 1];

& number /= 10;

}

int check = 1;

while (check > 0){

& check = 0;

& int add = 0;

& for (int i = operationWithIt.size() - 1 ; i > 0 ; i--){

& add = 0;

& int k;

& if (operationWithIt[i] == operationWithIt[i-1]){

& k = i + 1;

& check++;

& add += operationWithIt[i] + operationWithIt[i-1];

& i--;

& while (i > 0 && (operationWithIt[i] == operationWithIt[i-1]) ){

& add += operationWithIt[i-1];

& i--;

& }

& if (add > 9){

& operationWithIt[i] = add%10;

& operationWithIt[i+1] = (add - add%10)/10;

& } else {

& operationWithIt[i] = add;

& operationWithIt.erase(operationWithIt.begin() + i + 1, operationWithIt.begin() + k);

& }

& }

& }

}

for (int i = operationWithIt.size() - 1 ; i >= 0 ; i--)

{

& result += operationWithIt[i];

& result *= 10;

}

result /= 10;

return result;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment