CRAN02 - Roommate Agreement
Leonard was always sickened by how Sheldon considered himself better than him. To decide once and for all who is better among them they decided to ask each other a puzzle. Sheldon pointed out that according to Roommate Agreement Sheldon will ask first. Leonard seeing an opportunity decided that the winner will get to rewrite the Roommate Agreement.
Sheldon thought for a moment then agreed to the terms thinking that Leonard will never be able to answer right. For Leonard, Sheldon thought of a puzzle which is as follows. He gave Leonard n numbers, which can be both positive and negative. Leonard had to find the number of continuous sequence of numbers such that their sum is zero.
For example if the sequence is- 5, 2, -2, 5, -5, 9
There are 3 such sequences
2, -2, 5, -5
Since this is a golden opportunity for Leonard to rewrite the Roommate Agreement and get rid of Sheldon's ridiculous clauses, he can't afford to lose. So he turns to you for help. Don't let him down.
First line contains T - number of test cases
Second line contains n - the number of elements in a particular test case.
Next line contain n elements, ai (1 <= i <= n) separated by spaces.
The number of such sequences whose sum if zero.
1 <= t <= 5
1 <= n <= 10^6
-10 <= ai <= 10
Input: 2 4 0 1 -1 0 6 5 2 -2 5 -5 9
Output: 6 3
How to solve this by using 2pointers?
Last edit: 2017-10-02 01:58:49
Is there any one who solve it with two pointers technique??
Trailing spaces in input. Got WA in python because of that.
Declare long long !!
Prefix Sum and Hashmap. Easy :)
use sum array to store prefix sum ,and count the frequency .
Simple one forget to declare result as long long cause me 1 WA
same as SUBSEQ :D