Please help debugging problem D of START103D

Problem Link

Judgement: WA

Solution:

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
const char nl = '\n';

string ans(string str) {
    ll int len = str.length();
    if (len < 3) return "-1";
    for (ll int i = len - 3; i < len; ++i) {
        for (char digit = '0'; digit <= '9'; ++digit) {
            string temp = str;
            temp[i] = digit;
            if (stoi(temp.substr(len - 3, 3)) % 8 == 0) {
                return temp;
            }
        }
    }
    return "-1";
}

inline void solve() {
    ll int N;
    string str;
    cin >> N >> str;
    if (str == "0") {
        cout << 0 << nl;
        return;
    }

    if (str.length() >= 3 && stoi(str.substr(str.length() - 3, 3)) % 8 == 0) cout << str << nl;
    else {
        string result = ans(str);
        if (result != "-1") cout << result << nl;

        else cout << 8 << nl;
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll int T;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

Hi, there could a lot tests that may cause in WA because your code is not matching for all requirements of the given task.
One of them is tests that cause in WA is,
1
1 10

the output should be, 16 or something else but your code is outputting 8 that is not length of 2.

Another input is
1
1 100
the output should be 104 or something else but your code outputted 000 that starts with leading zeros.

Did you get where is your code is not working properly?

codechef.com/viewsolution/1025297287
I have solved this problem in python. You may visit here. There, I explained this problem. I think, that would be helpful))

1 Like

Yes, thanks a lot.

1 Like