Count of N digit Numbers whose sum of every K consecutive digits is equal

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 = 1
Output: 9
Explanation: 
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 = 2
Output: 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:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program for the above approach
#include <math.h>
#include <stdio.h>
  
// Function to count the number of 
// N-digit numbers such that sum of 
// every k consecutive digits are equal
int countDigitSum(int N, int K)
{
      
    // Range of numbers
    int l = (int)pow(10, N - 1),
        r = (int)pow(10, N) - 1;
    int count = 0;
  
    for(int i = l; i <= r; i++)
    {
        int num = i;
  
        // Extract digits of the number
        int digits[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
int main()
{
      
    // Given N and K
    int N = 2, K = 1;
  
    // Function call
    printf("%d", countDigitSum(N, K));
      
    return 0;
}
  
// This code is contributed by piyush3010 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to count the number of 
# N-digit numbers such that sum of 
# every k consecutive digits are equal
def countDigitSum(N, K):
      
    # Range of numbers
    l = pow(10, N - 1)
    r = pow(10, N) - 1
    count = 0
      
    for i in range(l, r + 1):
        num = i
  
        # Extract digits of the number
        digits = [0] * N
          
        for j in range(N - 1, -1, -1):
            digits[j] = num % 10
            num //= 10
          
        sum = 0
        flag = 0
  
        # Store the sum of first K digits
        for j in range(0, K):
            sum += digits[j]
  
        # Check for every
        # k-consective digits
        for j in range(1, N - K + 1):
            curr_sum = 0
              
            for m in range(j, j + K):
                    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 += 1
          
    return count
  
# Driver code
  
# Given N and K
N = 2
K = 1
  
# Function call
print(countDigitSum(N, K))
  
# This code is contributed by sanjoy_62

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for 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
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()
{
      
    // Given N and K
    int N = 2, K = 1;
  
    // Function call
    Console.Write(countDigitSum(N, K));
}
}
  
// This code is contributed by sanjoy_62

chevron_right


Output: 

9

Time Complexity: O(10N * 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:

  1. Get the range of numbers i.e., 10N-1  to 10N.
  2. 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.
  3. 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.
  4. If the sum obtained is equal to the above sum S then check for next K digits.
  5. Otherwise, repeat the above step for the next numbers.

Below is the implementation of the above approach:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program for the above approach
#include <stdio.h>
#include <math.h>
  
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
int countDigitSum(int N, int K)
{
      
    // Range of numbers
    int l = (int)pow(10, N - 1),
        r = (int)pow(10, N) - 1;
      
    int count = 0;
    for(int i = l; i <= r; i++)
    {
        int num = i;
  
        // Extract digits of the number
        int digits[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
int main()
{
      
    // Given integer N and K
    int N = 2, K = 1;
      
    printf("%d", countDigitSum(N, K));
      
    return 0;
}
  
// This code is contributed by piyush3010 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}
  
/* This code is contributed by piyush3010 */

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for 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
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()
{
      
    // Given N and K
    int N = 2, K = 1;
  
    // Function call
    Console.Write(countDigitSum(N, K));
}
}
  
// This code is contributed by sanjoy_62

chevron_right


Output: 

9

Time Complexity: O(10N *N)
Auxiliary Space: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.



Improved By : piyush3010, sanjoy_62