- Related Questions & Answers
- Find if array can be divided into two subarrays of equal sum in C++
- Check if a number can be expressed as sum two abundant numbers in C++
- Find the sums for which an array can be divided into subarrays of equal sum in Python
- Check if two line segments intersect
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Check if a number can be written as sum of three consecutive integers in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Check if two StringDictionary objects are equal or not in C#
- Check if a number can be represented as sum of non zero powers of 2 in C++
- Check if a large number is divisible by 11 or not in C++
- Check if a large number is divisible by 25 or not in C++
- Check if a large number is divisible by 3 or not in C++
- Check if a large number is divisible by 5 or not in C++
- Check if a large number is divisible by 75 or not in C++
- Check if a large number is divisible by 8 or not in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Here we will see a program, that can check whether a number can be divided into more than one segments with equal sum. Suppose a number is like 74325, then this can be segmented into three parts (7), (4, 3), (2, 5), all are of same um value.

We have to follow these steps to solve this problem.

- Take the number as string
- use an array to hold prefix sum of the array
- Now traversing from second element to last element, and the first segment will be 0 to i-1, whose sum will be placed at prefix_sum[i - 1]
- Use another variable which traverses from 1 to n, then keep adding the sum.
- If the sum value is same as the prefix_sum[i – 1] at any stage, then the segment has sum equal to the first.
- Re initialize the segment sum value as 0, then keep moving the pointer.
- If at any stage the segment sum is greater than the prefix_sum[i – 1] then break the loop
- If we reach at the last destination, and if last segment sum is same as the first segment sum, then it can be divided into segments of equal sum.

#include <iostream> using namespace std; bool canBeSegmented(string str) { int n = str.length(); int prefix_sum[n]; prefix_sum[0] = str[0] - '0'; for (int i = 1; i < n; i++) { prefix_sum[i] = prefix_sum[i - 1] + (str[i] - '0'); } for (int i = 1; i <= n - 1; i++) { int sum = prefix_sum[i - 1]; int prev_sum = 0; int it = i; bool flag = false; while (it < n) { prev_sum += str[it] - '0'; if (prev_sum == sum) { prev_sum = 0; flag = true; } else if (prev_sum > sum) { break; } it++; } if (prev_sum == 0 && it == n && flag) { return true; } } return false; } int main() { string s = "74325"; if (canBeSegmented(s)) cout << "Yes, This can be segmented into more than two segments"; else cout << "No, This can not be segmented into more than two segments"; }

Yes, This can be segmented into more than two segments

Advertisements