Given an array of length n, find number of subsets where XOR of a subset is equal to a given number [closed]
Given an array, arr , of length n , find how many subsets of arr there are such that XOR(^) of those subsets is equal to a given number, ans . I have this dp approach but is there a way to improve its time complexity. ans is always less than 1024. Here ans is the no. such that XOR(^) of the subsets is equal to it. arr[n] contains all the numbers memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for(i = 1; i <= n; i++){ for(j = 0; j < 1024; j++) { dp[i][j] = (dp[i-1][j] + dp[i-1][j^arr[i]]); } } cout << (dp[n][ans]); Mohit Jain From user3386109 's comment, building on top of your code: /* Warning: