Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Partitions with Given Difference

https://www.geeksforgeeks.org/problems/partitions-with-given-difference/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=partitions-with-given-difference

Given an array arr[], partition it into two subsets(possibly empty) such that each element must belong to only one subset. Let the sum of the elements of these two subsets be sum1 and sum2. Given a difference d, count the number of partitions in which sum1 is greater than or equal to sum2 and the difference between sum1 and sum2 is equal to d.

Examples :

Input: arr[] = [5, 2, 6, 4], d = 3 Output: 1 Explanation: There is only one possible partition of this array. Partition : {6, 4}, {5, 2}. The subset difference between subset sum is: (6 + 4) - (5 + 2) = 3. Input: arr[] = [1, 1, 1, 1], d = 0 Output: 6 Explanation: We can choose two 1's from indices {0,1}, {0,2}, {0,3}, {1,2}, {1,3}, {2,3} and put them in sum1 and remaning two 1's in sum2. Thus there are total 6 ways for partition the array arr. Input: arr[] = [1, 2, 1, 0, 1, 3, 3], d = 11 Output: 2 Constraint: 1 <= arr.size() <= 50 0 <= d <= 50 0 <= arr[i] <= 6