I encountered Return Gift problem and tried to solve it by the following approach -
- I have made a 2d array where the row represents the numbers in the array and column represents the index from where the window is starting. Basically, dp[i][j] contains the frequency of ith element in the window starting from j.
- I first calculate the frequency of each element which are in k window size of 0th window then I have made a maxarr array whose ith element represents the exponential sum of window starting from i. So for now I have dp which contains the frequency of element of 0th window and maxarr which contains exponential sum of 0th window.
- Starting from index 1 I copy the frequency of previous columns as it is except for 2 case - if element is just excluded compare to previous window then I put the frequenct prev-1 and if then element is just added in the window then put prev+1 in current column.
- After this I calculated the maxarr's all element and find the maximum.
However I got only 2 case accepted. Kindly help me to correct my mistake. Do I need to update my code here?