Given a number x, print all possible non-increasing sequences with sum equals to x.
Input: x = 3 Output: 1 1 1 2 1 3 Input: x = 4 Output: 1 1 1 1 2 1 1 2 2 3 1 4
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to use a recursive function, an array arr to store all sequences one by one and an index variable curr_idx to store current next index in arr. Below is algorithm.
1) If current sum is equal to x, then print current sequence.
2) Place all possible numbers from 1 to x-curr_sum numbers at curr_idx in array. Here curr_sum is sum of current elements in arr. After placing a number, recur for curr_sum + number and curr_idx+1,
Below is the implementation of above steps.
1 1 1 1 1 2 1 1 1 2 2 1 3 1 1 3 2 4 1 5
This article is contributed by Ashish Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Print all sequences of given length
- Print all sequences starting with n and consecutive difference limited to k
- Print all increasing sequences of length k from first n natural numbers
- Print all longest common sub-sequences in lexicographical order
- Minimum number of consecutive sequences that can be formed in an array
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Print all Semi-Prime Numbers less than or equal to N
- Print all Jumping Numbers smaller than or equal to a given value
- Number of sequences which has HEAD at alternate positions to the right of the first HEAD
- Count number of triplets with product equal to given number with duplicates allowed
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Count number of subsets of a set with GCD equal to a given number
- Print a number strictly less than a given number such that all its digits are distinct.
- Sum of digits equal to a given number in PL/SQL
- Find a number x such that sum of x and its digits is equal to given n.