Check if a large number can be divided into two or more segments of equal sum
Given a very large Number N. The task is to check if the number can be divided into two or more segments of an equal sum.
Examples:
Input: N = 73452
Output: Yes
Segments of {7}, {3, 4}, {5, 2} which has equal sum of 7
Input: N = 1248
Output: No
The following steps can be followed to solve the problem:
- Since the number can be large, the number is initialized in a string.
- Use prefixSum array to store the prefix sum of the array.
- Now traverse from the second element to last, and the first segment thus will be 0 to i-1, whose sum is Prefixsum[i-1].
- Use another pointer that traverses from i to n, and keep adding the sum.
- If the sum at any stage is equal to Prefixsum[i-1], then the segment has a sum equal to first.
- Reinitialize the segment sum value to 0 and keep moving the pointer.
- If at any stage the segment sum exceeds the sum of the first segment, then break, as the division with segment sum as prefixsum[i-1] is not possible.
- If the pointer reaches the last number, check if the last segment sum is equal to the first segment sum i.e., prefixsum[i-1], then it can be divided into segments of the equal sum.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool check(string s)
{
int n = s.length();
int Presum[n];
Presum[0] = s[0] - '0' ;
for ( int i = 1; i < n; i++) {
Presum[i] = Presum[i - 1] + (s[i] - '0' );
}
for ( int i = 1; i <= n - 1; i++) {
int sum = Presum[i - 1];
int presum = 0;
int it = i;
int flag = 0;
while (it < n) {
presum += s[it] - '0' ;
if (presum == sum) {
presum = 0;
flag = 1;
}
else if (presum > sum) {
break ;
}
it++;
}
if (presum == 0 && it == n && flag == 1) {
return true ;
}
}
return false ;
}
int main()
{
string s = "73452" ;
if (check(s))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
public class GFG {
static boolean check(String s)
{
int n = s.length();
int [] Presum = new int [n];
char [] s1 = s.toCharArray();
Presum[ 0 ] = s1[ 0 ] - '0' ;
for ( int i = 1 ; i < n; i++) {
Presum[i] = Presum[i - 1 ] + (s1[i] - '0' );
}
for ( int i = 1 ; i <= n - 1 ; i++) {
int sum = Presum[i - 1 ];
int presum = 0 ;
int it = i;
int flag = 0 ;
while (it < n) {
presum += s1[it] - '0' ;
if (presum == sum) {
presum = 0 ;
flag = 1 ;
}
else if (presum > sum) {
break ;
}
it++;
}
if (presum == 0 && it == n && flag == 1 ) {
return true ;
}
}
return false ;
}
public static void main(String[] args) {
String s = "73452" ;
if (check(s))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(s):
n = len (s)
Presum = [ 0 ] * n
Presum[ 0 ] = int (s[ 0 ])
for i in range ( 1 , n):
Presum[i] = Presum[i - 1 ] + int (s[i])
for i in range ( 1 , n - 1 ):
sum = Presum[i - 1 ]
presum = 0
it = i
flag = 0
while it < n:
presum + = int (s[it])
if presum = = sum :
presum = 0
flag = 1
elif presum > sum :
break
it + = 1
if presum = = 0 and it = = n and flag = = 1 :
return True
return False
s = "73452"
if check(s):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool check(String s)
{
int n = s.Length;
int [] Presum = new int [n];
char [] s1 = s.ToCharArray();
Presum[0] = s1[0] - '0' ;
for ( int i = 1; i < n; i++)
{
Presum[i] = Presum[i - 1] + (s1[i] - '0' );
}
for ( int i = 1; i <= n - 1; i++)
{
int sum = Presum[i - 1];
int presum = 0;
int it = i;
int flag = 0;
while (it < n)
{
presum += s1[it] - '0' ;
if (presum == sum)
{
presum = 0;
flag = 1;
}
else if (presum > sum)
{
break ;
}
it++;
}
if (presum == 0 && it == n && flag == 1)
{
return true ;
}
}
return false ;
}
public static void Main(String[] args)
{
String s = "73452" ;
if (check(s))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function check(s)
{
let n = s.length;
var Presum = [];
Presum.push(parseInt(s[0]));
let i;
for (i = 1; i < n; i++) {
Presum.push(Presum[i - 1] + parseInt(s[i]));
}
for (i = 1; i <= n - 1; i++) {
let sum = Presum[i - 1];
let presum = 0;
let it = i;
let flag = 0;
while (it < n) {
presum += parseInt(s[it]);
if (presum == sum) {
presum = 0;
flag = 1;
}
else if (presum > sum) {
break ;
}
it++;
}
if (presum == 0 && it == n && flag == 1) {
return true ;
}
}
return false ;
}
var s = "73452" ;
if (check(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(N)
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...