Reinfected Solution | CodeChef Puzzle in C++

Problem

We are very near to our goal now. The enemy is being weakened continuously. But alas, sensing his fall, the hacker has spread a worm into our network. It is consuming all our files at a tremendous rate and must be stopped at all costs. The only way to stop it is to enter a specific code number in to the worm.

CodeChef Problem Link

Input

The first line will consist of the total number of test cases T. The next T lines will consist of number N on each line.

Output

For each test case, output is a number.

Sample Input
3
34
156
893
Sample Output
6
0
13
Solution

The idea is to print the remainder that is obtained when the number N is divided by the sum of digits of number N.

Code Implementation

//
//  main.cpp
//  Reinfected
//
//  Created by Himanshu on 20/02/22.
//

#include <iostream>
using namespace std;

int main() {
    int T;
    cin>>T;
    
    while (T--) {

        int num, sum = 0, dgt, ans;
        cin>>num;
        
        int temp = num;
        
        while (temp > 0) {
             dgt = temp % 10;
             sum+=dgt;
             temp = temp/10;
        }
        
        ans = num%sum;
        cout<<ans<<endl;
    }

    return 0;
}

Time Complexity: O(log(n))

Leave a Reply

Your email address will not be published. Required fields are marked *