Split the number N by maximizing the count of subparts divisible by K
Last Updated :
18 Feb, 2022
Given a numeric string N and an integer K, the task is to split digits of N into subparts such that the number of segments divisible by K is maximized.
Note: We can make any number of vertical cuts between pairs of adjacent digits.
Examples:
Input: N = 32, K = 4
Output: 1
Explanation:
32 is divisible by 4 but none if its digits are individually divisible, so we don’t perform any splits.
Input: N = 2050, K = 5
Output: 3
Explanation:
2050 can be split into 2, 0, 5, 0 where 0, 5, 0 are divisible by 5.
Input: N = 00001242, K = 3
Output: 6
Explanation:
00001242 can be split into 0, 0, 0, 0, 12, 42 where all the parts are divisible by 3.
Approach:
To solve the problem mentioned above we will try to use a recursive approach.
- Check if there is a vertical partition between the current character and the next one, if no then we perform recursion again for the next index and update the substring value by concatenating the present character.
- Now, if there is a vertical partition between the current character and the next character, then there exist two cases:
- If the present subStr is divisible by X, then we add 1 because the present subStr is 1 of the possible answer, then recur for the next index and update subStr as an empty string.
- If the present subStr is not divisible by X, then we simply recur for the next index and update subStr as an empty string.
- Return a maximum of the two possible cases mentioned above.
Below is the implementation of the above logic
C++
#include <bits/stdc++.h>
using namespace std;
int count(string N, int X,
string subStr,
int index, int n)
{
if (index == n)
return 0;
string a = subStr + N[index];
int b = 0;
if (stoll(a) % X == 0)
b = 1;
int m1 = count(N, X, a, index + 1, n);
int m2 = b + count(N, X, "" ,
index + 1, n);
return max(m1, m2);
}
int main()
{
string N = "00001242" ;
int K = 3;
int l = N.length();
cout << count(N, K, "" , 0, l)
<< endl;
return 0;
}
|
Java
class GFG{
static int count(String N, int X,
String subStr,
int index, int n)
{
if (index == n)
return 0 ;
String a = subStr + N.charAt(index);
int b = 0 ;
if (Long.valueOf(a) % X == 0 )
b = 1 ;
int m1 = count(N, X, a, index + 1 , n);
int m2 = b + count(N, X, "" ,
index + 1 , n);
return Math.max(m1, m2);
}
public static void main(String[] args)
{
String N = "00001242" ;
int K = 3 ;
int l = N.length();
System.out.print(count(N, K, "" , 0 , l) + "\n" );
}
}
|
Python3
def count(N, X, subStr, index, n):
if (index = = n):
return 0
a = subStr + N[index]
b = 0
if ( int (a) % X = = 0 ):
b = 1
m1 = count(N, X, a, index + 1 , n)
m2 = b + count(N, X, "", index + 1 , n)
return max (m1, m2)
N = "00001242"
K = 3
l = len (N)
print (count(N, K, "", 0 , l))
|
C#
using System;
class GFG{
static int count(String N, int X,
String subStr,
int index, int n)
{
if (index == n)
return 0;
String a = subStr + N[index];
int b = 0;
if ( long . Parse(a) % X == 0)
b = 1;
int m1 = count(N, X, a, index + 1, n);
int m2 = b + count(N, X, "" ,
index + 1, n);
return Math.Max(m1, m2);
}
public static void Main(String[] args)
{
String N = "00001242" ;
int K = 3;
int l = N.Length;
Console.Write(count(N, K, "" , 0, l) + "\n" );
}
}
|
Javascript
<script>
function count(N, X,
subStr,
index, n)
{
if (index == n)
return 0;
let a = subStr + N[index];
let b = 0;
if (parseInt(a) % X == 0)
b = 1;
let m1 = count(N, X, a, index + 1, n);
let m2 = b + count(N, X, "" ,
index + 1, n);
return Math.max(m1, m2);
}
let N = "00001242" ;
let K = 3;
let l = N.length;
document.write(count(N, K, "" , 0, l) + "\n" );
</script>
|
Share your thoughts in the comments
Please Login to comment...