Divide number into two parts divisible by given numbers
Last Updated :
08 Apr, 2023
Given a large number in string format and we are also given two numbers f and s. We need to divide the large number into two continuous parts such that the first part is divisible by f and the second part is divisible by s.
Examples:
Input: num = “246904096”
f = 12345
s = 1024
Output: Yes
We can divide num into “24690” and “4096”
which are divisible by f and s respectively.
Input : num = “1234”
f = 1
s = 123
Output : No
We can not divide num into two parts under
given constraint.
A simple solution is to generate all divisions of a given number and check if a division is divisible.
We can solve this problem by storing remainders of each prefix and suffix of the given string and if at any index, prefix reminder and suffix reminder both are zero, then we know that we can break the string at this index. The procedure is explained for the above example,
String = “246904096”
All prefix reminders with 12345 are,
Prefix 2 reminder with 12345, 2
prefix 24 reminder with 12345, 24
prefix 246 reminder with 12345, 246
prefix 2469 reminder with 12345, 2469
prefix 24690 reminder with 12345, 0
prefix 246904 reminder with 12345, 4
prefix 2469040 reminder with 12345, 40
prefix 24690409 reminder with 12345, 409
prefix 246904096 reminder with 12345, 4096
All suffix reminder with 1024 are,
Suffix 6 reminder with 1024, 6
Suffix 96 reminder with 1024, 96
Suffix 096 reminder with 1024, 96
Suffix 4096 reminder with 1024, 0
Suffix 04096 reminder with 1024, 0
Suffix 904096 reminder with 1024, 928
Suffix 6904096 reminder with 1024, 288
Suffix 46904096 reminder with 1024, 800
Suffix 246904096 reminder with 1024, 288
Now we can see that at index 5 both reminders
are 0, so the string can be broken into 24680
and 4096.
We can get (i)th suffix reminder by (i – 1)th suffix reminder as, (sr[i] = sr[i - 1] * 10 + s[i]) % f, i.e. just multiply previous reminder by 10 and add current digit and then take reminder by f.
For getting (i)th prefix reminder by (i + 1)th prefix reminder, we can do, (pr[i] = pr[i + 1] + s[i] * base) % s, i.e. add next reminder and current digit multiplied with base value which will be 1 for last digit, 10 for second last digit and so on and then we will take reminder by s overall.
Total time complexity of solution will O(N)
C++
// C++ code to break the number string into
// two divisible parts by given numbers
#include <bits/stdc++.h>
using namespace std;
// method prints divisible parts if possible,
// otherwise prints 'Not possible'
void printTwoDivisibleParts(string num, int f, int s)
{
int N = num.length();
// creating arrays to store reminder
int prefixReminder[N + 1];
int suffixReminder[N + 1];
suffixReminder[0] = 0;
// looping over all suffix and storing
// reminder with f
for (int i = 1; i < N; i++)
// getting suffix reminder from previous
// suffix reminder
suffixReminder[i] = (suffixReminder[i - 1] * 10 +
(num[i - 1] - '0')) % f;
prefixReminder[N] = 0;
int base = 1;
// looping over all prefix and storing
// reminder with s
for (int i = N - 1; i >= 0; i--) {
// getting prefix reminder from next
// prefix reminder
prefixReminder[i] = (prefixReminder[i + 1] +
(num[i] - '0') * base) % s;
// updating base value
base = (base * 10) % s;
}
// now looping over all reminders to check
// partition condition
for (int i = 0; i < N; i++) {
// if both reminders are 0 and digit itself
// is not 0, then print result and return
if (prefixReminder[i] == 0 &&
suffixReminder[i] == 0 &&
num[i] != '0') {
cout << num.substr(0, i) << " "
<< num.substr(i) << endl;
return;
}
}
// if we reach here, then string can' be
// partitioned under constraints
cout << "Not Possible\n";
}
// Driver code to test above methods
int main()
{
string num = "246904096";
int f = 12345;
int s = 1024;
printTwoDivisibleParts(num, f, s);
return 0;
}
Java
// Java program to break the number string
// into two divisible parts by given numbers
public class DivisibleParts
{
// method prints divisible parts if
// possible, otherwise prints 'Not possible'
static void printTwoDivisibleParts(String num,
int f, int s)
{
int N = num.length();
// creating arrays to store reminder
int[] prefixReminder = new int[N + 1];
int[] suffixReminder = new int[N + 1];
suffixReminder[0] = 0;
// looping over all suffix and storing
// reminder with f
for (int i = 1; i < N; i++)
// getting suffix reminder from
// previous suffix reminder
suffixReminder[i] = (suffixReminder[i - 1] * 10 +
(num.charAt(i - 1) - '0')) % f;
prefixReminder[N] = 0;
int base = 1;
// looping over all prefix and storing
// reminder with s
for (int i = N - 1; i >= 0; i--) {
// getting prefix reminder from next
// prefix reminder
prefixReminder[i] = (prefixReminder[i + 1] +
(num.charAt(i ) - '0') * base) % s;
// updating base value
base = (base * 10) % s;
}
// now looping over all reminders to
// check partition condition
for (int i = 0; i < N; i++) {
// if both reminders are 0 and digit
// itself is not 0, then print result
// and return
if (prefixReminder[i] == 0 &&
suffixReminder[i] == 0 &&
num.charAt(i ) != '0') {
System.out.println( num.substring(0, i)
+" " + num.substring(i));
return;
}
}
// if we reach here, then string can' be
// partitioned under constraints
System.out.println("Not Possible");
}
/* Driver program */
public static void main(String[] args)
{
String num = "246904096";
int f = 12345;
int s = 1024;
printTwoDivisibleParts(num, f, s);
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 code to break the
# number string into two
# divisible parts by given
# numbers
# method prints divisible
# parts if possible, otherwise
# prints 'Not possible'
def printTwoDivisibleParts(num, f, s):
N = len(num);
# creating arrays
# to store reminder
prefixReminder = [0]*(N + 1);
suffixReminder = [0]*(N + 1);
# looping over all
# suffix and storing
# reminder with f
for i in range(1,N):
# getting suffix
# reminder from previous
# suffix reminder
suffixReminder[i] = (suffixReminder[i - 1] * 10 +
(ord(num[i - 1]) -
48)) % f;
base = 1;
# looping over all
# prefix and storing
# reminder with s
for i in range(N - 1,-1,-1):
# getting prefix
# reminder from next
# prefix reminder
prefixReminder[i] = (prefixReminder[i + 1] +
(ord(num[i]) - 48) *
base) % s;
# updating base value
base = (base * 10) % s;
# now looping over
# all reminders to check
# partition condition
for i in range(N):
# if both reminders are
# 0 and digit itself
# is not 0, then print
# result and return
if (prefixReminder[i] == 0 and suffixReminder[i] == 0 and num[i] != '0'):
print(num[0:i],num[i:N]);
return 0;
# if we reach here, then
# string can' be partitioned
# under constraints
print("Not Possible");
# Driver code
if __name__=='__main__':
num = "246904096";
f = 12345;
s = 1024;
printTwoDivisibleParts(num,f, s);
# This code is contributed
# by mits
C#
// C# program to break the
// number string into two
// divisible parts by given
// numbers
using System;
class GFG
{
// method prints divisible
// parts if possible, otherwise
// prints 'Not possible'
static void printTwoDivisibleParts(String num,
int f, int s)
{
int N = num.Length;
// creating arrays to
// store reminder
int[] prefixReminder = new int[N + 1];
int[] suffixReminder = new int[N + 1];
suffixReminder[0] = 0;
// looping over all
// suffix and storing
// reminder with f
for (int i = 1; i < N; i++)
// getting suffix reminder from
// previous suffix reminder
suffixReminder[i] = (suffixReminder[i - 1] * 10 +
(num[i - 1] - '0')) % f;
prefixReminder[N] = 0;
int base1 = 1;
// looping over all
// prefix and storing
// reminder with s
for (int i = N - 1; i >= 0; i--)
{
// getting prefix reminder
// from next prefix reminder
prefixReminder[i] = (prefixReminder[i + 1] +
(num[i] - '0') * base1) % s;
// updating base1 value
base1 = (base1 * 10) % s;
}
// now looping over all
// reminders to check
// partition condition
for (int i = 0; i < N; i++)
{
// if both reminders are
// 0 and digit itself is
// not 0, then print result
// and return
if (prefixReminder[i] == 0 &&
suffixReminder[i] == 0 &&
num[i] != '0')
{
Console.WriteLine(num.Substring(0, i) +
" " + num.Substring(i));
return;
}
}
// if we reach here, then
// string can' be partitioned
// under constraints
Console.WriteLine("Not Possible");
}
// Driver Code
public static void Main()
{
String num = "246904096";
int f = 12345;
int s = 1024;
printTwoDivisibleParts(num, f, s);
}
}
// This code is contributed by mits
PHP
<?php
// PHP code to break the
// number string into two
// divisible parts by given
// numbers
// method prints divisible
// parts if possible, otherwise
// prints 'Not possible'
function printTwoDivisibleParts($num, $f, $s)
{
$N = strlen($num);
// creating arrays
// to store reminder
$prefixReminder = array_fill(0, $N + 1, 0);
$suffixReminder = array_fill(0, $N + 1, 0);
// looping over all
// suffix and storing
// reminder with f
for ($i = 1; $i < $N; $i++)
// getting suffix
// reminder from previous
// suffix reminder
$suffixReminder[$i] = ($suffixReminder[$i - 1] * 10 +
(ord($num[$i - 1]) -
48)) % $f;
$base = 1;
// looping over all
// prefix and storing
// reminder with s
for ($i = $N - 1; $i >= 0; $i--)
{
// getting prefix
// reminder from next
// prefix reminder
$prefixReminder[$i] = ($prefixReminder[$i + 1] +
(ord($num[$i]) - 48) *
$base) % $s;
// updating base value
$base = ($base * 10) % $s;
}
// now looping over
// all reminders to check
// partition condition
for ($i = 0; $i < $N; $i++)
{
// if both reminders are
// 0 and digit itself
// is not 0, then print
// result and return
if ($prefixReminder[$i] == 0 &&
$suffixReminder[$i] == 0 &&
$num[$i] != '0')
{
echo substr($num, 0, $i)." ".
substr($num,$i)."\n";
return;
}
}
// if we reach here, then
// string can' be partitioned
// under constraints
echo "Not Possible\n";
}
// Driver code
$num = "246904096";
$f = 12345;
$s = 1024;
printTwoDivisibleParts($num,
$f, $s);
// This code is contributed
// by mits
?>
JavaScript
<script>
// javascript program to break the
// number string into two
// divisible parts by given
// numbers
// method prints divisible
// parts if possible, otherwise
// prints 'Not possible'
function printTwoDivisibleParts( num, f, s)
{
var N = num.length;
// creating arrays to
// store reminder
var prefixReminder = []
var suffixReminder = []
suffixReminder[0] = 0;
// looping over all
// suffix and storing
// reminder with f
for (var i = 1; i < N; i++)
// getting suffix reminder from
// previous suffix reminder
suffixReminder[i] = (suffixReminder[i - 1] * 10 + (num[i - 1] - '0')) % f;
prefixReminder[N] = 0;
var base1 = 1;
// looping over all
// prefix and storing
// reminder with s
for (var i = N - 1; i >= 0; i--)
{
// getting prefix reminder
// from next prefix reminder
prefixReminder[i] = (prefixReminder[i + 1] + (num[i] - '0') * base1) % s;
// updating base1 value
base1 = (base1 * 10) % s;
}
// now looping over all
// reminders to check
// partition condition
for (var i = 0; i < N; i++)
{
// if both reminders are
// 0 and digit itself is
// not 0, then print result
// and return
if (prefixReminder[i] == 0 && suffixReminder[i] == 0 && num[i] != '0')
{
document.write(num.substring(0, i) + " " + num.substring(i));
return;
}
}
// if we reach here, then
// string can' be partitioned
// under constraints
document.write("Not Possible");
}
// Driver Code
var num = "246904096";
var f = 12345;
var s = 1024;
printTwoDivisibleParts(num, f, s);
// This code is contributed by bunnyram19.
</script>
Output:
24690 4096
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Divide given number into two even parts
Given a number N, the task is to check if this number can be divided into 2 even parts. Examples: Input: N = 8Output: YESExplanation: 8 can be divided into two even parts in two ways, 2, 6 or 4, 4 as both are even. Input: N = 5Output: NO Naive approach: Check all number pairs upto N, such that they
5 min read
Smallest n digit number divisible by given three numbers
Given x, y, z and n, find smallest n digit number which is divisible by x, y and z. Examples: Input : x = 2, y = 3, z = 5 n = 4Output : 1020Input : x = 3, y = 5, z = 7 n = 2Output : Not possibleRecommended PracticeMighty DivisorTry It!Method: Brute-forceThe brute-force approach to solve this problem
15+ min read
Sum of n digit numbers divisible by a given number
Given n and a number, the task is to find the sum of n digit numbers that are divisible by given number.Examples: Input : n = 2, number = 7Output : 728Explanation: There are thirteen n digit numbers that are divisible by 7. Numbers are : 14+ 21 + 28 + 35 + 42 + 49 + 56 + 63 +70 + 77 + 84 + 91 + 98.
9 min read
Count n digit numbers divisible by given number
Given number of digit n and a number, the task is to count all the numbers which are divisible by that number and having n digit. Examples : Input : n = 2, number = 7Output : 13Explanation: There are nine n digit numbers that are divisible by 7. Numbers are 14, 21, 28, 35, 42, 49, .... 91, 98 Input
8 min read
Smallest K digit number divisible by all numbers in given array
Given an array arr[]. The task is to create the smallest K digit number divisible by all numbers of arr[]. Examples: Input: arr[] = {2, 3, 5}, N = 3Output: 120Explanation: 120 is divisible by 2, 3 and 5 Input: arr[] = {2, 6, 7, 4, 5}, N = 5Output: 10080 Recursive approach: This problem can be solved
7 min read
Count of numbers upto M divisible by given Prime Numbers
Given an array arr[] of Prime Numbers and a number M, the task is to count the number of elements in the range [1, M] that are divisible by any of the given prime numbers. Examples: Input: arr[] = {2, 3, 5, 7} M = 100 Output: 78 Explanation:In total there are 78 numbers that are divisible by either
5 min read
Count N digit numbers divisible by both given numbers X and Y
Given X, Y, and N. Find the number of N digit integers divisible by both X and Y. Examples: Input: N = 2, X = 5, Y = 7Output: 2Explanation: There are only 2 two-digit numbers divisible by both 5 and 7. They are 35 and 70. Input: N = 1, X = 2, Y = 3Output: 1Explanation: 6 is the only 1-digit number d
7 min read
Smallest number divisible by first n numbers
Given a number n find the smallest number evenly divisible by each number 1 to n.Examples: Input : n = 4 Output : 12 Explanation : 12 is the smallest numbers divisible by all numbers from 1 to 4 Input : n = 10 Output : 2520 Input : n = 20 Output : 232792560If you observe carefully the ans must be th
8 min read
Partition a number into two divisible parts
Given a number (as string) and two integers a and b, divide the string in two non-empty parts such that the first part is divisible by a and the second part is divisible by b. If the string can not be divided into two non-empty parts, output "NO", else print "YES" with the two parts. Examples: Input
15+ min read
Count the numbers divisible by 'M' in a given range
A and B are two numbers which define a range, where A <= B. Find the total numbers in the given range [A ... B] divisible by 'M'Examples: Input : A = 25, B = 100, M = 30 Output : 3 Explanation : In the given range [25 - 100], 30, 60 and 90 are divisible by 30 Input : A = 6, B = 15, M = 3 Output :
9 min read