Given N, the number of digits of an integer which is greater than or equal to 2 and a weight W. The task is to find the count of integers which have N digits and weight W.
Note: Weight is defined as the difference between the consecutive digits of an integer.
Input : N = 2, W = 3 Output : 6 Input : N = 2, W = 4 Output : 5
In the above example, the total possible 2 digit integers with weight equal 3 will be 6. Like the number 14 has weight 3 (4-1) and 25, 36, 47, 58, 69 has weight 3. If we see it carefully we’ll find the logic that if we increment the weight as 5 of a 2 digit number, then the total possible such numbers will be 5. With weight 6 of 2 digit number, the total possible numbers will be 4 and then 3 and so on. Also, if we increase the number of digits. Say, n equal to 3 with weight 3, then the total possible numbers will be 60 and 600 for n equal to 4 with weight 3 and so on.
Number of digits | Weight —> Total possible such numbers
|2|2 —> 7||2|3 —> 6||2|4 —> 5||2|5 —> 4||2|6 —> 3||2|7 —> 2||2|8 —> 1|
|3|2 —> 70||3|3 —> 60||3|4 —> 50||3|5 —> 40||3|6 —> 30||3|7 —> 20||3|8 —> 10|
|4|2 —>700||4|3 —>600||4|4 —>500||4|5 —>400||4|6 —>300||4|7 —>200||4|8 —>100|
As you can see in the above table that with increase in the number of digits, the quantity of numbers with weight ‘w’ is following a pattern, where it is changing in the multiple of 10^(n-2), where ‘n’ is the number of digits.
Below is the step by step algorithm to solve this problem:
- Check if the given Weight(W) is Positive or Negative.
- Subtract Weight(W) from 9 if positive.
- Add Weight to 10 if it is negative and then update the new weight.
- For n digit integer, multiply 10^(n-2) with this updated weight.
- This will give us the number of integers satisfying this weight.
Below is the implementaion of above approach:
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 'd' digit positive integers with 0 as a digit
- Count of N-digit numbers having digit XOR as single digit
- Largest number less than N with digit sum greater than the digit sum of N
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find the remainder when First digit of a number is divided by its Last digit
- Last digit of a number raised to last digit of N factorial
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Count the nodes in the given tree whose weight is a powerful number
- Count the nodes in the given Tree whose weight is a Perfect Number
- Count positive integers with 0 as a digit and maximum 'd' digits
- Count of m digit integers that are divisible by an integer n
- Sum of integers upto N with given unit digit
- Integers from the range that are composed of a single distinct digit
- Sum of integers upto N with given unit digit (Set 2)
- Sum of last digit of all integers from 1 to N divisible by M
- Check if frequency of each digit is less than the digit
- Count n digit numbers not having a particular digit
- Count numbers in a range with digit sum divisible by K having first and last digit different
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.