Find two subsequence such that their sum is equal

Viewed 28

You are given two integers N and A. You are required to find two subsequences such that their sum is equal, in the array having elements between [1-N] neglecting the integer A.

Example:

Input: N = 6, A = 5;
Then Array = [1, 2, 3, 4, 6], as we neglected element A i.e 5 here.
Output: {1, 2, 3} and {6}
Explanation: The sum of subsequences {1, 2, 3} = 6 and the sum of subsequence {6} is 6.

Input Format:

Take input of two integers which are space separated i.e N and A.

Output Format:

As we can clearly see that we are dividing the array into parts i.e subsequences, you are required to give an output of array (1-indexed array) having length of N, where each ith index indicates the element from 1-N. If ith element is in the first subsequence then fill it 1 and if it is in the second subsequence fill it with 2, while the element A and elements that do not take part in any of these two subsequence are filled with 0.

Example 1:

Input: N = 5, A = 2;
Output: [1, 0, 1, 2, 0]
Explanation: N = 5 then the array = [1, 2, 3, 4, 5], As we have A = 2 then the element 2
would not take part. Therefore the two subsequences are {1, 3} and {4,}, thus the output 
is [1, 0, 1, 2, 0].

Example 2:

Input: N = 6, A = 5;
Output: [1, 1, 1, 0, 0, 2]

Constraints:

1 <= N <= 100
1 <= A <= N

NOTE: It is guaranteed to have solution.

0 Answers
Related