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:

## Java

`// 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*

## C#

`// 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
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of N-digit numbers in base K with no two consecutive zeroes
- Count numbers formed by given two digit with sum having given digits
- Count of N-digit numbers with all distinct digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count numbers (smaller than or equal to N) with given digit sum
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in a range with digit sum divisible by K having first and last digit different
- N digit numbers divisible by 5 formed from the M digits
- 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 of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s

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.