Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
Last Updated :
13 Apr, 2021
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=1
Output: 9
Explanation:
All two digit numbers satisfying the required conditions are {11, 22, 33, 44, 55, 66, 77, 88, 99}
Input: N = 3, K = 2
Output: 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 ith digit will be equal to the (i-k)th digit for the number to satisfy the condition such that the sum of every 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 first K digits 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 10K - 10(K-1). Therefore, print the calculated value as the answer.
Below is the implementation of the above approach:
C++
// 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
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);
}
}
Python3
# 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
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
JavaScript
<script>
// Javascript 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
function count(n, k)
{
let count = Math.pow(10, k) - Math.pow(10, k - 1);
// Print the answer
document.write(count);
}
// Driver Code
let n = 2, k = 1;
count(n, k);
// This code is contributed by souravmahato348.
</script>
Time Complexity: O(log K)
Auxiliary Space: O(1)
Similar Reads
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 = 1Output: 9Explanation: The numbers are 11, 22, 33, 44, 55, 66, 77, 88, 99 with sum of every 1 consecutive digits equal
15 min read
Count of N digit Numbers having no pair of equal consecutive Digits Given an integer N, the task is to find the total count of N digit numbers such that no two consecutive digits are equal. Examples: Input: N = 2 Output: 81 Explanation: Count possible 2-digit numbers, i.e. the numbers in the range [10, 99] = 90 All 2-digit numbers having equal consecutive digits are
15 min read
Count of n digit numbers whose sum of digits equals to given sum Given two integers n and sum, the task is to find the count of all n digit numbers with sum of digits equal to sum. Note: Leading 0's are not counted as digits. If there exist no n digit number with sum of digits equal to given sum, print -1.Example: Input: n = 2, sum= 2Output: 2Explanation: The num
15+ min read
Count of N-digit numbers whose bitwise AND of adjacent digits equals 0 Given a positive integer N, the task is to count the number of N-digit numbers such that the bitwise AND of adjacent digits equals 0. Examples: Input: N = 1Output: 10Explanation: All numbers from 0 to 9 satisfy the given condition as there is only one digit. Input: N = 3Output: 264 Naive Approach: T
8 min read
Count of integer whose factorial of digit sum contains every digit of original number Given an array arr[] of length n which contains positive integers (0 ⤠arr[i] ⤠109), the task is to count the number of elements present in the given array such that the factorial of their digit sum (we will do digit sum while its value is greater than 10) contains every digit present in the origin
14 min read