# Top down dp for XORSUB problem

Can any one write top down dp solution for this problem and please also explain what are the overlaping sub problems in this problem.

Let’s first develop a recursive approach for the problem. The question says, we need to find subsets such the the `xor` result of elements of the subset and the number `k` should be maximized. So, basically, we need to find the subset. See, the below code:

``````void findMaxXor(int idx, int currXor) {
if (idx == n) {
maxXor = max(maxXor,k^currXor);
return;
}

// either include idx'th element or don't include
findMaxXor(idx + 1, currXor);
findMaxXor(idx+1, (currXor^a[idx]));
``````

}

Every element has two possibilities, either include that element in the subset or don’t include it. Variable `idx` is an iterator for the array elements and `currXor` tells the current xor result. Following is the recursive tree:

``````           (0,0) // 0'th element and current xor is 0
/    \
(exc a)   (inc a)
/             \
(1,0)           (1,(0^a)) ; (after including xor becomes currXor^a)
``````

… Continue with this tree, and you’ll get overlapping sub-problems which will confirm you that DP is applicable.

We can now memoize the above recursive solution in a very easy manner. Whenever you need to this, make sure the only parameters you pass in recursive calls are the ones changing. Example, I have passed only two variables `idx` and `currXor`. All other variables I need like `k` and `n` are not passed in recursive calls. This helps us to visualize the DP states, the number of which is equal to the number of changing parameters in the recursive call.
So, for above, we have two DP states. So, our DP table will be `dp[i][j]` which means the `xor result j` can be achieved from the subsets of `array[0...i]`. Thus, dp[i][j] = 1 tells that we can get the xor result j from the subsets of a[0…i] i.e. we find what all xor results we can get from all the subsets of array. So, let’s memoize:

``````int dp;  // initialize this to -1
``````

void findMaxXorDp ( ll idx, ll currXor ) {

``````if ( idx == n ) {
dp[n-1][currXor] = 1;
return;
}

if ( dp[idx][currXor] != -1 )  // if it is not -1, then it means, we have faced this subproblem before also, so we'll not proceed with this.
return;

dp[idx][currXor] = 1;  // mark this as 1 to show that from the subsets of a[0..idx], we can get currXor as the xor result

findMaxXorDp(idx+1,currXor);  // don't include this idx'th element
findMaxXorDp(idx+1, (currXor^a[idx])); // include this idx'th element
``````

}

``````ll res = 0;
for ( ll i=0; i<=1024; i++ ) {
if ( dp[n-1][i] != -1 )
res = max(res,(i^k));
}
``````

Next, we find the maximum xor result as we have all possible xor results we can get from the subsets of a[0…n].

Hope this clears.
Feel free to ask any doubts.

//
{"ft_lang_label":"__label__en","ft_lang_prob":0.8285155,"math_prob":0.97720814,"size":2651,"snap":"2021-31-2021-39","text_gpt3_token_len":765,"char_repetition_ratio":0.12920287,"word_repetition_ratio":0.008421052,"special_character_ratio":0.2900792,"punctuation_ratio":0.13508771,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99710083,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-02T19:35:45Z\",\"WARC-Record-ID\":\"<urn:uuid:e4f5fc68-d947-4514-8b8b-517e276f9285>\",\"Content-Length\":\"15479\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e6bf2b5b-7eb0-4aa2-acf9-dee5e757c752>\",\"WARC-Concurrent-To\":\"<urn:uuid:ba59a59a-32d8-4eb7-a333-38c9594409a6>\",\"WARC-IP-Address\":\"54.166.204.247\",\"WARC-Target-URI\":\"https://discusstest.codechef.com/t/top-down-dp-for-xorsub-problem/13927\",\"WARC-Payload-Digest\":\"sha1:HT4YZDVKRDNMEWIKBXXYSBYG7KNWLVZT\",\"WARC-Block-Digest\":\"sha1:BBPE7Q5GWDLEH4BFB2QKNR5Q2J7WIZGI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046154356.39_warc_CC-MAIN-20210802172339-20210802202339-00190.warc.gz\"}"}