Count numbers with difference between number and its digit sum greater than specific value
Last Updated :
22 Apr, 2021
Given a positive value N, we need to find the count of numbers smaller than N such that the difference between the number and sum of its digits is greater than or equal to given specific value diff.
Examples:
Input : N = 13, diff = 2
Output : 4
Then 10, 11, 12 and 13 satisfy the given
condition shown below,
10 – sumofdigit(10) = 9 >= 2
11 – sumofdigit(11) = 9 >= 2
12 – sumofdigit(12) = 9 >= 2
13 – sumofdigit(13) = 9 >= 2
Whereas no number from 1 to 9 satisfies
above equation so final result will be 4
We can solve this problem by observing a fact that for a number k less than N,
if k – sumofdigit(k) >= diff then
above equation will be true for (k+1)
also because we know that sumofdigit(k+1)
is not greater than sumofdigit(k) + 1
so, k + 1 - sumofdigit(k + 1) >=
k - sumofdigit(k)
but we know that right side of above
inequality is greater than diff,
so left side will also be greater than
diff.
So, finally we can say that if a number k satisfies the difference condition then (k + 1) will also satisfy same equation so our job is to find the smallest number which satisfies the difference condition then all numbers greater than this and up to N will satisfy the condition so our answer will be N – smallest number we found.
We can find the smallest number satisfying this condition using binary search so total time complexity of solution will be O(log N)
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfDigit( int K)
{
int sod = 0;
while (K)
{
sod += K % 10;
K /= 10;
}
return sod;
}
int totalNumbersWithSpecificDifference( int N, int diff)
{
int low = 1, high = N;
while (low <= high)
{
int mid = (low + high) / 2;
if (mid - sumOfDigit(mid) < diff)
low = mid + 1;
else
high = mid - 1;
}
return (N - high);
}
int main()
{
int N = 13;
int diff = 2;
cout << totalNumbersWithSpecificDifference(N, diff);
return 0;
}
|
Java
class Test
{
static int sumOfDigit( int K)
{
int sod = 0 ;
while (K != 0 )
{
sod += K % 10 ;
K /= 10 ;
}
return sod;
}
static int totalNumbersWithSpecificDifference( int N, int diff)
{
int low = 1 , high = N;
while (low <= high)
{
int mid = (low + high) / 2 ;
if (mid - sumOfDigit(mid) < diff)
low = mid + 1 ;
else
high = mid - 1 ;
}
return (N - high);
}
public static void main(String args[])
{
int N = 13 ;
int diff = 2 ;
System.out.println(totalNumbersWithSpecificDifference(N, diff));
}
}
|
Python3
def sumOfDigit(K):
sod = 0
while (K):
sod = sod + K % 10
K = K / / 10
return sod
def totalNumbersWithSpecificDifference(N,diff):
low = 1
high = N
while (low < = high):
mid = (low + high) / / 2
if (mid - sumOfDigit(mid) < diff):
low = mid + 1
else :
high = mid - 1
return (N - high)
N = 13
diff = 2
print (totalNumbersWithSpecificDifference(N, diff))
|
C#
using System;
class Test {
static int sumOfDigit( int K)
{
int sod = 0;
while (K != 0)
{
sod += K % 10;
K /= 10;
}
return sod;
}
static int totalNumbersWithSpecificDifference( int N,
int diff)
{
int low = 1, high = N;
while (low <= high)
{
int mid = (low + high) / 2;
if (mid - sumOfDigit(mid) < diff)
low = mid + 1;
else
high = mid - 1;
}
return (N - high);
}
public static void Main()
{
int N = 13;
int diff = 2;
Console.Write(totalNumbersWithSpecificDifference(N, diff));
}
}
|
PHP
<?php
function sumOfDigit( $K )
{
$sod = 0;
while ( $K )
{
$sod += $K % 10;
$K /= 10;
}
return $sod ;
}
function totalNumbersWithSpecificDifference( $N , $diff )
{
$low = 1; $high = $N ;
while ( $low <= $high )
{
$mid = floor (( $low + $high ) / 2);
if ( $mid - sumOfDigit( $mid ) < $diff )
$low = $mid + 1;
else
$high = $mid - 1;
}
return ( $N - $high );
}
$N = 13;
$diff = 2;
echo totalNumbersWithSpecificDifference( $N , $diff );
?>
|
Javascript
<script>
function sumOfDigit(K)
{
let sod = 0;
while (K)
{
sod += K % 10;
K /= 10;
}
return sod;
}
function
totalNumbersWithSpecificDifference(N, diff)
{
let low = 1;
let high = N;
while (low <= high)
{
let mid = Math.floor((low + high) / 2);
if (mid - sumOfDigit(mid) < diff)
low = mid + 1;
else
high = mid - 1;
}
return (N - high);
}
let N = 13;
let diff = 2;
document.write(
totalNumbersWithSpecificDifference(N, diff)
);
</script>
|
Output:
4
Share your thoughts in the comments
Please Login to comment...