This is very intersting problem.I almost solved this problem correctly but i did a small mistake.Actually I solved this problem using prefix sum.First I calculated left sum then top to bottom sum.Then I print this , a[x2][y2]-a[x1-1][y2]-a[x2][y1-1].Sample test passed but wrong answer in hidden test cases. Then I thought more and got where I made mistake. Acctualy I forgot to add a[x-1][y-1].The acctual answer will be a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x-1][y-1] For better understand, check this image first https://www.techiedelight.com/wp-content/uploads/Result.png Thank You