Given an integer N, find out the count of numbers(m) that satisfy the condition m + sum(m) + sum (sum(m)) = N, where sum(m) denotes the sum of digits in m. Given N <= 10e9.
Input: 9 Output: 1 Explanation: Only 1 positive integer satisfies the condition that is 3, 3 + sum(3) + sum(sum(3)) = 3 + 3 + 3 = 9 Input: 9939 Output: 4 Explanation: m can be 9898, 9907, 9910 and 9913. 9898 + sum(9898) + sum(sum(9898)) = 9898 + 34 + 7 = 9939. 9907 + sum(9907) + sum(sum(9907)) = 9907 + 25 + 7 = 9939. 9910 + sum(9910) + sum(sum(9910)) = 9910 + 19 + 10 = 9939. 9913 + sum(9913) + sum(sum(9913)) = 9913 + 22 + 4 = 9939.
Approach: The first thing to note is that we are given the constraint N<=10e9. This means sum(x) can at maximum be 81 for any number, This is because the largest number below 10e9 is 999999999 whose digits add up to 81. The maximum case for sum(sum(x)) will be 16 (number being 79<=81), so at max it 81 + 16 which is 97 needs to be checked. We just need to iterate from N – 97 to N and check which integers satisfy the equation because no integer smaller than N-97 can satisfy the equation and neither can any integer greater than N can.
Below is the implementation of the above approach.
This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N
- Generate N integers satisfying the given conditions
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Check if a given pair of Numbers are Betrothed numbers or not
- Count of Binary Digit numbers smaller than N
- Count numbers with same first and last digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count natural numbers whose all permutation are greater than that number
- Count numbers with difference between number and its digit sum greater than specific value
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count numbers with unit digit k in given range
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count n digit numbers not having a particular digit
- Count numbers formed by given two digit with sum having given digits
- Count numbers (smaller than or equal to N) with given digit sum
- Count Unary Numbers in a Range
- Count the number of carry operations required to add two numbers
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count Numbers with N digits which consists of even number of 0’s
- Count of Numbers in Range where first digit is equal to last digit of the number