Numbers having difference with digit sum more than s


The digit sum for a given number is the sum of all the digits present in the given number. We will be given a number n and s and we have to find all the numbers which are in the range 1 to n and have the difference between the number and the sum of its digits greater than s. We will implement two approaches with the code and the discussion on time and space complexity.

Input

N = 15, S = 5

Output

6

Explanation

For all the numbers in the range 0 to 9, the difference between digit sum is 0. 10 to 15, each number has a difference of 9 between the digits.

Input

N = 20, S = 25

Output

0

Explanation

For all the numbers in the range 0 to 9, the difference between the digit sum is 0. Similarly, the difference between the number and the digits is 9 for each number in the range of 10 to 19.

For 20 the difference is 19.

Observation

Before implementing our first algorithm let us see a general trend present between the numbers.

For each number in the range, 0 to 9 the difference between the digits is 0.

For each number in the range, 10 to 19 the difference between the digits is 9.

For each number in the range, 100 to 109 the difference between the digits is 99.

By using the observation, we will implement the code.

Approach 1

In this approach, we will traverse over the given range from 1 to n with a difference of less than s.

Example

#include <iostream>
using namespace std;
// function to find the count of numbers with difference of more than s
int count(int n, int s){    
   // base condition 
   if(s == 0){
      // 1 to 9 have a difference of 0
      return n-9;
   }
   else{        
      // traversing over the number with the gap of 10
      int diff = 0; // variable to store the previous difference 
      for(int i = 10; i<n; i += 10){
         int cur = i;
         int digitSum = 0;
         while(cur){
            digitSum += cur%10;
            cur /= 10;
         }            
         diff = i-digitSum; 
         if(diff > s){
            return n-i+1;
         }
      }
      return 0;
   }
}
int main(){
   int n = 20;
   int s = 6;    
   cout<<"The total numbers with the difference between the number and sum of digits are greater than "<< s << " in the range of 1 to "<< n << " are: ";
   // calling to function 
   cout<<count(n,s)<<endl;
   return 0;
}

Output

The total numbers with the difference between the number and sum of digits are greater than 6 in the range of 1 to 20 are: 11

Time and Space Complexity

The time complexity of the above code is O(N*log(N)). We have used the factor of 10 every time to traverse but it does not put that much impact.

The space complexity of the above is O(1), as we have not used any extra space.

In the previous approach, we implemented an observation by dividing the number by 10, but still, this was not impactful, so no we will move to another approach which is far better.

Approach 2

In C++ language we can have a maximum number of size 1e18 and there will be a total of 18 digits present in it. Let us assume all the digits are 9 then we will get 18 * 9 which is 162. So the maximum difference between the number and the digit can be 162.

  • All the numbers less than s will have a difference less than s

  • All the numbers greater than s+162 have a difference greater than it.

So, we will traverse over the number from s to a minimum of n and s + 162 and find the number with a difference greater than s.

Example

#include <bits/stdc++.h>
using namespace std;
// function to get the digit sum 
int digitSum(int cur){
   int sum = 0;
   while(cur){
      sum += cur%10;
      cur /= 10;
   }
   return sum;
}
// function to find the count of number with difference more than s
int count(int n, int s){    
   // getting number minimum of n and s + 163
   int mi =  min(n, s + 163);
   for(int i = s; i<= mi; i++){
      int sum = digitSum(i);
      if(i - sum > s){
         return n - i +1;
      }
   }
   return 0;    
}
int main(){
   int n = 1000;
   int s = 100;    
   cout<<"The total numbers with the difference between the number and sum of digits are greater than "<< s << " in the range of 1 to "<< n << " are: ";
   // calling to function 
   cout<<count(n,s)<<endl;
   return 0;
}

Output

The total numbers with the difference between the number and sum of digits are greater than 100 in the range of 1 to 1000 are: 891

Time and Space Complexity

The time complexity of the above code is O(log(S)) because we have just traversed over the 163 elements and found their digit sum in log time.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In this tutorial, we have implemented a program to find the total number of integers in the range 1 to the given number n that have the difference between the number and sum of digits is greater than a given number s. We have implemented two approaches one with the time complexity of O(N*log(N)) and constant space and while the second one with constant space and logarithmic time complexity.

Updated on: 01-Sep-2023

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements