Solution 3 for Scaler Topics Fortnightly Contest - 12
Learn via video course

DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
This article is part of the Scaler Topics Fortnightly Contest - 12
Solution Approach
- Note that the Brute force of can be reduced to by considering the fact that only tuple with property will be counted.
- Can you optimize it further?
- This question can be solved in by mapping all elements into very big elements let's say in the range 1 to and the sum of subarrays can be used as hash values.
- Now we can just store the sum in the map to find an answer in .
- Note that a similar array must have the same sum, although the converse is not true: for example but 1, 2, and 3 are randomly mapped to a very big integer.
- Thus the possibility of collision is very low.