Number of ways to form a number with maximum Ks in it
Last Updated :
15 Dec, 2021
Given a number N and a digit K, the task is to calculate the number of ways to form a number with the maximum number of Ks in it, where in an operation, two adjacent digits of N that sum up to K are both replaced with K.
Examples :
Input :N=1454781, K=9
Output : 2
Explanation :9 can occur maximum two times after applying given operation and there are two ways of doing it. Two numbers formed are : 19479 and 14979. 194781 cannot be formed as it contains 9 only once which is not maximum.
Input : N=1007, K=8
Output : 1
Explanation : No operations can be applied on 1007.Thus, the maximum number of 8s that can be present is 0, and there is only one possible such number, which is 1007.
Approach: The following observations help in solving the problem:
- Considering the number as string, substrings of the form “ABAB…” where A+B=K are the only segments of the number that contribute to the answer.
- If the length of the contributing substring is even, there is only one way of applying the operations on them, and thus, they don’t contribute to the answer. For example, if the substring is “ABAB”, it can be changed only into “KK” to get the maximum number of K’s in the final number
- Otherwise, there will ?L/2? ways to get the maximum number of Ks, where L is the length of the substrings. For example, if the substring is “ABABA”, this can be turned into the following:
- “KKA”
- “KAK”
- “AKK”
Follow the steps below to solve the problem:
- Convert N into a string, say S.
- Initialize a variable ans to 1, to store the final answer.
- Traverse the string S and for each current index i, do the following:
- Initialize a variable count to 1, to store the length of the current answer.
- Loop while i is less than length of S, and the sum of the digits at S[i] and S[i-1] is equal to K, and do the following:
- Increment i.
- Increment count.
- If count is odd, update ans as ans*(count+1)/2.
- Return ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int noOfWays( int N, int K)
{
string S = to_string(N);
int ans = 1;
for ( int i = 1; i < S.length(); i++) {
int count = 1;
while (i < S.length()
&& S[i] - '0' + S[i - 1] - '0' == K) {
count++;
i++;
}
if (count % 2)
ans *= (count + 1) / 2;
}
return ans;
}
int main()
{
int N = 1454781;
int K = 9;
cout << noOfWays(N, K) << endl;
}
|
Java
import java.util.*;
class GFG{
static int noOfWays( int N, int K)
{
String S = String.valueOf(N);
int ans = 1 ;
for ( int i = 1 ; i < S.length(); i++)
{
int count = 1 ;
while (i < S.length() && ( int )S.charAt(i) - 48 +
( int )S.charAt(i - 1 ) - 48 == K)
{
count++;
i++;
}
if (count % 2 == 1 )
ans *= (count + 1 ) / 2 ;
}
return ans;
}
public static void main(String[] args)
{
int N = 1454781 ;
int K = 9 ;
System.out.print(noOfWays(N, K));
}
}
|
Python3
def noOfWays(N, K):
S = str (N)
ans = 1
for i in range ( 1 , len (S)):
count = 1
while (i < len (S) and ord (S[i]) +
ord (S[i - 1 ]) - 2 * ord ( '0' ) = = K):
count + = 1
i + = 1
if (count % 2 ):
ans * = (count + 1 ) / / 2
return ans
if __name__ = = '__main__' :
N = 1454781
K = 9
print (noOfWays(N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int noOfWays( int N, int K)
{
string S = N.ToString();
int ans = 1;
for ( int i = 1; i < S.Length; i++)
{
int count = 1;
while (i < S.Length && ( int )S[i] - 48 +
( int )S[i - 1] - 48 == K)
{
count++;
i++;
}
if (count % 2 == 1)
ans *= (count + 1) / 2;
}
return ans;
}
public static void Main()
{
int N = 1454781;
int K = 9;
Console.Write(noOfWays(N, K));
}
}
|
Javascript
<script>
function noOfWays(N, K)
{
let S = N.toString();
let ans = 1;
for (let i = 1; i < S.length; i++)
{
let count = 1;
while (i < S.length && S[i].charCodeAt() - 48 +
S[i - 1].charCodeAt() - 48 == K)
{
count++;
i++;
}
if (count % 2 == 1)
ans *= Math.floor((count + 1) / 2);
}
return ans;
}
let N = 1454781;
let K = 9;
document.write(noOfWays(N, K));
</script>
|
Time Complexity: O(Log10N), since, the number of digits in N is Log10N
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...