Given two integers **N **and **K**, the task is to find the count of all possible **N-digit** numbers having sum of every **K** consecutive digits of the number are equal.**Examples:**

Input:N = 2, K=1Output:9Explanation:

All two digit numbers satisfying the required conditions are {11, 22, 33, 44, 55, 66, 77, 88, 99}

Input:N = 3, K = 2Output:90

**Naive and Sliding Window Approach:** Refer to Count of N digit Numbers whose sum of every K consecutive digits is equal for the simplest approach and Sliding Window technique based approach.**Logarithmic Approach:**

For the sum of **K**-consecutive elements to be always equal, the entire number is governed by its first **K** digits.

- The
**i**digit will be equal to the^{th}**(i-k)**digit for the number to satisfy the condition such that the sum of every^{th}**K**consecutive digit is the same.

Illustration:

N = 5 and K = 2

If the first two digits are 1 and 2, then the number has to be 12121 so that sum of every 2 consecutive digits is 3.

Observe that the first 2 digits i.e. the firstKdigits repeats itself.

Therefore, to solve the problem, the task is now to find out the total count of **K-digit** numbers which is equal to **10 ^{K }– 10^{(K-1)}**. Therefore, print the calculated value as the answer.

Below is the implementation of the above approach:

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to count the number of ` `// N-digit numbers such that sum of ` `// every K consecutive digits are equal ` `void` `count(` `int` `n, ` `int` `k) `
`{ ` ` ` `long` `count = (` `long` `)(` `pow` `(10, k) - ` `pow` `(10, k - 1)); `
` ` ` ` `// Print the answer `
` ` `cout << (count); `
`} ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` `int` `n = 2, k = 1; `
` ` `count(n, k); `
`} ` ` ` `// This code is contributed by Ritik Bansal` |

*chevron_right*

*filter_none*

`// Java Program to implement ` `// the above approach ` `class` `GFG { `
` ` ` ` `// Function to count the number of `
` ` `// N-digit numbers such that sum of `
` ` `// every K consecutive digits are equal `
` ` `public` `static` `void` `count(` `int` `n, ` `int` `k) `
` ` `{ `
` ` `long` `count `
` ` `= (` `long` `)(Math.pow(` `10` `, k) `
` ` `- Math.pow(` `10` `, k - ` `1` `)); `
` ` ` ` `// Print the answer `
` ` `System.out.print(count); `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `main(String[] args) `
` ` `{ `
` ` `int` `n = ` `2` `, k = ` `1` `; `
` ` `count(n, k); `
` ` `} `
`} ` |

*chevron_right*

*filter_none*

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to count the number of ` `# N-digit numbers such that sum of ` `# every K consecutive digits are equal ` `def` `count(n, k): `
` ` ` ` `count ` `=` `(` `pow` `(` `10` `, k) ` `-` `pow` `(` `10` `, k ` `-` `1` `)); `
` ` ` ` `# Print the answer `
` ` `print` `(count); `
` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: `
` ` ` ` `n ` `=` `2` `; `
` ` `k ` `=` `1` `; `
` ` ` ` `count(n, k); `
` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

`// C# Program to implement ` `// the above approach ` `using` `System; `
`class` `GFG{ `
` ` ` ` `// Function to count the number of `
` ` `// N-digit numbers such that sum of `
` ` `// every K consecutive digits are equal `
` ` `public` `static` `void` `count(` `int` `n, ` `int` `k) `
` ` `{ `
` ` `long` `count = (` `long` `)(Math.Pow(10, k) - `
` ` `Math.Pow(10, k - 1)); `
` ` ` ` `// Print the answer `
` ` `Console.Write(count); `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `Main(String[] args) `
` ` `{ `
` ` `int` `n = 2, k = 1; `
` ` `count(n, k); `
` ` `} `
`} ` ` ` `// This code is contributed by Rohit_ranjan` |

*chevron_right*

*filter_none*

**Output:**

9

**Time Complexity:** O(log K) **Auxiliary Space: **O(1)

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.

## Recommended Posts:

- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count of N digit Numbers having no pair of equal consecutive Digits
- Count of n digit numbers whose sum of digits equals to given sum
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of N-digit numbers having digit XOR as single digit
- Print all n-digit numbers whose sum of digits equals to given sum
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count all N digit numbers whose digits are multiple of X
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count of Numbers in Range where first digit is equal to last digit of the number
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Maximum sum of two elements whose digit sum is equal
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.