I was solving this problem on Leetcode- Link- Problem . I just thought of variation, “If no duplicates as well as no rearrangements is possible, count total ways to reach the target”. How to solve this? Example(as given in Leetcode]-
[1,2,3], Target=4
[1,1,1,1]
[1,1,2]
[1,3]
[2,2]
So total answer = 4
My code for this problem-
Code
int combinationSum4(vector<int>& nums, int target) {
long long dp[target+1];
dp[0]=1;
for(int i=1;i<=target;i++){
dp[i]=0;
for(int j=0;j<nums.size();j++){
if(i>=nums[j]){
dp[i]+=dp[i-nums[j]];
}
}
}
return dp[target];
}
How to modify the same code to the variant problem?