bum+=arr[i]; bum-=arr[i];
This is wrong. It should be bum-=arr[i-p];
Sorry for such a silly mistake . Thanks a lot