#include <bits/stdc++.h>
using namespace std;
vector<int> plusOne(vector<int>& digits) {
int sum, carry = 0, n = digits.size();
//add 1 to the last digit
sum = digits[n - 1] + 1;
//see if there is a carry generated
if(sum > 9){
carry = 1;
digits[n - 1] = sum % 10;
}else{
//if no carry, add 1 to lsb and return
digits[n - 1] = sum;
return digits;
}
//if carry, keep adding till there is no carry
for(int i = n - 2; i >= 0; i--){
sum = digits[i] + carry;
if(sum > 9){
carry = 1;
digits[i] = sum % 10;
}else{
//if no carry, return
digits[i] = sum;
return digits;
}
}
//add 1 to the front if there is carry in the end
if(carry){
digits.insert(digits.begin(), 1);
}
return digits;
}
int main() {
int myints[] = {1, 2, 9};
vector<int> v(myints, myints+3);
vector<int> result = plusOne(v);
for(int i = 0 ; i < result.size(); i++){
cout << result[i];
}
cout << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2ZWN0b3I8aW50PiBwbHVzT25lKHZlY3RvcjxpbnQ+JiBkaWdpdHMpIHsKICAgIGludCBzdW0sIGNhcnJ5ID0gMCwgbiA9IGRpZ2l0cy5zaXplKCk7CiAgICAvL2FkZCAxIHRvIHRoZSBsYXN0IGRpZ2l0CiAgICBzdW0gPSBkaWdpdHNbbiAtIDFdICsgMTsKICAgIC8vc2VlIGlmIHRoZXJlIGlzIGEgY2FycnkgZ2VuZXJhdGVkCiAgICBpZihzdW0gPiA5KXsKICAgICAgICBjYXJyeSA9IDE7CiAgICAgICAgZGlnaXRzW24gLSAxXSA9IHN1bSAlIDEwOwogICAgfWVsc2V7CiAgICAgICAgLy9pZiBubyBjYXJyeSwgYWRkIDEgdG8gbHNiIGFuZCByZXR1cm4KICAgICAgICBkaWdpdHNbbiAtIDFdID0gc3VtOwogICAgICAgIHJldHVybiBkaWdpdHM7CiAgICB9CiAgICAvL2lmIGNhcnJ5LCBrZWVwIGFkZGluZyB0aWxsIHRoZXJlIGlzIG5vIGNhcnJ5Ci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