Count lexicographically increasing K | C ++ program for the above approach ; Function to count K - length strings from first N alphabets ; To keep track of column sum in dp ; Auxiliary 2d dp array ; Initialize dp [ 0 ] [ i ] = 1 and update the column_sum ; Iterate for K times ; Iterate for N times ; dp [ i ] [ j ] : Stores the number of ways to form i - length strings consisting of j letters ; Update the column_sum ; Print number of ways to arrange K - length strings with N alphabets ; Driver Code ; Given N and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void waysToArrangeKLengthStrings ( int N , int K ) { int column_sum [ N + 1 ] = { 0 } , i , j ; int dp [ K + 1 ] [ N + 1 ] = { 0 } ; for ( i = 0 ; i <= N ; i ++ ) { dp [ 0 ] [ i ] = 1 ; column_sum [ i ] = 1 ; } for ( i = 1 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { dp [ i ] [ j ] += column_sum [ j - 1 ] ; column_sum [ j ] += dp [ i ] [ j ] ; } } cout << dp [ K ] [ N ] ; } int main ( ) { int N = 5 , K = 2 ; waysToArrangeKLengthStrings ( N , K ) ; return 0 ; }