Complete to 9#11482

Complete to 9 (Editorial)

Solution

General Idea : We are given a three-digit number . For each digit of , we replace it with its complement to , which is: [ 9 - d ]

Observing that applying this operation to all three digits together is equivalent to: [ (999 - n) ]

Since is guaranteed to be a three-digit number (100 \le n \le 999), subtracting it from directly produces the desired result.

This approach is simple, efficient, and runs in constant time .


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

#define int long long
#define Moageza ios::sync_with_stdio(false); cin.tie(NULL);

void solve() {
    int n;
    cin >> n;
    cout << 999 - n;
}

signed main() {
    Moageza
    solve();
    return 0;
}