Problem:
Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cstdio>
#include <vector>

using namespace std;

void get(vector <int> &A, int &max_1, int &max_2)
{
    for(int i = 0; i < A.size(); i++)
    {
        if(A[i] >= A[max_1])
        {
            max_2 = max_1;
            max_1 = i;
        }
        else if(A[i] > A[max_2])
        {
            max_2 = i;
        }
    }
}

int main()
{
    int no_of_elements;
    scanf("%d", &no_of_elements);

    const int MAX_N = 1e5 + 5;
    vector <int> odd_frequency(MAX_N, 0);
    vector <int> even_frequency(MAX_N, 0);

    for(int i = 1; i <= no_of_elements; i++)
    {
        int element;
        scanf("%d", &element);

        if(i%2 == 0)
        {
            even_frequency[element]++;
        }
        else if(i%2 == 1)
        {
            odd_frequency[element]++;
        }
    }

    int most_frequent_even_element = 0, second_most_frequent_even_element = 0;
    get(even_frequency, most_frequent_even_element, second_most_frequent_even_element);

    int most_frequent_odd_element = 0, second_most_frequent_odd_element = 0;
    get(odd_frequency, most_frequent_odd_element, second_most_frequent_odd_element);

    int even_changes = 0, odd_changes = 0;
    if(most_frequent_even_element != most_frequent_odd_element)
    {
        even_changes = no_of_elements/2 - even_frequency[most_frequent_even_element];
        odd_changes = no_of_elements/2 - odd_frequency[most_frequent_odd_element];
    }
    else if(most_frequent_even_element == most_frequent_odd_element)
    {
        if(even_frequency[most_frequent_even_element] > odd_frequency[most_frequent_odd_element])
        {
            even_changes = no_of_elements/2 - even_frequency[most_frequent_even_element];
            odd_changes = no_of_elements/2 - odd_frequency[second_most_frequent_odd_element];
        }
        if(even_frequency[most_frequent_even_element] < odd_frequency[most_frequent_odd_element])
        {
            even_changes = no_of_elements/2 - even_frequency[second_most_frequent_even_element];
            odd_changes = no_of_elements/2 - odd_frequency[most_frequent_odd_element];
        }
      if(even_frequency[most_frequent_even_element] == odd_frequency[most_frequent_odd_element])
        {
            if(odd_frequency[second_most_frequent_odd_element]<even_frequency[second_most_frequent_even_element])
            {
              even_changes = no_of_elements/2 - even_frequency[second_most_frequent_even_element];
            odd_changes = no_of_elements/2 - odd_frequency[most_frequent_odd_element];
            }
        else
        {
          even_changes = no_of_elements/2 - even_frequency[most_frequent_even_element];
            odd_changes = no_of_elements/2 - odd_frequency[second_most_frequent_odd_element];
        }
        }
    }

    int changes = even_changes + odd_changes;
    printf("%d\n", changes);
    return 0;
}