Given two integers **N **and **K**, the task is to find the total count of **N**-digit number such that the sum of every **K **consecutive digits of the number is equal.

**Examples:**

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

The numbers are 11, 22, 33, 44, 55, 66, 77, 88, 99 with sum of every 1 consecutive digits equal to 1, 2, 3, 4, 5, 6, 7, 8, 9 respectively.

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

**Naive Approach:** Iterate for all possible **N-digit** numbers and calculate the sum of every **K** consecutive digits of the number. If all the sums are equal then include this is the count else check for the next number.

Below is the implementation of the above approach:

## Java

`// Java program for the above approach ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count the number of ` ` ` `// N-digit numbers such that sum of ` ` ` `// every k consecutive digits are equal ` ` ` `static` `int` `countDigitSum(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `// Range of numbers ` ` ` `int` `l = (` `int` `)Math.pow(` `10` `, N - ` `1` `), ` ` ` `r = (` `int` `)Math.pow(` `10` `, N) - ` `1` `; ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = l; i <= r; i++) { ` ` ` `int` `num = i; ` ` ` ` ` `// Extract digits of ` ` ` `// the number ` ` ` `int` `digits[] = ` `new` `int` `[N]; ` ` ` ` ` `for` `(` `int` `j = N - ` `1` `; ` ` ` `j >= ` `0` `; j--) { ` ` ` ` ` `digits[j] = num % ` `10` `; ` ` ` `num /= ` `10` `; ` ` ` `} ` ` ` `int` `sum = ` `0` `, flag = ` `0` `; ` ` ` ` ` `// Store the sum of ` ` ` `// first K digits ` ` ` `for` `(` `int` `j = ` `0` `; j < K; j++) ` ` ` `sum += digits[j]; ` ` ` ` ` `// Check for every ` ` ` `// k-consective digits ` ` ` `for` `(` `int` `j = ` `1` `; ` ` ` `j < N - K + ` `1` `; j++) { ` ` ` ` ` `int` `curr_sum = ` `0` `; ` ` ` ` ` `for` `(` `int` `m = j; ` ` ` `m < j + K; m++) { ` ` ` ` ` `curr_sum += digits[m]; ` ` ` `} ` ` ` ` ` `// If sum is not equal ` ` ` `// then break the loop ` ` ` `if` `(sum != curr_sum) { ` ` ` `flag = ` `1` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Increment the count if it ` ` ` `// satisfy the given condition ` ` ` `if` `(flag == ` `0` `) { ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` ` ` `main(String[] args) ` ` ` `{ ` ` ` `// Given N and K ` ` ` `int` `N = ` `2` `, K = ` `1` `; ` ` ` ` ` `// Function call ` ` ` `System.out.print(countDigitSum(N, K)); ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output:**

9

**Time Complexity:** O(10^{N }* N * K)**Auxiliary Space:** O(N)

**Efficient Approach:** To optimize the above naive approach the idea is to use the Sliding window technique to check if the sum of K-Consecutive digits of the number are equal or not. Below are the steps:

- Get the range of numbers i.e.,
**10**.^{N-1 }to 10^{N} - For each number in the above range, consider a window of length
**K**and find the sum of each digit. Store this sum as**S**. - Find the sum of next K digits using the sliding window by including the next
**K**digits in the sum and remove the previous**K**digits from the sum. - If the sum obtained is equal to the above sum
**S**then check for next**K**digits. - Otherwise, repeat the above step for the next numbers.

Below is the implementation of the above approach:

## Java

`// Java program for the above approach ` `class` `GFG { ` ` ` ` ` `// Function to count the number of ` ` ` `// N-digit numbers such that sum of ` ` ` `// every k consecutive digits are equal ` ` ` `static` `int` `countDigitSum(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `// Range of numbers ` ` ` `int` `l = (` `int` `)Math.pow(` `10` `, N - ` `1` `), ` ` ` `r = (` `int` `)Math.pow(` `10` `, N) - ` `1` `; ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = l; i <= r; i++) { ` ` ` `int` `num = i; ` ` ` ` ` `// Extract digits of the number ` ` ` `int` `digits[] = ` `new` `int` `[N]; ` ` ` `for` `(` `int` `j = N - ` `1` `; j >= ` `0` `; j--) { ` ` ` `digits[j] = num % ` `10` `; ` ` ` `num /= ` `10` `; ` ` ` `} ` ` ` `int` `sum = ` `0` `, flag = ` `0` `; ` ` ` ` ` `// Store the sum of ` ` ` `// first K digits ` ` ` `for` `(` `int` `j = ` `0` `; j < K; j++) ` ` ` `sum += digits[j]; ` ` ` ` ` `// Check for every ` ` ` `// k-consective digits ` ` ` `// using sliding window ` ` ` `for` `(` `int` `j = K; j < N; j++) { ` ` ` ` ` `if` `(sum - digits[j - K] ` ` ` `+ digits[j] ` ` ` `!= sum) { ` ` ` `flag = ` `1` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `if` `(flag == ` `0` `) { ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Given integer N and K ` ` ` `int` `N = ` `2` `, K = ` `1` `; ` ` ` `System.out.print(countDigitSum(N, K)); ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output:**

9

**Time Complexity: ***O(10 ^{N} *N)*

**Auxiliary Space:**

*O(N)*

## Recommended Posts:

- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- 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 with exactly K non-zero digits and distinct odd digit sum
- Count of n digit numbers whose sum of digits equals to given sum
- Count of N-digit numbers with all distinct digits
- 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 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
- Print all n-digit numbers whose sum of digits equals to given sum
- N digit numbers divisible by 5 formed from the M digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M

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.