This question was asked in the coding round of the company in our placements.
For a string consisting of numbers from 1 to 9. We need to arrange the string so that it is divided into groups. We need to consider no. possible rows, such that the sum of the group <= the sum of the next consecutive group.
Example1
input: 1234
output: 6
Rows:
- {1,2,3,4}
- {1,2,34}
- {12,3,4}
- {12.34}
- {} 1234
- {1234}
the first combination is here, 1 <2 <3 <4. the second combination, 1 <2 (3 + 4). and so on.
Example2
entrance: 516
output: 3
Rows:
Now, the time taken by brute force to generate all the rows is O (2 ^ (n-1)). My question is how to solve it better than brute force?
Restriction: input line length 1 <= n <= 1000
source share